Re: [R] generate random numbers that sum up to 1

2006-10-16 Thread Grant Izmirlian
So,  Alberto, you didn't see my post?  If Y has d independent components that 
are gamma distributed with common rate and shapes A_1, A_2, ..., A_d, then X, 
given by the components of Y divided by their sum has distribution 
Dirichlet(A_1, A_2, ..., A_d).  If you want Uniform on the d-simplex, then use 
A_1 = A_2 = ... = A_d = 1 (just as Duncan said)


original message:

Duncan Murdoch's definition is _the_ only one that I know.  X is Uniform on A 
means  E phi(X) = \int_A phi(x)  dx / \int_A dx, so that the probability 
density is equal to 1/ \int_A dx everwhere on the set A.  

By the way, another way to simulate X ~ Dirichlet(A1, A2, ..., Ad) 
is to generate d independent gamma variables having equal rate parameter
(doesn't matter, so why not 1) and shape parameters  A1, A2, ..., Ad
Then the vector of components divided by their sum is the desired 
Dirichlet:


n - 10
d - 3  # for three numbers that add to one ( the unit simplex in R^3)
A - rep(1, 3)  # for uniform
X - matrix(0, n, d)
for (k in 1:3)  X[,k] - rgamma(n, shape=A[k], rate=1)
S - X %*% rep(1, d)
Y - X/S

Present example will simulate n independant 3 vectors, each having 
non-negative components summing to 1, and having a distribution
assigning equal mass to every possible value.

Changing d and the components of A will provide an arbitrary Dirichlet
on the unit simplex in R^d

Grant Izmirlian

NCI




 Duncan Murdoch wrote Another definition of uniform is to have equal
 density for all possible vectors; the Dirichlet distribution with
 parameters (1,1,1) would give you that. 

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-16 Thread Alberto Monteiro

Grant Izmirlian wrote:

 So,  Alberto, you didn't see my post?

I think I didn't - but you are demanding too much from my memory;
I can hardly remember what I saw yesterday!

 If Y has d independent 
 components that are gamma distributed with common rate and shapes 
 A_1, A_2, ..., A_d, then X, given by the components of Y divided by 
 their sum has distribution Dirichlet(A_1, A_2, ..., A_d).  If you 
 want Uniform on the d-simplex, then use A_1 = A_2 = ... = A_d = 1 
 (just as Duncan said)
 
The problem is that I wasn't aware that this was the Dirichlet
distribution [R does not have this distribution, AFAIK, but I
should have consulted the Borg of All Wisdom, the Wikipedia].

Alberto Monteiro

__
R-help@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.


[R] generate random numbers that sum up to 1

2006-10-16 Thread Alberto Monteiro
I found another method to generate this, in the Borg of all Wisdom:
http://en.wikipedia.org/wiki/Simplex

Alberto Monteiro

---

Section Random Sampling:

The second method [[the first method is based on the Dirichlet 
distribution -- avfm]] to generate a random point on the unit 
simplex is based on the order statistics of the uniform distribution 
on the unit interval, and was popularized by Horst Kraemer. The 
algorithm is as follows:

Set p0 = 0 and pK=1. 

Generate K-1 uniform random draws pi from the open interval (0,1). 

Sort into ascending order the K+1 points p0, ..., pK. 

The K coordinates t1, ..., tK of the final point on the unit 
simplex are given by ti=pi-pi-1. 

It has been pointed out by Smith and Tromble that the second 
method is technically only valid if none of the differences 
pi-p(i-1) are equal to zero. In practice, it is sufficient 
to merely re-run the algorithm to generate a new set of 
points if this happens.

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-12 Thread sun
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 [EMAIL PROTECTED]
To: Alberto Monteiro [EMAIL PROTECTED]
Cc: r-help@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@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.

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-11 Thread Grant Izmirlian
Duncan Murdoch's definition is _the_ only one that I know.  X is Uniform on A 
means  E phi(X) = \int_A phi(x)  dx / \int_A dx, so that the probability 
density is equal to 1/ \int_A dx everwhere on the set A.  

By the way, another way to simulate X ~ Dirichlet(A1, A2, ..., Ad) 
is to generate d independent gamma variables having equal rate parameter
(doesn't matter, so why not 1) and shape parameters  A1, A2, ..., Ad
Then the vector of components divided by their sum is the desired 
Dirichlet:


n - 10
d - 3  # for three numbers that add to one ( the unit simplex in R^3)
A - rep(1, 3)  # for uniform
X - matrix(0, n, d)
for (k in 1:3)  X[,k] - rgamma(n, shape=A[k], rate=1)
S - X %*% rep(1, d)
Y - X/S

Present example will simulate n independant 3 vectors, each having 
non-negative components summing to 1, and having a distribution
assigning equal mass to every possible value.

Changing d and the components of A will provide an arbitrary Dirichlet
on the unit simplex in R^d

Grant Izmirlian

NCI




 Duncan Murdoch wrote Another definition of uniform is to have equal
 density for all possible vectors; the Dirichlet distribution with
 parameters (1,1,1) would give you that. 

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-11 Thread Alberto Monteiro
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.

Alberto Monteiro

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-11 Thread Duncan Murdoch
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@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.


[R] generate random numbers that sum up to 1

2006-10-10 Thread sun
I am trying to generate a vector of random numbers with the constraint that 
they have to sum up to one with uniform distribution.

eg. {0.1,0.7,0.2 }

any function to do this? Thanks.

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-10 Thread Rolf Turner

As I have previously asked, in response to a similar
question:  Is this a homework problem?

cheers,

Rolf Turner
[EMAIL PROTECTED]

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-10 Thread Peter Dalgaard
Rolf Turner [EMAIL PROTECTED] writes:

 As I have previously asked, in response to a similar
 question:  Is this a homework problem?

If so, he has a pretty nasty teacher...

-- 
   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@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.


Re: [R] generate random numbers that sum up to 1

2006-10-10 Thread Peter Dalgaard
sun [EMAIL PROTECTED] writes:

 I am trying to generate a vector of random numbers with the constraint that 
 they have to sum up to one with uniform distribution.
 
 eg. {0.1,0.7,0.2 }
 
 any function to do this? Thanks.

Depending on what you mean by uniform, this may be a solution

 x-runif(3)
 x/sum(x)
[1] 0.1130642 0.4098608 0.4770750

What you can't have is the individual components uniform and
identically distributed because the sum of the means would be 1.5 and
equal to the mean of the sum which is 1...

-- 
   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@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.


Re: [R] generate random numbers that sum up to 1

2006-10-10 Thread Ben Fairbank
 
The trick is in defining random in the face of the three apparently
incompatible constraints that the numbers be random, that they sum to 1,
and that they be uniformly distributed.  Tell us more.  Perhaps extend
Peter D's solution by first deciding (at random) how many components
each solution will have.

Ben Fairbank

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of sun
Sent: Tuesday, October 10, 2006 8:28 AM
To: r-help@stat.math.ethz.ch
Subject: [R] generate random numbers that sum up to 1

I am trying to generate a vector of random numbers with the constraint
that they have to sum up to one with uniform distribution.

eg. {0.1,0.7,0.2 }

any function to do this? Thanks.

__
R-help@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.

__
R-help@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.


Re: [R] generate random numbers that sum up to 1

2006-10-10 Thread Duncan Murdoch
On 10/10/2006 9:53 AM, Peter Dalgaard wrote:
 sun [EMAIL PROTECTED] writes:
 
 I am trying to generate a vector of random numbers with the constraint that 
 they have to sum up to one with uniform distribution.
 
 eg. {0.1,0.7,0.2 }
 
 any function to do this? Thanks.
 
 Depending on what you mean by uniform, this may be a solution
 
 x-runif(3)
 x/sum(x)
 [1] 0.1130642 0.4098608 0.4770750
 
 What you can't have is the individual components uniform and
 identically distributed because the sum of the means would be 1.5 and
 equal to the mean of the sum which is 1...
 

Another definition of uniform is to have equal density for all possible 
vectors; the Dirichlet distribution with parameters (1,1,1) would give 
you that.  RSiteSearch finds at least a couple of packages (VGAM, 
MCMCpack) that provide simulations from the Dirichlet.

Duncan Murdoch

__
R-help@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.