Hi, I am looking for a way to generate a matrix of random numbers in a way that each row of the matrix would sum to 1 and that the numbers in the columns of the matrix would have a uniform distribution. So far I have found several ways to create random numbers that would sum to 1, but then the distribution of the individual elements is more or less skewed - there are much more small numbers than larger ones. Any help and suggestions? - B?rbel -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html Sent from the R help mailing list archive at Nabble.com.
how to generate a set of random numbers that sum to 1 with uniform distribution of elements
7 messages · Bärbel, Albyn Jones, Heramb +2 more
What uniform distribution do you want for the columns? The average value of X_k given \sum X_i = 1 must be 1/n. If you start with X_i ~ U(0,1), the result must be skewed. If you generate X_i uniformly distributed on (0, 2/n), the conditional distribution given the sum is 1 will be less skewed. U <- matrix(runif(1000*100)/50,nrow=1000) s <- apply(U,1,sum) V <- U/s qqplot(U[,1],V[,1]) albyn
On Wed, Nov 07, 2012 at 05:02:10AM -0800, B?rbel wrote:
Hi, I am looking for a way to generate a matrix of random numbers in a way that each row of the matrix would sum to 1 and that the numbers in the columns of the matrix would have a uniform distribution. So far I have found several ways to create random numbers that would sum to 1, but then the distribution of the individual elements is more or less skewed - there are much more small numbers than larger ones. Any help and suggestions? - B?rbel -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Albyn Jones Reed College jones at reed.edu
Hi,
This is a humble try.
Matrix=function(n){Tab=cbind(runif(n,0,1))
for(i in 2:n)
{x=NULL
for(j in 1:n)
{x=c(x,runif(1,0,1-sum(Tab[j,])))}
Tab=cbind(Tab,x)
}
Table<<-Tab
}
n=1,2,3,4.....
Matrix(n)
1. The columns will follow uniform distribution with respective parameters.
2. The row sums will be approximately equal to '1'.
3. The discrepancy in 2nd Point can be removed if the number of columns are increased.
I hope this works
Best,
Heramb M. Gadgil
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of B?rbel
Sent: Wednesday, November 07, 2012 6:32 PM
To: r-help at r-project.org
Subject: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
Hi,
I am looking for a way to generate a matrix of random numbers in a way that
each row of the matrix would sum to 1 and that the numbers in the columns of
the matrix would have a uniform distribution. So far I have found several
ways to create random numbers that would sum to 1, but then the distribution
of the individual elements is more or less skewed - there are much more
small numbers than larger ones.
Any help and suggestions?
- B?rbel
--
View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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 is a modified code if it helps.
Matrix=function(n,simulations){Tab=cbind(runif(n,0,1))
for(i in 2:simulations)
{x=NULL
for(j in 1:n)
{x=c(x,runif(1,0,1-sum(Tab[j,])))}
Tab=cbind(Tab,x)
}
Table<<-Tab
}
Matrix(n,simulations)
#Matrix(5,100)
colnames(Table)=NULL
Best,
Heramb
-----Original Message-----
From: Heramb [mailto:heramb.gadgil at gmail.com]
Sent: Thursday, November 08, 2012 12:26 AM
To: 'B?rbel'; 'r-help at r-project.org'
Subject: RE: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
Hi,
This is a humble try.
Matrix=function(n){Tab=cbind(runif(n,0,1))
for(i in 2:n)
{x=NULL
for(j in 1:n)
{x=c(x,runif(1,0,1-sum(Tab[j,])))}
Tab=cbind(Tab,x)
}
Table<<-Tab
}
n=1,2,3,4.....
Matrix(n)
1. The columns will follow uniform distribution with respective parameters.
2. The row sums will be approximately equal to '1'.
3. The discrepancy in 2nd Point can be removed if the number of columns are increased.
I hope this works
Best,
Heramb M. Gadgil
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of B?rbel
Sent: Wednesday, November 07, 2012 6:32 PM
To: r-help at r-project.org
Subject: [R] how to generate a set of random numbers that sum to 1 with uniform distribution of elements
Hi,
I am looking for a way to generate a matrix of random numbers in a way that
each row of the matrix would sum to 1 and that the numbers in the columns of
the matrix would have a uniform distribution. So far I have found several
ways to create random numbers that would sum to 1, but then the distribution
of the individual elements is more or less skewed - there are much more
small numbers than larger ones.
Any help and suggestions?
- B?rbel
--
View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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.
On Nov 7, 2012, at 10:48 AM, Albyn Jones wrote:
What uniform distribution do you want for the columns? The average value of X_k given \sum X_i = 1 must be 1/n. If you start with X_i ~ U(0,1), the result must be skewed. If you generate X_i uniformly distributed on (0, 2/n), the conditional distribution given the sum is 1 will be less skewed. U <- matrix(runif(1000*100)/50,nrow=1000) s <- apply(U,1,sum) V <- U/s qqplot(U[,1],V[,1])
Couldn't one just do: mat <- matrix(runif(1000*10), ncol=10) # mean value per element = 0.5 mat2 <- mat/rowSums(mat) # mean value of rowSums= 5
David. > > albyn > > On Wed, Nov 07, 2012 at 05:02:10AM -0800, B?rbel wrote: >> Hi, >> I am looking for a way to generate a matrix of random numbers in a way that >> each row of the matrix would sum to 1 and that the numbers in the columns of >> the matrix would have a uniform distribution. So far I have found several >> ways to create random numbers that would sum to 1, but then the distribution >> of the individual elements is more or less skewed - there are much more >> small numbers than larger ones. >> Any help and suggestions? >> - B?rbel >> >> >> >> -- >> View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. > > -- > Albyn Jones > Reed College > jones at reed.edu > > ______________________________________________ > 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. David Winsemius, MD Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121107/b848ebde/attachment.pl>
Thanks a lot for all your input!! -- View this message in context: http://r.789695.n4.nabble.com/how-to-generate-a-set-of-random-numbers-that-sum-to-1-with-uniform-distribution-of-elements-tp4648695p4648973.html Sent from the R help mailing list archive at Nabble.com.