-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchi_square.py
51 lines (43 loc) · 1.45 KB
/
chi_square.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
from math import sqrt
from collections import Counter
import lcg
import bbs
import ansi
import numpy as np
import matplotlib.pyplot as plt
def is_random(random_nums):
# Calculate the number of samples - n
n = len(random_nums)
r = 10 # no. of classes
# According to Sedgewick:
# This is valid if n is greater than about 10r
# if n <= 10 * r:
# return False
n_r = n / r # expected frquency
# PART A: Get frequency of randoms
ht = []
mn = min(random_nums)
mx = max(random_nums)
diff = (mx - mn) // r
while mn <= mx:
ht.append(len(list(x for x in random_nums if mn <= x <= mn + diff)))
mn = mn + diff + 1
print(ht)
# PART B: Calculate chi-square - this approach is in Sedgewick
chi_square = sum((v - n_r)**2 for v in ht) / (n_r * 1.0)
print(chi_square)
# PART C: According to Sedgewick:
# The statistic should be within 2(r)^1/2 of r
# This is valid if N is greater than about 10r
return abs(chi_square - r) <= 2 * sqrt(r)
if __name__ == '__main__':
n_terms_bbs = 1000
n_terms_lcg = 1000
n_terms_ansi = 1000
terms = bbs.test(n_terms=n_terms_bbs)[1] # terms - tuple
terms_bbs = [t_[0] for t_ in terms]
terms_lcg = lcg.test(2**12, 125, 1, 1, n_terms_lcg)[0]
terms_ansi = ansi.test(limit=n_terms_ansi)[0]
print("test_lcg: ", is_random(terms_lcg))
print("test_bbs: ", is_random(terms_bbs))
print("test_ansi: ", is_random(terms_ansi))