Generate N random numbers with a given probability and condition

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Generate N random numbers with a given probability and condition

Nelly Reduan
Dear all,

I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:

x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))

But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
Many thanks for your time
Nell


        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Jeff Newmiller
This looks like homework (which is off topic here per the Posting Guide). Also, please send your emails in plain text format to avoid us seeing your message differently than you do.

On July 4, 2018 3:21:34 PM PDT, Nelly Reduan <[hidden email]> wrote:

>Dear all,
>
>I would like to generate N random numbers with a given probability and
>condition but I'm not sure how to do this.
>For example, I have N = 20 and the vector from which to choose is
>seq(0, 10, 1). I have tested:
>
>x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28,
>times=length(seq(0, 10, 1))))
>
>But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
>Many thanks for your time
>Nell
>
>
> [[alternative HTML version deleted]]

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Rolf Turner
In reply to this post by Nelly Reduan

On 05/07/18 10:21, Nelly Reduan wrote:

> Dear all,
>
> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
>
> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
>
> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
> Many thanks for your time.

Your thinking requires considerable clarification.

(1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.

(2) Hence length(seq(0,10,1)) is 11.

(3) Likewise max(seq(0,10,1)) is 10.

(4) Your prob vector is *constant* --- so specifying "prob" makes
     no difference --- the result is the same as if you omitted "prob".

(5) You need to think carefully about what you really mean by "random".
     In what way do you want the final result to be "random"?

I expect that the lecturer who assigned this problem to you  needs to
clarify his/her thinking as well.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Nelly Reduan
Thank you very much for your reply.


By omitting the probability, the expected results could be:


c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0)


c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)


If I omit the probability, I would like to generate N random positive integers that sum to M and the integers would be selected from a uniform distribution.


Many thanks for your time

Nell


________________________________
De : Rolf Turner <[hidden email]>
Envoyé : mercredi 4 juillet 2018 16:11:11
À : Nelly Reduan
Cc : [hidden email]
Objet : Re: [R] Generate N random numbers with a given probability and condition


On 05/07/18 10:21, Nelly Reduan wrote:

> Dear all,
>
> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
>
> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
>
> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
> Many thanks for your time.

Your thinking requires considerable clarification.

(1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.

(2) Hence length(seq(0,10,1)) is 11.

(3) Likewise max(seq(0,10,1)) is 10.

(4) Your prob vector is *constant* --- so specifying "prob" makes
     no difference --- the result is the same as if you omitted "prob".

(5) You need to think carefully about what you really mean by "random".
     In what way do you want the final result to be "random"?

I expect that the lecturer who assigned this problem to you  needs to
clarify his/her thinking as well.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Jim Lemon-4
Hi Nell,
I may not have the right idea about this, but I think you need to do
this in two steps if it can be done. Let's say you want a sequence of
20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate
the monotonically increasing sequences like this:

c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2)
...
c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10)

So if you select one of these sequences at random, there will be a
further set of sequences that are permutations of it. By randomly
selecting one of those permutations, I think you can solve your
problem. However, this is going to be computationally intensive, with
the set of permutations being very large for large N. Here is an
example using N = M = 5:

# enumerate the sequences = M
rs5<-list(c(1,1,1,1,1),c(0,1,1,1,2),c(0,0,1,1,3),c(0,0,0,1,4),
 c(0,0,1,2,2),c(0,0,0,2,3),c(0,0,0,0,5))
library(crank)
# generate the permutations for one sequence (120 in this case)
rs5_s1<-permute(rs5[[sample(1:length(rs5),1)]])
# select one of the permutations at random
rs5_s1[sample(1:dim(rs5_s1)[1],1),]
[1] 4 0 1 0 0

Jim

On Wed, Jul 11, 2018 at 10:11 AM, Nelly Reduan <[hidden email]> wrote:

> Thank you very much for your reply.
>
>
> By omitting the probability, the expected results could be:
>
>
> c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0)
>
>
> c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
>
>
> If I omit the probability, I would like to generate N random positive integers that sum to M and the integers would be selected from a uniform distribution.
>
>
> Many thanks for your time
>
> Nell
>
>
> ________________________________
> De : Rolf Turner <[hidden email]>
> Envoyé : mercredi 4 juillet 2018 16:11:11
> À : Nelly Reduan
> Cc : [hidden email]
> Objet : Re: [R] Generate N random numbers with a given probability and condition
>
>
> On 05/07/18 10:21, Nelly Reduan wrote:
>
>> Dear all,
>>
>> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
>> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
>>
>> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
>>
>> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
>> Many thanks for your time.
>
> Your thinking requires considerable clarification.
>
> (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.
>
> (2) Hence length(seq(0,10,1)) is 11.
>
> (3) Likewise max(seq(0,10,1)) is 10.
>
> (4) Your prob vector is *constant* --- so specifying "prob" makes
>      no difference --- the result is the same as if you omitted "prob".
>
> (5) You need to think carefully about what you really mean by "random".
>      In what way do you want the final result to be "random"?
>
> I expect that the lecturer who assigned this problem to you  needs to
> clarify his/her thinking as well.
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Bert Gunter-2
In reply to this post by Nelly Reduan
You need to heed Rolf's advice.

N random integers by definition cannot have a fixed sum.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Tue, Jul 10, 2018 at 5:11 PM, Nelly Reduan <[hidden email]> wrote:

> Thank you very much for your reply.
>
>
> By omitting the probability, the expected results could be:
>
>
> c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0)
>
>
> c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
>
>
> If I omit the probability, I would like to generate N random positive
> integers that sum to M and the integers would be selected from a uniform
> distribution.
>
>
> Many thanks for your time
>
> Nell
>
>
> ________________________________
> De : Rolf Turner <[hidden email]>
> Envoyé : mercredi 4 juillet 2018 16:11:11
> À : Nelly Reduan
> Cc : [hidden email]
> Objet : Re: [R] Generate N random numbers with a given probability and
> condition
>
>
> On 05/07/18 10:21, Nelly Reduan wrote:
>
> > Dear all,
> >
> > I would like to generate N random numbers with a given probability and
> condition but I'm not sure how to do this.
> > For example, I have N = 20 and the vector from which to choose is seq(0,
> 10, 1). I have tested:
> >
> > x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28,
> times=length(seq(0, 10, 1))))
> >
> > But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
> > Many thanks for your time.
>
> Your thinking requires considerable clarification.
>
> (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.
>
> (2) Hence length(seq(0,10,1)) is 11.
>
> (3) Likewise max(seq(0,10,1)) is 10.
>
> (4) Your prob vector is *constant* --- so specifying "prob" makes
>      no difference --- the result is the same as if you omitted "prob".
>
> (5) You need to think carefully about what you really mean by "random".
>      In what way do you want the final result to be "random"?
>
> I expect that the lecturer who assigned this problem to you  needs to
> clarify his/her thinking as well.
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: Generate N random numbers with a given probability and condition

Rolf Turner
In reply to this post by Jim Lemon-4
On 11/07/18 12:46, Jim Lemon wrote:

> Hi Nell,
> I may not have the right idea about this, but I think you need to do
> this in two steps if it can be done. Let's say you want a sequence of
> 20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate
> the monotonically increasing sequences like this:
>
> c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
> c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2)
> ...
> c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10)

<SNIP>

Jim:  You should *not* do people's homework for them!

cheers,

Rolf

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: Generate N random numbers with a given probability and condition

Boris Steipe
Wasn't there also the requirement that the numbers be drawn from a uniform distribution? These sequences are not. I wonder whether this can for all practical purposes be simplified to consider only the sequence with maximum entropy.

B.




> On 2018-07-11, at 06:23, Rolf Turner <[hidden email]> wrote:
>
> On 11/07/18 12:46, Jim Lemon wrote:
>> Hi Nell,
>> I may not have the right idea about this, but I think you need to do
>> this in two steps if it can be done. Let's say you want a sequence of
>> 20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate
>> the monotonically increasing sequences like this:
>> c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
>> c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2)
>> ...
>> c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10)
>
> <SNIP>
>
> Jim:  You should *not* do people's homework for them!
>
> cheers,
>
> Rolf
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Nelly Reduan
In reply to this post by Nelly Reduan
Many thanks Jim for your help. I am trying to apply the permutations with a sequence of 20 but I obtain the error message:


Error in matrix(NA, nrow = nrows, ncol = lenx) :
  invalid 'nrow' value (too large or NA)
In addition: Warning message:
In matrix(NA, nrow = nrows, ncol = lenx) :
  NAs introduced by coercion to integer range


Here is the code:

library(partitions)
library(crank)
r <- t(restrictedparts(10, 20))
r <- split(r, seq(nrow(r)))
rp <- crank::permute(r[[sample(1:length(r), 1)]])
rp[sample(1:dim(rp)[1],1),]


In this case, Is it correct to permute the elements of a vector rather than to permute a vector ?


Many thanks for your time.

Have a nice day

Nell


________________________________
De : Jim Lemon <[hidden email]>
Envoyé : mardi 10 juillet 2018 17:44:13
À : Nelly Reduan
Objet : Re: [R] Generate N random numbers with a given probability and condition

Hi Nell,
I may not have the right idea about this, but I think you need to do
this in two steps if it can be done. Let's say you want a sequence of
20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate
the monotonically increasing sequences like this:

c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2)
...
c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10)

So if you select one of these sequences at random, there will be a
further set of sequences that are permutations of it. By randomly
selecting one of those permutations, I think you can solve your
problem. However, this is going to be computationally intensive, with
the set of permutations being very large for large N. Here is an
example using N = M = 5:

# enumerate the sequences = M
rs5<-list(c(1,1,1,1,1),c(0,1,1,1,2),c(0,0,1,1,3),c(0,0,0,1,4),
 c(0,0,1,2,2),c(0,0,0,2,3),c(0,0,0,0,5))
library(crank)
# generate the permutations for one sequence (120 in this case)
rs5_s1<-permute(rs5[[sample(1:length(rs5),1)]])
# select one of the permutations at random
rs5_s1[sample(1:dim(rs5_s1)[1],1),]
[1] 4 0 1 0 0

Jim

On Wed, Jul 11, 2018 at 10:11 AM, Nelly Reduan <[hidden email]> wrote:

> Thank you very much for your reply.
>
>
> By omitting the probability, the expected results could be:
>
>
> c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0)
>
>
> c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
>
>
> If I omit the probability, I would like to generate N random positive integers that sum to M and the integers would be selected from a uniform distribution.
>
>
> Many thanks for your time
>
> Nell
>
>
> ________________________________
> De : Rolf Turner <[hidden email]>
> Envoyé : mercredi 4 juillet 2018 16:11:11
> À : Nelly Reduan
> Cc : [hidden email]
> Objet : Re: [R] Generate N random numbers with a given probability and condition
>
>
> On 05/07/18 10:21, Nelly Reduan wrote:
>
>> Dear all,
>>
>> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
>> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
>>
>> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
>>
>> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
>> Many thanks for your time.
>
> Your thinking requires considerable clarification.
>
> (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.
>
> (2) Hence length(seq(0,10,1)) is 11.
>
> (3) Likewise max(seq(0,10,1)) is 10.
>
> (4) Your prob vector is *constant* --- so specifying "prob" makes
>      no difference --- the result is the same as if you omitted "prob".
>
> (5) You need to think carefully about what you really mean by "random".
>      In what way do you want the final result to be "random"?
>
> I expect that the lecturer who assigned this problem to you  needs to
> clarify his/her thinking as well.
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Jim Lemon-4
Hi Nell,
As I said, the number of permutations increases rapidly with the
number of values to be permuted. Using the package "permute", which is
much more sophisticated than the basic function in the crank package,
a vector of length 20 has 2.432902e+18 possible permutations. While
your problem can be solved for small vectors by simply generating all
the permutations and then sampling that set, it is not a general
solution. You may be able to use the functions in the permute package
to handle a 20 element vector, but I am not familiar enough with the
functions to tell you how.

Jim


On Thu, Jul 12, 2018 at 3:14 AM, Nelly Reduan <[hidden email]> wrote:

> Many thanks Jim for your help. I am trying to apply the permutations with a
> sequence of 20 but I obtain the error message:
>
>
> Error in matrix(NA, nrow = nrows, ncol = lenx) :
>   invalid 'nrow' value (too large or NA)
> In addition: Warning message:
> In matrix(NA, nrow = nrows, ncol = lenx) :
>   NAs introduced by coercion to integer range
>
> Here is the code:
>
> library(partitions)
> library(crank)
> r <- t(restrictedparts(10, 20))
> r <- split(r, seq(nrow(r)))
> rp <- crank::permute(r[[sample(1:length(r), 1)]])
> rp[sample(1:dim(rp)[1],1),]
>
> In this case, Is it correct to permute the elements of a vector rather than
> to permute a vector ?
>
>
> Many thanks for your time.
>
> Have a nice day
>
> Nell
>
>
> ________________________________
> De : Jim Lemon <[hidden email]>
> Envoyé : mardi 10 juillet 2018 17:44:13
> À : Nelly Reduan
> Objet : Re: [R] Generate N random numbers with a given probability and
> condition
>
> Hi Nell,
> I may not have the right idea about this, but I think you need to do
> this in two steps if it can be done. Let's say you want a sequence of
> 20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate
> the monotonically increasing sequences like this:
>
> c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
> c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2)
> ...
> c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10)
>
> So if you select one of these sequences at random, there will be a
> further set of sequences that are permutations of it. By randomly
> selecting one of those permutations, I think you can solve your
> problem. However, this is going to be computationally intensive, with
> the set of permutations being very large for large N. Here is an
> example using N = M = 5:
>
> # enumerate the sequences = M
> rs5<-list(c(1,1,1,1,1),c(0,1,1,1,2),c(0,0,1,1,3),c(0,0,0,1,4),
>  c(0,0,1,2,2),c(0,0,0,2,3),c(0,0,0,0,5))
> library(crank)
> # generate the permutations for one sequence (120 in this case)
> rs5_s1<-permute(rs5[[sample(1:length(rs5),1)]])
> # select one of the permutations at random
> rs5_s1[sample(1:dim(rs5_s1)[1],1),]
> [1] 4 0 1 0 0
>
> Jim
>
> On Wed, Jul 11, 2018 at 10:11 AM, Nelly Reduan <[hidden email]> wrote:
>> Thank you very much for your reply.
>>
>>
>> By omitting the probability, the expected results could be:
>>
>>
>> c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0)
>>
>>
>> c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
>>
>>
>> If I omit the probability, I would like to generate N random positive
>> integers that sum to M and the integers would be selected from a uniform
>> distribution.
>>
>>
>> Many thanks for your time
>>
>> Nell
>>
>>
>> ________________________________
>> De : Rolf Turner <[hidden email]>
>> Envoyé : mercredi 4 juillet 2018 16:11:11
>> À : Nelly Reduan
>> Cc : [hidden email]
>> Objet : Re: [R] Generate N random numbers with a given probability and
>> condition
>>
>>
>> On 05/07/18 10:21, Nelly Reduan wrote:
>>
>>> Dear all,
>>>
>>> I would like to generate N random numbers with a given probability and
>>> condition but I'm not sure how to do this.
>>> For example, I have N = 20 and the vector from which to choose is seq(0,
>>> 10, 1). I have tested:
>>>
>>> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28,
>>> times=length(seq(0, 10, 1))))
>>>
>>> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
>>> Many thanks for your time.
>>
>> Your thinking requires considerable clarification.
>>
>> (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.
>>
>> (2) Hence length(seq(0,10,1)) is 11.
>>
>> (3) Likewise max(seq(0,10,1)) is 10.
>>
>> (4) Your prob vector is *constant* --- so specifying "prob" makes
>>      no difference --- the result is the same as if you omitted "prob".
>>
>> (5) You need to think carefully about what you really mean by "random".
>>      In what way do you want the final result to be "random"?
>>
>> I expect that the lecturer who assigned this problem to you  needs to
>> clarify his/her thinking as well.
>>
>> cheers,
>>
>> Rolf Turner
>>
>> --
>> Technical Editor ANZJS
>> Department of Statistics
>> University of Auckland
>> Phone: +64-9-373-7599 ext. 88276
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Duncan Murdoch-2
In reply to this post by Nelly Reduan
On 04/07/2018 6:21 PM, Nelly Reduan wrote:
> Dear all,
>
> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
>
> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
>
> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
> Many thanks for your time

I'd recommend an MCMC solution to this problem.  Set up a distribution
that is uniform on vectors that satisfy the conditions, with penalties
on vectors that don't.  Use the Metropolis algorithm with proposals that
pick a pair of entries and increase one, decrease the other, then let
MCMC run.  At the end, filter out the cases that violate the conditions.

The hard part is knowing how long to let it run for a satisfactory
sample, and how correlated later draws will be.  Propp and Wilson's
perfect sampling algorithm might allow an exact draw, though I don't
quite see how, and I'm not sure it would be worth the trouble.  Just run
for a few thousand steps and it should be fine.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

Göran Broström-3
In reply to this post by Nelly Reduan


On 2018-07-05 00:21, Nelly Reduan wrote:

> Dear all,
>
> I would like to generate N random numbers with a given probability and condition but I'm not sure how to do this.
> For example, I have N = 20 and the vector from which to choose is seq(0, 10, 1). I have tested:
>
> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28, times=length(seq(0, 10, 1))))
>
> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
> Many thanks for your time
> Nell

Maybe the paper "Acceptance–Rejection Sampling from the Conditional
Distribution of Independent Discrete Random Variables, given their Sum",
Statistics 34, pages 247-257, by Leif Nilsson and myself (2000) is relevant?

Göran

>
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Generate N random numbers with a given probability and condition

David Winsemius

> On Jul 12, 2018, at 12:44 AM, Göran Broström <[hidden email]> wrote:
>
> "Acceptance–Rejection Sampling from the Conditional Distribution of Independent Discrete Random Variables, given their Sum", Statistics 34, pages 247-257

Dear Go:ran;

I'm fully retired with no subscriber academic library that I can easily access. I've done a good faith search and can find no pdf's and the publisher website is apparently unable to deliver paid copies at this time. I wonder if you would be so kind as to send a preprint or other form of that article? I'm not so much interested in the the statistics as I am to see the two mentioned applications.

Best regards;

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.