-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawNumberFromDist.py
46 lines (30 loc) · 1.02 KB
/
drawNumberFromDist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def drawNumberFromDist(distType,distParams):
if( distType == 'fixed' ):
return float(distParams)
elif( distType == 'uniform'):
return drawFromUniform(distParams)
elif( distType == 'gaussianTruncated'):
return drawFromGaussianTruncated(distParams)
def drawFromUniform(minMax):
from numpy import random
if(len(minMax)!=2):
print('Invalid parameters for uniform distribution')
return
else:
return float(minMax[0]) + random.rand()*(float(minMax[1])-float(minMax[0]))
def drawFromGaussianTruncated(muSigmaClip):
from numpy import random
muSigmaClip = muSigmaClip
mu = float(muSigmaClip[0])
sigma = float(muSigmaClip[1])
clip = float(muSigmaClip[2])
randNum = mu + random.standard_normal()*sigma
clipLowerThresh = mu - (sigma*clip)
clipUpperThresh = mu + (sigma*clip)
while( randNum < clipLowerThresh or randNum > clipUpperThresh ):
randNum = mu + random.standard_normal()*sigma
return randNum
if __name__ == "__main__":
a = drawNumberFromDist('gaussianTruncated',[10,1,2])
print str(a)