[R] generate random numbers that sum up to 1

Duncan Murdoch murdoch at stats.uwo.ca
Wed Oct 11 23:39:21 CEST 2006


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



More information about the R-help mailing list