1
|
from sample_slottables import *
|
2
|
import haizea.common.stats as stats
|
3
|
import math
|
4
|
|
5
|
|
6
|
SEEDS = [660756695, 1080106124, 441535308, 1531785557, 3449773776, 2239192905, 1944782933, 377958281,
|
7
|
1698866825, 4281919021, 2985069635, 1929791444, 2054454583, 3428593444, 3259033264, 643731936,
|
8
|
2921350595, 1575932719, 2236362645, 1020609972, 1592461297, 1460695161, 1636954632, 76307538,
|
9
|
862656448, 2450493480, 247968499, 766348682, 3084561872, 1179378301, 1391629128, 1038658793,
|
10
|
3582609773, 392253809, 2732213167, 3688908610, 866221636, 1817396766, 3402959080, 2653694808,
|
11
|
1596091165, 188549655, 1900651916, 1577002145, 3060320535, 1268074655, 1752021485, 2783937267,
|
12
|
3482472935, 1513342535, 1655096731, 2485501475, 3972059090, 822958367, 4172029370, 3057570066,
|
13
|
1599256642, 2858736230, 1414979451, 303997155, 3247160141, 1629523852, 2258358509, 2132879613]
|
14
|
|
15
|
PAIRS = [(93.0, 93.0), (245.0, 178.0), (32.0, 234.0), (9.0, 151.0)]
|
16
|
|
17
|
NUM_VALUES = 10000
|
18
|
|
19
|
def mean(l):
|
20
|
return sum(l, 0.0) / len(l)
|
21
|
|
22
|
def stdev(l, m):
|
23
|
return math.sqrt(sum([(x - m)**2 for x in l], 0.0) / len(l))
|
24
|
|
25
|
class TestStats(object):
|
26
|
def __init__(self):
|
27
|
pass
|
28
|
|
29
|
def do_test(self, distribution, expected_mean, expected_stdev, error):
|
30
|
for seed in SEEDS:
|
31
|
distribution.seed(seed)
|
32
|
l = distribution.get_list(NUM_VALUES)
|
33
|
m = mean(l)
|
34
|
diff = abs(expected_mean - mean(l))
|
35
|
assert(diff < error)
|
36
|
s = stdev(l, m)
|
37
|
if expected_stdev != None:
|
38
|
diff = abs(expected_stdev - stdev(l, m))
|
39
|
assert(diff < error)
|
40
|
|
41
|
def test_uniform(self):
|
42
|
for a, length in PAIRS:
|
43
|
b = a + length
|
44
|
dist = stats.UniformDistribution(a, b)
|
45
|
expected_mean = (a+b)/2
|
46
|
expected_stdev = math.sqrt((b-a)**2 / 12)
|
47
|
error = length * 0.01
|
48
|
self.do_test(dist, expected_mean, expected_stdev, error)
|
49
|
|
50
|
def test_normal(self):
|
51
|
for mu, sigma in PAIRS:
|
52
|
print mu,sigma
|
53
|
dist = stats.NormalDistribution(mu, sigma)
|
54
|
error = (sigma / math.sqrt(NUM_VALUES)) * 3
|
55
|
self.do_test(dist, mu, sigma, error)
|
56
|
|
57
|
def test_pareto(self):
|
58
|
for l, length in PAIRS:
|
59
|
h = l + length
|
60
|
for a in [2.01, 2.5, 3.0, 3.5]:
|
61
|
dist = stats.BoundedParetoDistribution(l, h, a)
|
62
|
expected_mean = (l**a/(1-(l/h)**a)) * (a/(a-1)) * ((1/l**(a-1)) - (1/h**(a-1)))
|
63
|
expected_stdev = None
|
64
|
error = length * 0.01
|
65
|
self.do_test(dist, expected_mean, expected_stdev, error)
|