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}}
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 <shubhak at ambaresearch.com> wrote:> From: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> > Subject: [R] Sum(Random Numbers)=100 > To: r-help at stat.math.ethz.ch > 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 at 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.
...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:m_olshansky at yahoo.com] Sent: Tuesday, July 08, 2008 12:09 PM To: r-help at stat.math.ethz.ch; 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 <shubhak at ambaresearch.com> wrote:> From: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> > Subject: [R] Sum(Random Numbers)=100 > To: r-help at stat.math.ethz.ch > 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 at 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}}
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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 2:54 AM An: m_olshansky at yahoo.com; r-help at stat.math.ethz.ch 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:m_olshansky at yahoo.com] Sent: Tuesday, July 08, 2008 12:09 PM To: r-help at stat.math.ethz.ch; 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 <shubhak at ambaresearch.com> wrote:> From: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> > Subject: [R] Sum(Random Numbers)=100 > To: r-help at stat.math.ethz.ch > 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 at 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 at 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.
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:m_olshansky at yahoo.com] > Sent: Tuesday, July 08, 2008 12:09 PM > To: r-help at stat.math.ethz.ch; 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 <shubhak at ambaresearch.com> > wrote: > >> From: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> >> Subject: [R] Sum(Random Numbers)=100 >> To: r-help at stat.math.ethz.ch >> 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 at 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 at 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 richard.pearson at postgrad.manchester.ac.uk 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
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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 1:59 AM An: r-help at stat.math.ethz.ch 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 at 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.
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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Shubha Vishwanath Karanth Gesendet: Tuesday, July 08, 2008 1:59 AM An: r-help at stat.math.ethz.ch 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 at 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.
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 patrick at burns-stat.com +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:m_olshansky at yahoo.com] > Sent: Tuesday, July 08, 2008 12:09 PM > To: r-help at stat.math.ethz.ch; 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 <shubhak at ambaresearch.com> > wrote: > > >> From: Shubha Vishwanath Karanth <shubhak at ambaresearch.com> >> Subject: [R] Sum(Random Numbers)=100 >> To: r-help at stat.math.ethz.ch >> 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 at 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 at 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. > > >
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