„Tets_Python/TotOnline.py“ ändern

main
Gideon Regehr 2023-05-09 11:47:47 +02:00
parent 9b7af12c8e
commit c1ddda4493
1 changed files with 226 additions and 227 deletions

View File

@ -9,7 +9,7 @@ from scipy.special import gammaincc as gammaincc
class TotOnline:
@staticmethod
def total_failure(binary_data: str, pattern_length=10):
def total_failure_test(binary_data: str, pattern_length=10):
length_of_binary_data = len(binary_data)
@ -32,13 +32,13 @@ class TotOnline:
vobs_02[int(binary_data[i:i + pattern_length + 1:], 2)] += 1
# Calculate the test statistics and p values
vobs = [vobs_01, vobs_02]
vObs = [vobs_01, vobs_02]
sums = zeros(2)
for i in range(2):
for j in range(len(vobs[i])):
if vobs[i][j] > 0:
sums[i] += vobs[i][j] * log(vobs[i][j] / length_of_binary_data)
for j in range(len(vObs[i])):
if vObs[i][j] > 0:
sums[i] += vObs[i][j] * log(vObs[i][j] / length_of_binary_data)
sums /= length_of_binary_data
ape = sums[0] - sums[1]
@ -46,7 +46,7 @@ class TotOnline:
p_value = gammaincc(pow(2, pattern_length - 1), xObs / 2.0)
return (p_value, (p_value >= 0.01))
return p_value, (p_value >= 0.01)
@staticmethod
def monobit_test(binary_data: str):
@ -71,10 +71,10 @@ class TotOnline:
p_value = erfc(fabs(sObs) / sqrt(2))
# return a p_value and randomness result
return (p_value, (p_value >= 0.01))
return p_value, (p_value >= 0.01)
@staticmethod
def block_frequency(binary_data: str, block_size=128):
def block_frequency_test(binary_data: str, block_size=128):
length_of_bit_string = len(binary_data)
@ -119,12 +119,11 @@ class TotOnline:
# Compute P-Value
p_value = gammaincc(number_of_blocks / 2, result / 2)
return (p_value, (p_value >= 0.01))
return p_value, (p_value >= 0.01)
@staticmethod
def run_test(binary_data: str):
one_count = 0
vObs = 0
length_of_binary_data = len(binary_data)
@ -139,7 +138,7 @@ class TotOnline:
# Step 2 - If it can be shown that absolute value of (π - 0.5) is greater than or equal to tau
# then the run test need not be performed.
if abs(pi - 0.5) >= tau:
return (0.0000)
return 0.0000
else:
# Step 3 - Compute vObs
for item in range(1, length_of_binary_data):
@ -148,9 +147,9 @@ class TotOnline:
vObs += 1
# Step 4 - Compute p_value = erfc((|vObs 2nπ * (1π)|)/(2 * sqrt(2n) * π * (1π)))
p_value = erfc(abs(vObs - (2 * (length_of_binary_data) * pi * (1 - pi))) / (2 * sqrt(2 * length_of_binary_data) * pi * (1 - pi)))
p_value = erfc(abs(vObs - (2 * length_of_binary_data * pi * (1 - pi))) / (2 * sqrt(2 * length_of_binary_data) * pi * (1 - pi)))
return (p_value, (p_value > 0.01))
return p_value, (p_value > 0.01)
@staticmethod
def longest_one_block_test(binary_data: str):
@ -161,7 +160,7 @@ class TotOnline:
# Initialized k, m. n, pi and v_values
if length_of_binary_data < 128:
# Not enough data to run this test
return (0.00000, 'Error: Not enough data to run this test')
return 0.00000, 'Error: Not enough data to run this test'
elif length_of_binary_data < 6272:
k = 3
m = 8
@ -183,7 +182,7 @@ class TotOnline:
block_start = 0
block_end = m
xObs = 0
# This will intialized an array with a number of 0 you specified.
# This will initialize an array with a number of 0 you specified.
frequencies = zeros(k + 1)
# print('Number of Blocks: ', number_of_blocks)
@ -224,4 +223,4 @@ class TotOnline:
p_value = gammaincc(float(k / 2), float(xObs / 2))
return (p_value, (p_value > 0.01))
return p_value, (p_value > 0.01)