[R] Creating 3 vectors that sum to 1
Petr Savicky
savicky at praha1.ff.cuni.cz
Wed Mar 30 12:07:18 CEST 2011
On Tue, Mar 29, 2011 at 01:42:18PM -0600, Greg Snow wrote:
> Or we could expand a bit more:
>
> require(TeachingDemos)
> require(gtools)
>
> n <- 1000
> rtrg <- matrix(NA, n, 3)
> for (i in 1:n) rtrg[i,] <- diff(c(0, sort(runif(2)), 1))
>
> rtrg2 <- matrix(NA, n, 3)
> for (i in 1:n) {
> tmp <- runif(3)
> rtrg2[i, ] <- tmp/sum(tmp)
> }
>
> rtrg3 <- matrix( rexp(n*3), ncol=3 )
> rtrg3 <- rtrg3/rowSums(rtrg3)
>
> rtrg4 <- rdirichlet(n, rep(1,3))
If i understand correctly, this is an efficient way to generate the
uniform distribution over the triangle. Thank you for pointing this
out.
Generating the uniform distribution is a natural question related
to the original request.
> par(mfrow=c(2,2))
> triplot(rtrg, pch='.') # Looks more uniformly distributed
This distribution is also exactly uniform.
If x is generated as sort(runif(2)), then it is uniformly distributed
over the two dimensional triangle 0 <= x[1] <= x[2] <= 1.
The transformation, which maps x to c(x[1], x[2] - x[1], 1 - x[2])
is linear, so it preserves the uniform distribution and its output
ranges over the triangle with corners [1, 0, 0], [0, 1, 0], [0, 0, 1].
> triplot(rtrg2, col=2, pch='.') # Corners are sparsely populated
The ratio of the density in the center and in the corners seems
to be 27.
Consider a small area A on the triangle between the points (1, 0, 0),
(0, 1, 0), (0, 0, 1). The points x generated as runif(3), which are mapped
to A by the transformation x/sum(x), consist of two types of points.
(1) The cone (or a pyramid) between A and the point (0, 0, 0).
(2) Points on the extension of this cone away from (0, 0, 0),
which are still inside the cube [0, 1]^3.
The volume of (1) depends on the area of A, but not on its location
within the triangle, since the height of the cone is always the same.
If A is close to a corner, then (2) is small compared to (1) (the ratio
converges to 0, if A is limited to small neighborhood of the corner).
If A is close to the center of the triangle, then the union of (1) and (2)
contains a cone approximately 3 times larger than the cone (1). So,
the volume of (1) and (2) together is about 27 times larger than (1) alone.
(The ratio converges to 27, if A is limited to small neighborhood of the
center).
Petr Savicky.
More information about the R-help
mailing list