[R] generate random numbers that sum up to 1

sun as at hut.at
Thu Oct 12 12:12:11 CEST 2006


Thanks for the answers.

I am sorry if the question is too simple to make everyone thought it is a 
homework, but it is not. I am setting up a simulation with expected utility 
theory which use EU = sum(Pi*Ui) form and I have to randomly generate these 
Pis, I do not really know which distribution these Pis has to follow but 
just know they have to be equally random hence the question.

I thought  Dirichlet(1,1, ..., 1)  could do the trick for me.

Sun

----- Original Message ----- 
From: "Duncan Murdoch" <murdoch at stats.uwo.ca>
To: "Alberto Monteiro" <albmont at centroin.com.br>
Cc: <r-help at stat.math.ethz.ch>
Sent: Wednesday, October 11, 2006 11:39 PM
Subject: Re: [R] generate random numbers that sum up to 1


> On 10/11/2006 5:04 PM, Alberto Monteiro wrote:
>> I don't have the previous messages, but it seems to me that
>> the solutions didn't quite get the requirements of the problem.
>>
>> For example, "it's obvious that" for n = 2, the
>> random variables X1 and X2 should be X1 <- runif(1)
>> and X2 <- 1 - X1; while the solution X <- runif(2);
>> X1 <- X[1] / sum(X); X2 <- X[2] / sum(X) will give
>> different distributions, as shown in this test case:
>>
>> N <- 1000
>> M <- matrix(runif(2 * N), 2, N)
>> X <- M[1,] / (M[1,] + M[2,])
>> hist(X)
>>
>> "It's obvious that" for a generic n-th dimensional
>> set of uniform variables X1, ... X_n subject to the
>> restriction X1 + ... + X_n = 1, the solution is to
>> take a uniform distribution in the (n-1)-th dimensional
>> hyperpyramid generated by the above relation and the
>> restrictions that each X_i >= 0.
>>
>> For example, for n = 3, we should sample from the
>> equilateral triangle with vertices c(1,0,0), c(0,1,0)
>> and c(0,0,1).
>>
>> For n = 4, we should sample from the pyramid whose
>> vertices are c(1,0,0,0), c(0,1,0,0), c(0,0,1,0) and
>> c(0,0,0,1).
>>
>> I don't know if there is a simple formula to do this
>> sampling.
>
> That's exactly what the Dirichlet(1,1, ..., 1) distribution does.
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list