-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomness_tests.py
More file actions
210 lines (193 loc) · 6.43 KB
/
Randomness_tests.py
File metadata and controls
210 lines (193 loc) · 6.43 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import math
from scipy import stats
import itertools
significance = 0.05
significance2 = 0.01
def stringify(l):
a = ""
for c in l:
a += str(c)
return a
def to_int(b):
total = 0
i = 0
for bit in reversed(b):
total += bit*(2**i)
i+=1
return total
def tobin(seq):
a = []
for b in seq:
t = format(b, "b")
for c in t:
a.append(int(c))
print(len(a))
return a
def serial(seq):
if len(seq) < 21:
return "error, sequence too small."
n1 = seq.count(1)
n0 = seq.count(0)
n00 = 0
n01 = 0
n10 = 0
n11 = 0
for i in range(len(seq)-1):
subseq = str(seq[i])+str(seq[i+1])
if (subseq == "00"):
n00 += 1
elif(subseq == "01"):
n01 += 1
elif(subseq == "10"):
n10 += 1
elif(subseq == "11"):
n11 += 1
statistic = ((4/(len(seq)-1.0))*((n00**2)+(n01**2)+(n10**2)+(n11**2)))-((2*((n0**2)+(n1**2)))/(len(seq)+0.0))+1
if (1 - stats.chi2.cdf(statistic, 2)) < significance:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, 2)))
+", likely false.")
else:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, 2)))
+", likely true.")
#length of sequence must be >9
#chi square deg 1, non random is higher (one sided test)
def monobit(seq):
global significance
n_0 = 0
n_1 = 0
n = len(seq)
for bit in seq:
if bit == 0:
n_0 += 1
else:
n_1 += 1
statistic = ((n_0-n_1)**2)/float(n)
if (1 - stats.chi2.cdf(statistic, 1)) < significance:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, 1)))
+", likely false.")
else:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, 1)))
+", likely true.")
def poker(seq,m):
global significance
k = math.floor(len(seq)/(m+0.0))
if k < 5*(2**m):
return "error, invalid m parameter."
counter = [0 for i in range(2**m)]
comb = list(itertools.product([0, 1], repeat=m))
for i in range(k):
tup = ()
for j in range(m):
tup = tup+(seq[(m*i)+j],)
#tup = (seq[i],seq[(i)+1],seq[(i)+2],seq[(i)+3])
for j in range(2**m):
if (tup == comb[j]):
counter[j] += 1
break
sum_counter = 0
for i in range(2**m):
sum_counter += (counter[i]**2)
statistic = (((2**m)/(k+0.0))*(sum_counter))-k
if (1 - stats.chi2.cdf(statistic, (2**m)-1)) < significance:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, (2**m)-1)))
+", likely false.")
else:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, (2**m)-1)))
+", likely true.")
def runs(seq):
n = len(seq)
gap = 0
block = 0
zero = True
k = 1
while(True):
e = (n-k+1+3)/(2**(k+1+2))
if e >= 5:
k += 1
else:
break
e = (n-k+3)/(2**(k+2))
gap_counts = [0 for i in range(n)]
block_counts = [0 for i in range(n)]
for bit in seq:
if ((zero == True) and (bit == 1)):
zero = False
if gap > 0:
gap_counts[gap] += 1
gap = 0
if ((zero == False) and (bit == 0)):
zero = True
if block > 0:
block_counts[block] += 1
block = 0
if bit == 0:
gap += 1
else:
block += 1
zero = False
if gap > 0:
gap_counts[gap] += 1
if block > 0:
block_counts[block] += 1
sum_blocks = 0
sum_gaps = 0
for i in range(1,k+1):
e_i = (n-i+3)/(2**(i+2))
sum_blocks += ((block_counts[i] - e_i)**2)/e_i
sum_gaps+= ((gap_counts[i] - e_i)**2)/e_i
statistic = sum_blocks + sum_gaps
if (1 - stats.chi2.cdf(statistic, (2*k)-2)) < significance:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, (2*k)-2)))
+", likely false.")
else:
print("Statistic = "+str(statistic)+", Signficance = "+str((1 - stats.chi2.cdf(statistic, (2*k)-2)))
+", likely true.")
def autocor(seq,d):
if d > math.floor(len(seq)/2):
return "error, sequence too small."
xor_sum = 0
for i in range(len(seq)-d):
if (not(seq[i] and seq[i+d])) and (seq[i] or seq[i+d]):
xor_sum +=1
statistic = 2*(xor_sum-((len(seq)-d)/2.0))/math.sqrt(len(seq)-d)
if (1 - stats.norm.cdf(statistic) < significance) or (stats.norm.cdf(statistic) < significance):
print("Statistic = "+str(statistic)+", likely false.")
else:
print("Statistic = "+str(statistic)+", likely true.")
def universal(seq,L,Q,K):#Q should be at least 10*(2**L)
#K hsould be at least 1000*(2**L)
#sequence length should be at least 1010*(2**l)
distribution_values = [
[6, 5.2177052, 2.954],
[7, 6.1962507 ,3.125],
[8, 7.1836656 ,3.238],
[9, 8.1764248 ,3.311],
[10, 9.1723243 ,3.356],
[11, 10.170032 ,3.384],
[12, 11.168765 ,3.401],
[13, 12.168070 ,3.410],
[14, 13.167693 ,3.416],
[15, 14.167488, 3.419],
[16, 15.167379, 3.421]]
if (not (L < 17) and (L > 5)) or (K<(1000*(2**L))) or (Q<10*(2**L)) or (len(seq) < L*1010*(2**L)) or (((K+Q)*L) > len(seq)):
print(K,(1000*(2**L)))
print(Q,10*(2**L))
print(len(seq),(K+Q)*L)
print("Parameter error")
else:
T = [0 for i in range(2**L)]
for i in range(0,Q):
T[to_int(seq[i*L:(i*L)+L])] = i
total = 0
for i in range(Q,Q+K):
total += math.log(i-T[to_int(seq[i*L:(i*L)+L])],2)
T[to_int(seq[i*L:(i*L)+L])] = i
value = total/float(K)
c = 0.7 - (0.8/float(L)) + ((4+(32/float(L)))*K**(-3/float(L)))/15.0
stdev = c*math.sqrt(distribution_values[L-6][2]/float(K))
statistic = abs((value - distribution_values[L-6][1])/(math.sqrt(2)*stdev))
p_value = math.erfc(statistic)
if (p_value < significance2):
print("Statistic = "+str(statistic)+", Signficance = "+str(p_value)+", likely false.")
else:
print("Statistic = "+str(statistic)+", Signficance = "+str(p_value)+", likely true.")