"""
Generate sample of values following a beta-pert or a uniform distribution
"""
# load python modules
import numpy as np
import scipy.special as sc
from scipy.interpolate import interp1d
import logging
# create local logger
log = logging.getLogger(__name__)
[docs]def computeParameters(a, b, c):
""" Compute alpha, beta and mu """
# computation of paramters
if a > b or b > c or a > c:
message = 'a:%.2f must be smaller than b: %.2f must be smaller than c: %.2f' % (a, b, c)
log.error(message)
raise ValueError(message)
mu = (a + 4*b + c) / 6.0
alpha = (4*b + c - 5*a) / (c - a)
beta = (5*c - a - 4*b) / (c - a)
return alpha, beta, mu
[docs]def computePert(a, b, c, x, alpha, beta):
""" Compute the CDF and PDF of the Pert distribution using scipy betainc function """
# Compute pert pdf for testing if the retrieved sample fits the desired distribution
PDF = (((x - a)**(alpha - 1)) * ((c - x)**(beta-1))) / \
(sc.beta(alpha, beta) * ((c - a)**(alpha + beta - 1)))
# compute regularized incomplete beta function for pert distribution using scipy
z = (x - a) / (c - a)
CDF = sc.betainc(alpha, beta, z)
# use scipy.interpolate to create a function that can be used to extract samples
CDFint = interp1d(CDF, x)
return PDF, CDF, CDFint
[docs]def getEmpiricalCDF(sample):
""" Derive empirical CDF using numpy histogram and cumsum """
binsNo = int(len(sample) * 0.25)
hist, binsEd = np.histogram(sample, bins=binsNo)
CDFEmp = np.cumsum(hist)
CDFEmpPlot = CDFEmp / CDFEmp[-1]
return CDFEmpPlot, binsEd[1:]
[docs]def getEmpiricalCDFNEW(sample):
""" Derive empirical CDF using sorted sample """
# sort sample
sampleSorted = np.sort(sample)
sampleSize = len(sample)
ECDF = np.zeros(sampleSize)
for m in range(sampleSize):
cumsum = 0
for l in range(sampleSize):
if sample[l] <= sampleSorted[m]:
cumsum = cumsum + 1
ECDF[m] = cumsum / sampleSize
return ECDF, sampleSorted