Skip to content
Prev 273982 / 398506 Next

How to draw 4 random weights that sum up to 1?

On Oct 10, 2011, at 12:44 PM, Uwe Ligges wrote:

            
For the non-statisticians in the audience like myself who didn't know  
what that distribution might "look like" (it being difficult to  
visualize densities on your 3-dimensional manifold in 4-space),  here  
is my effort to get an appreciation:

  M4 <- matrix(runif(40000), ncol=4)
  M4 <- M4/rowSums(M4)
# just a larger realization of Ligges' advice
  colMeans(M4)
[1] 0.2503946 0.2499594 0.2492118 0.2504342
  plot(density(M4[,1]))
  lines(density(M4[,2]),col="red")
  lines(density(M4[,3]),col="blue")
  lines(density(M4[,4]),col="green")

plot(density(rowSums(M4[,1:2])))

  plot(density(rowSums(M4[,1:3])))
plot(density(rowSums(M4[,2:4])))

# rather kewl results, noting that these are a reflecion around 0.5 of  
the single vector densities.