Skip to content
Julie edited this page Oct 2, 2019 · 6 revisions

Sample N weights summing up to 1

The following R commands can be used to create M sets each containing N random values that sum up to 1.0:

source("PieShareDistribution.R")
set.seed(12345)
M <- 3      # number of random number sets
N <- 5      # number of random numbers that need to sum up to 1.0
w <- PieShareDistribution(N,M)

The matrix contains M=3 sets each containing N=5 values:

> w
          [,1]       [,2]       [,3]       [,4]        [,5]
[1,] 0.2731606 0.37453736 0.06287683 0.28645480 0.002970389
[2,] 0.4063181 0.10918382 0.14508110 0.01172191 0.327695089
[3,] 0.3007902 0.04115058 0.31467161 0.05232317 0.291064429

They all sum up to 1.0:

> rowSums (w, na.rm = FALSE, dims = 1)
[1] 1 1 1

Sample N weights providing (N-1) uniformly sampled random numbers

For most applications such as sensitivity analysis, however, the method is providing the user with random numbers distributed between 0 and 1 that need to be used to perform the analysis.

Hence, the algorithm allows you to provide a matrix of random numbers R (M rows and N-1 columns) that will be transformed into N weights that will sum up to 1.0.

source("PieShareDistribution.R")
set.seed(12345)
M <- 3      # number of random number sets
N <- 5      # number of random numbers that need to sum up to 1.0
R <- runif((N-1)*M,0,1)
dim(R) <- c(M,N-1); #convert to matrix
w <- PieShareDistribution(N,M,randomnumbers=R)

The matrix contains M=3 sets each containing N=5 values:

> w
          [,1]       [,2]       [,3]       [,4]        [,5]
[1,] 0.2731606 0.37453736 0.06287683 0.28645480 0.002970389
[2,] 0.4063181 0.10918382 0.14508110 0.01172191 0.327695089
[3,] 0.3007902 0.04115058 0.31467161 0.05232317 0.291064429

> rowSums (w, na.rm = FALSE, dims = 1)
[1] 1 1 1   

Funded under IMPC project of Global Water Futures program.

Table of contents

Clone this wiki locally