Generate 3 random number that sum to 1 in R

前端 未结 6 1162
花落未央
花落未央 2020-12-16 18:15

I am hoping to create 3 (non-negative) quasi-random numbers that sum to one, and repeat over and over.

Basically I am trying to partition something into three rand

6条回答
  •  借酒劲吻你
    2020-12-16 19:09

    This question involves subtler issues than might be at first apparent. After looking at the following, you may want to think carefully about the process that you are using these numbers to represent:

    ## My initial idea (and commenter Anders Gustafsson's):
    ## Sample 3 random numbers from [0,1], sum them, and normalize
    jobFun <- function(n) {
        m <- matrix(runif(3*n,0,1), ncol=3)
        m<- sweep(m, 1, rowSums(m), FUN="/")
        m
    }
    
    ## Andrie's solution. Sample 1 number from [0,1], then break upper 
    ## interval in two. (aka "Broken stick" distribution).
    andFun <- function(n){
      x1 <- runif(n)
      x2 <- runif(n)*(1-x1)
      matrix(c(x1, x2, 1-(x1+x2)), ncol=3)
    }
    
    ## ddzialak's solution (vectorized by me)
    ddzFun <- function(n) {
        a <- runif(n, 0, 1)
        b <- runif(n, 0, 1)
        rand1 = pmin(a, b)
        rand2 = abs(a - b)
        rand3 = 1 - pmax(a, b)
        cbind(rand1, rand2, rand3)
    }
    
    ## Simulate 10k triplets using each of the functions above
    JOB <- jobFun(10000)
    AND <- andFun(10000)
    DDZ <- ddzFun(10000)
    
    ## Plot the distributions of values
    par(mfcol=c(2,2))
    hist(JOB, main="JOB")
    hist(AND, main="AND")
    hist(DDZ, main="DDZ")
    

    enter image description here

提交回复
热议问题