Skip to content
Julie edited this page Sep 27, 2019 · 15 revisions

Sample N weights summing up to 1

The Python script can be used as follows to create M sets each containing N random values that sum up to 1.0:

import numpy as np
import PieShareDistribution

# make sure we get for this example reproducible results
np.random.seed(seed=12345)

M = 3      # number of random number sets
N = 5      # number of random numbers that need to sum up to C

# matrix of random sets (# rows = M, # of columns = N) 
rand = PieShareDistribution(M, N, remainder=True)

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

rand[0,:]    # [ 1.45478257  0.18399595  0.13153194  0.25154563  0.9781439 ]   
rand[1,:]    # [ 0.18915093  0.21120041  0.4866893   0.07378246  0.03917689]
rand[2,:]    # [ 0.29212136  0.21071729  0.24744715  0.24005194  0.00966226]

They all sum up to 1.0:

np.sum(rand[:,:],axis=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.

np.random.seed(seed=12345)
M = 3    # number of random number sets
N = 5    # number of random numbers that need to sum up to 1.0
R = np.random.rand(M,N-1)
rand = PieShareDistribution(M, N, remainder=True, randomnumbers=rr)

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

rand[0,:]    # [ 1.45478257  0.18399595  0.13153194  0.25154563  0.9781439 ]   
rand[1,:]    # [ 0.18915093  0.21120041  0.4866893   0.07378246  0.03917689]
rand[2,:]    # [ 0.29212136  0.21071729  0.24744715  0.24005194  0.00966226]

Funded under IMPC project of Global Water Futures program.

Table of contents

Clone this wiki locally