-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistribution.py
58 lines (38 loc) · 1.05 KB
/
distribution.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
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
class Distribution(object):
@staticmethod
def factory(dist, rate):
assert dist in Distribution.distributions()
return globals()[dist.capitalize()](rate)
@staticmethod
def distributions():
return [cls.__name__.lower() for cls in Distribution.__subclasses__()]
class Uniform(Distribution):
def __init__(self, rate):
self.rate = rate
def next(self):
return 1.0 / self.rate
class Poisson(Distribution):
def __init__(self, rate):
self.rate = rate
def next(self):
return np.random.exponential(1 / self.rate)
class Pareto(Distribution):
def __init__(self, rate):
self.rate = rate
def next(self):
# We chose the constant 2, so that the mean is (1/rate)
return np.random.pareto(2) / self.rate
def test():
p = 0
u = 0
e = 0
rate = 100 # qps
uniform = Uniform(rate)
exp = Poisson(rate)
pareto = Pareto(rate)
for _ in range(100):
u = uniform.next()
e = exp.next()
p = pareto.next()
print("uniform: {:.2f} poisson: {:.2f} pareto: {:.2f}".format(u, e, p))