File tree Expand file tree Collapse file tree 1 file changed +24
-2
lines changed
Expand file tree Collapse file tree 1 file changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -27,8 +27,30 @@ def getAvg(self):
2727 return s / 100
2828 def rand (self ):
2929 r = random .random () * 100
30+ return self .getValueFromPercentile (r )
31+ def getPercentileFromValue (self , x ):
32+ if x < 0 or x > self .cdf [- 1 ][0 ]:
33+ return - 1
3034 for i in range (1 , len (self .cdf )):
31- if r <= self .cdf [i ][1 ]:
35+ if x <= self .cdf [i ][0 ]:
36+ x0 , y0 = self .cdf [i - 1 ]
37+ x1 , y1 = self .cdf [i ]
38+ return y0 + (y1 - y0 )/ (x1 - x0 )* (x - x0 )
39+ def getValueFromPercentile (self , y ):
40+ for i in range (1 , len (self .cdf )):
41+ if y <= self .cdf [i ][1 ]:
3242 x0 ,y0 = self .cdf [i - 1 ]
3343 x1 ,y1 = self .cdf [i ]
34- return x0 + (x1 - x0 )/ (y1 - y0 )* (r - y0 )
44+ return x0 + (x1 - x0 )/ (y1 - y0 )* (y - y0 )
45+ def getIntegralY (self , y ):
46+ s = 0
47+ for i in range (1 , len (self .cdf )):
48+ x0 , y0 = self .cdf [i - 1 ]
49+ x1 , y1 = self .cdf [i ]
50+ if y <= self .cdf [i ][1 ]:
51+ s += 0.5 * (x0 + x0 + (x1 - x0 )/ (y1 - y0 )* (y - y0 ))* (y - y0 ) / 100.
52+ break
53+ else :
54+ s += 0.5 * (x1 + x0 ) * (y1 - y0 ) / 100.
55+
56+ return s
You can’t perform that action at this time.
0 commit comments