[R] Sum(Random Numbers)=100
Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
If they are really random you can not expect their sum to be 100. However, it is not difficult to get that given that the sum of n independent Poisson random variables equals N, any individual one has the conditional binomial distribution with size = N and p = 1/n, i.e. P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = k1 then the sum of the rest 49 must equal 100 - k1, so now you generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. Why do you need this? --- On Tue, 8/7/08, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> > Subject: [R] Sum(Random Numbers)=100 > To: [EMAIL PROTECTED] > Received: Tuesday, 8 July, 2008, 3:58 PM > Hi R, > > > > I need to generate 50 random numbers (preferably poisson), > such that > their sum is equal to 100. How do I do this? > > > > > > Thank you, > > Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > __ > R-help@r-project.org mailing list > 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
...actually I need to allocate certain amount of money (here I mentioned it as 100) to a randomly selected stocks(50 stocks)... i.e., 100 being divided among 50 stocks and preferably all are integer allocations(i.e., 5 8 56 12 etc without any decimals)... Thank you, Shubha -Original Message- From: Moshe Olshansky [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2008 12:09 PM To: [EMAIL PROTECTED]; Shubha Vishwanath Karanth Subject: Re: [R] Sum(Random Numbers)=100 If they are really random you can not expect their sum to be 100. However, it is not difficult to get that given that the sum of n independent Poisson random variables equals N, any individual one has the conditional binomial distribution with size = N and p = 1/n, i.e. P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = k1 then the sum of the rest 49 must equal 100 - k1, so now you generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. Why do you need this? --- On Tue, 8/7/08, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> > Subject: [R] Sum(Random Numbers)=100 > To: [EMAIL PROTECTED] > Received: Tuesday, 8 July, 2008, 3:58 PM > Hi R, > > > > I need to generate 50 random numbers (preferably poisson), > such that > their sum is equal to 100. How do I do this? > > > > > > Thank you, > > Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > __ > R-help@r-project.org mailing list > 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
Hi, I am afraid there is no other way except using brute force, that is , loop until their sum reaches your expectation. it is easy to figure out this probability by letting their sum to be a new random variable Z and Z = X_1 + \ldots + X_n where X_i ~ Poisson({\lambda}_i) . By calculating their moment generate function we can find the pmf of Z which is a new Poisson random variable with the parameter \sum_{i}{{\lambda}_i}. and Moshe Olshansky's method is also correct except it is based on the conditioning. On 2008-7-8, at 下午1:58, Shubha Vishwanath Karanth wrote: On 2008-7-8, at 下午2:39, Moshe Olshansky wrote: If they are really random you can not expect their sum to be 100. However, it is not difficult to get that given that the sum of n independent Poisson random variables equals N, any individual one has the conditional binomial distribution with size = N and p = 1/n, i.e. P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = k1 then the sum of the rest 49 must equal 100 - k1, so now you generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. Why do you need this? --- On Tue, 8/7/08, Shubha Vishwanath Karanth <[EMAIL PROTECTED] > wrote: From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> Subject: [R] Sum(Random Numbers)=100 To: [EMAIL PROTECTED] Received: Tuesday, 8 July, 2008, 3:58 PM Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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. --- Peng Jiang 江鹏 ,Ph.D. Candidate Antai College of Economics & Management 安泰经济管理学院 Department of Mathematics 数学系 Shanghai Jiaotong University (Minhang Campus) 800 Dongchuan Road 200240 Shanghai P. R. China __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
For arbitrary lambda_i it can take years until the sum of 50 such random variables is 100! But if one makes lambda_i = 2, then the probability that the sum of 50 of them equals 100 is about 1/sqrt(2*pi*100), so on average that sequence of 50 numbers must be generated about sqrt(2*pi*100)) ~ 25 times, which is very reasonable. --- On Tue, 8/7/08, Peng Jiang <[EMAIL PROTECTED]> wrote: > From: Peng Jiang <[EMAIL PROTECTED]> > Subject: Re: [R] Sum(Random Numbers)=100 > To: [EMAIL PROTECTED] > Cc: [EMAIL PROTECTED], "Shubha Vishwanath Karanth" <[EMAIL PROTECTED]> > Received: Tuesday, 8 July, 2008, 4:56 PM > Hi, >I am afraid there is no other way except using brute > force, that > is , loop until their sum reaches your expectation. > it is easy to figure out this probability by letting > their sum to be > a new random variable Z and Z = X_1 + \ldots + X_n > where X_i ~ Poisson({\lambda}_i) . By calculating > their moment > generate function we can find the pmf of Z which is > a new Poisson random variable with the parameter > \sum_{i}{{\lambda}_i}. > > and Moshe Olshansky's method is also correct except > it is based on > the conditioning. > On 2008-7-8, at 下午1:58, Shubha Vishwanath Karanth > wrote: > On 2008-7-8, at 下午2:39, Moshe Olshansky wrote: > > > If they are really random you can not expect their sum > to be 100. > > However, it is not difficult to get that given that > the sum of n > > independent Poisson random variables equals N, any > individual one > > has the conditional binomial distribution with size = > N and p = 1/n, > > i.e. > > P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). > > So you can generate X1 binomial with size = 100 and p > = 1/50; if X1 > > = k1 then the sum of the rest 49 must equal 100 - k1, > so now you > > generate X2 binomial with size = 100-k1 and p = 1/49; > if X2 = k2 > > then generate X3 binomial with size = 100 -(k1+k2) and > p = 1/48, etc. > > > > Why do you need this? > > > > > > --- On Tue, 8/7/08, Shubha Vishwanath Karanth > <[EMAIL PROTECTED] > > > wrote: > > > >> From: Shubha Vishwanath Karanth > <[EMAIL PROTECTED]> > >> Subject: [R] Sum(Random Numbers)=100 > >> To: [EMAIL PROTECTED] > >> Received: Tuesday, 8 July, 2008, 3:58 PM > >> Hi R, > >> > >> > >> > >> I need to generate 50 random numbers (preferably > poisson), > >> such that > >> their sum is equal to 100. How do I do this? > >> > >> > >> > >> > >> > >> Thank you, > >> > >> Shubha > >> > >> > >> > >> This e-mail may contain confidential and/or > privileged > >> i...{{dropped:13}} > >> > >> __ > >> R-help@r-project.org mailing list > >> 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. > > > > __ > > R-help@r-project.org mailing list > > 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. > > --- > Peng Jiang 江鹏 ,Ph.D. Candidate > Antai College of Economics & Management > 安泰经济管理学院 > Department of Mathematics > 数学系 > Shanghai Jiaotong University (Minhang Campus) > 800 Dongchuan Road > 200240 Shanghai > P. R. China __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
what you could (what I suggest) is not technically clean because your draw is not strictly random any more. But if you want to distribute 100 on 50 units with a Poisson-distributed variable x, then your lambda must be 100/50=2. You could then sample Poisson distribution ( rpois(50,2) ), sum over all 50 values and stop as soon as the sum is exactly 100. This may take a while though. And again, this is not strictly random as you are discarding lots of distributions to achieve that. Cheers, Daniel - cuncta stricte discussurus - -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 2:54 AM An: [EMAIL PROTECTED]; [EMAIL PROTECTED] Betreff: Re: [R] Sum(Random Numbers)=100 ...actually I need to allocate certain amount of money (here I mentioned it as 100) to a randomly selected stocks(50 stocks)... i.e., 100 being divided among 50 stocks and preferably all are integer allocations(i.e., 5 8 56 12 etc without any decimals)... Thank you, Shubha -Original Message- From: Moshe Olshansky [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2008 12:09 PM To: [EMAIL PROTECTED]; Shubha Vishwanath Karanth Subject: Re: [R] Sum(Random Numbers)=100 If they are really random you can not expect their sum to be 100. However, it is not difficult to get that given that the sum of n independent Poisson random variables equals N, any individual one has the conditional binomial distribution with size = N and p = 1/n, i.e. P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = k1 then the sum of the rest 49 must equal 100 - k1, so now you generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. Why do you need this? --- On Tue, 8/7/08, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> > Subject: [R] Sum(Random Numbers)=100 > To: [EMAIL PROTECTED] > Received: Tuesday, 8 July, 2008, 3:58 PM Hi R, > > > > I need to generate 50 random numbers (preferably poisson), such that > their sum is equal to 100. How do I do this? > > > > > > Thank you, > > Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > __ > R-help@r-project.org mailing list > 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
sum.x=sum(x) while(sum.x!=100) x=rpois(50,2) I have tried this, but this can really take some time. In fact, I would suggest an similarly "fraudulent" activity, draw 49 values, and set the last nonrandomly to the difference between 100 and the sum of the 49 values if this difference is a.) nonnegative and b.) not too extreme (say more than three lambdas out). This is "as random" as the previous approach, maybe even more. Best, Daniel - cuncta stricte discussurus - -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 1:59 AM An: [EMAIL PROTECTED] Betreff: [R] Sum(Random Numbers)=100 Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
For some reason, the while-loop I sent did not work. It was running forever although I don't know why. Anyway, using repeat works fast: repeat{x=rpois(50,2) if(sum(x)==100) break } - cuncta stricte discussurus - -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 1:59 AM An: [EMAIL PROTECTED] Betreff: [R] Sum(Random Numbers)=100 Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
Shubha Does table(ceiling(runif(100,0,50))) give you something like you want? Richard. Shubha Vishwanath Karanth wrote: ...actually I need to allocate certain amount of money (here I mentioned it as 100) to a randomly selected stocks(50 stocks)... i.e., 100 being divided among 50 stocks and preferably all are integer allocations(i.e., 5 8 56 12 etc without any decimals)... Thank you, Shubha -Original Message- From: Moshe Olshansky [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2008 12:09 PM To: [EMAIL PROTECTED]; Shubha Vishwanath Karanth Subject: Re: [R] Sum(Random Numbers)=100 If they are really random you can not expect their sum to be 100. However, it is not difficult to get that given that the sum of n independent Poisson random variables equals N, any individual one has the conditional binomial distribution with size = N and p = 1/n, i.e. P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = k1 then the sum of the rest 49 must equal 100 - k1, so now you generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. Why do you need this? --- On Tue, 8/7/08, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> Subject: [R] Sum(Random Numbers)=100 To: [EMAIL PROTECTED] Received: Tuesday, 8 July, 2008, 3:58 PM Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ R-help@r-project.org mailing list 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. -- Richard D. Pearson [EMAIL PROTECTED] School of Computer Science,http://www.cs.man.ac.uk/~pearsonr University of Manchester, Tel: +44 161 275 6178 Oxford Road, Mob: +44 7971 221181 Manchester M13 9PL, UK.Fax: +44 161 275 6204 __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
> x <- rmultinom(1, 100, rep(1/50, 50)) > as.vector(x) [1] 4 0 5 4 2 2 4 4 3 2 4 1 1 1 2 0 0 0 2 1 0 4 3 3 2 4 2 2 2 0 1 1 4 2 2 2 0 1 1 1 2 3 2 2 4 1 [47] 3 1 3 0 > sum(x) [1] 100 Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:[EMAIL PROTECTED] http://www.cmis.csiro.au/bill.venables/ -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Daniel Malter Sent: Tuesday, 8 July 2008 5:32 PM To: 'Shubha Vishwanath Karanth'; [EMAIL PROTECTED] Subject: Re: [R] Sum(Random Numbers)=100 For some reason, the while-loop I sent did not work. It was running forever although I don't know why. Anyway, using repeat works fast: repeat{x=rpois(50,2) if(sum(x)==100) break } - cuncta stricte discussurus - -Ursprüngliche Nachricht- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 1:59 AM An: [EMAIL PROTECTED] Betreff: [R] Sum(Random Numbers)=100 Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
Richard Pearson wrote: Shubha Does table(ceiling(runif(100,0,50))) give you something like you want? That's a neat(-ish) solution of Moshe's multinomial formulation. More generally, if p is a vector of probabilities you can simulate independent indicators with cut(runif(N), c(0,cumsum(p)) and then multinomials by tabulation. However, it is inefficient for large N, and awkward if you want replicates, which is why we have rmultinom(). In the present case use rmultinom(1, 100, rep(1/50,50)) -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
So really you want random portfolios. While your specification may satisfy the demands of your current application, in general generating random portfolios is more complicated. There can be a large number of constraints required to make the portfolios conform to a realistic situation. Two example constraints are that each position should have no more than 5% weight, and the volatility should be no more than 15%. More on random portfolios can be found at http://www.burns-stat.com/pages/Finance/random_portfolios.html Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Shubha Vishwanath Karanth wrote: ...actually I need to allocate certain amount of money (here I mentioned it as 100) to a randomly selected stocks(50 stocks)... i.e., 100 being divided among 50 stocks and preferably all are integer allocations(i.e., 5 8 56 12 etc without any decimals)... Thank you, Shubha -Original Message- From: Moshe Olshansky [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2008 12:09 PM To: [EMAIL PROTECTED]; Shubha Vishwanath Karanth Subject: Re: [R] Sum(Random Numbers)=100 If they are really random you can not expect their sum to be 100. However, it is not difficult to get that given that the sum of n independent Poisson random variables equals N, any individual one has the conditional binomial distribution with size = N and p = 1/n, i.e. P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = k1 then the sum of the rest 49 must equal 100 - k1, so now you generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. Why do you need this? --- On Tue, 8/7/08, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> Subject: [R] Sum(Random Numbers)=100 To: [EMAIL PROTECTED] Received: Tuesday, 8 July, 2008, 3:58 PM Hi R, I need to generate 50 random numbers (preferably poisson), such that their sum is equal to 100. How do I do this? Thank you, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}} __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
Hi Peng, Does this help? sum(diff(c(0,sort(sample(seq(1,99,1),50,replace=T)),100))) Regards Alex -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peng Jiang Sent: July 8, 2008 3:57 AM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Subject: Re: [R] Sum(Random Numbers)=100 Hi, I am afraid there is no other way except using brute force, that is , loop until their sum reaches your expectation. it is easy to figure out this probability by letting their sum to be a new random variable Z and Z = X_1 + \ldots + X_n where X_i ~ Poisson({\lambda}_i) . By calculating their moment generate function we can find the pmf of Z which is a new Poisson random variable with the parameter \sum_{i}{{\lambda}_i}. and Moshe Olshansky's method is also correct except it is based on the conditioning. On 2008-7-8, at 下午1:58, Shubha Vishwanath Karanth wrote: On 2008-7-8, at 下午2:39, Moshe Olshansky wrote: > If they are really random you can not expect their sum to be 100. > However, it is not difficult to get that given that the sum of n > independent Poisson random variables equals N, any individual one has > the conditional binomial distribution with size = N and p = 1/n, i.e. > P(Xi=k/Sn=N) = (N over k)*(1/n)^k*((n-1)/n)^(N-k). > So you can generate X1 binomial with size = 100 and p = 1/50; if X1 = > k1 then the sum of the rest 49 must equal 100 - k1, so now you > generate X2 binomial with size = 100-k1 and p = 1/49; if X2 = k2 then > generate X3 binomial with size = 100 -(k1+k2) and p = 1/48, etc. > > Why do you need this? > > > --- On Tue, 8/7/08, Shubha Vishwanath Karanth > <[EMAIL PROTECTED] > > wrote: > >> From: Shubha Vishwanath Karanth <[EMAIL PROTECTED]> >> Subject: [R] Sum(Random Numbers)=100 >> To: [EMAIL PROTECTED] >> Received: Tuesday, 8 July, 2008, 3:58 PM Hi R, >> >> >> >> I need to generate 50 random numbers (preferably poisson), >> such that >> their sum is equal to 100. How do I do this? >> >> >> >> >> >> Thank you, >> >> Shubha >> >> >> >> This e-mail may contain confidential and/or privileged >> i...{{dropped:13}} >> >> __ >> R-help@r-project.org mailing list >> 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. > > __ > R-help@r-project.org mailing list > 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. --- Peng Jiang 江鹏 ,Ph.D. Candidate Antai College of Economics & Management 安泰经济管理学院 Department of Mathematics 数学系 Shanghai Jiaotong University (Minhang Campus) 800 Dongchuan Road 200240 Shanghai P. R. China __ R-help@r-project.org mailing list 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. __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
On Tue, Jul 8, 2008 at 9:53 AM, Shubha Vishwanath Karanth < [EMAIL PROTECTED]> wrote: > ...actually I need to allocate certain amount of money (here I mentioned > it as 100) to a randomly selected stocks(50 stocks)... i.e., 100 being > divided among 50 stocks and preferably all are integer allocations(i.e., > 5 8 56 12 etc without any decimals)... so perhaps you can reformulate your problem: instead of generating random numbers with their sum constrained to be 100, it could be dividing 100 units randomly between 50 "bins", which is, essentially, random sampling with replacement stocks <- 1:50 money <- 100 allocations <- sample(stocks, money, replace=TRUE) # here you can add a prob argument to sample() # especially if you expect the results to be something like " 5 8 56 12 etc " allocations <- table(factor(allocations, levels=stocks)) # or, equivalently, colSums(outer(allocations, stocks, "==")) I don't know if this solves your problem but at least it's guaranteed to sum to 100 and give you only integer values. Kenn [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.
Re: [R] Sum(Random Numbers)=100
quote: In the present case use rmultinom(1, 100, rep(1/50,50)) At least in my version, rmultinom normalizes the last argument, so rep(1,50) works just as well. Back to the original problem: either assign 49 randoms and give the remainder to the 50th slot, or do the right thing :-) : You have 50 outcomes, so simply draw numbers from the uniform distribution over the range 1:50. Keep drawing until you've assigned a number to each of your "dollars" (i.e. each item in your sample set). Carl __ R-help@r-project.org mailing list 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.