GMTROM/Python_Tests/StartUpTest.py

60 lines
1.8 KiB
Python
Raw Permalink Normal View History

2023-05-16 13:44:22 +02:00
from math import fabs as fabs
from math import sqrt as sqrt
from scipy.special import erfc as erfc
2023-05-24 09:52:05 +02:00
import numpy as np
from scipy import stats
2023-05-16 13:44:22 +02:00
2023-05-24 09:52:05 +02:00
class StartUPTest:
2023-05-16 13:44:22 +02:00
@staticmethod
def run_all_tests(binary_data: str):
# Run monobit_test
2023-05-24 09:52:05 +02:00
p_value, result = StartUPTest.monobit_test(binary_data)
if not result:
return False
2023-05-24 09:52:05 +02:00
# Run chi_square
p_value, result, chi2_statistic = StartUPTest.chi_square(binary_data)
if not result:
return False
# All tests passed
return True
2023-05-16 13:44:22 +02:00
@staticmethod
def monobit_test(binary_data: str):
length_of_bit_string = len(binary_data)
# Variable for S(n)
count = 0
# Iterate each bit in the string and compute for S(n)
for bit in binary_data:
if bit == 48:
# If bit is 0, then -1 from the S(n)
count -= 1
elif bit == 49:
# If bit is 1, then +1 to the S(n)
count += 1
# Compute the test statistic
sObs = count / sqrt(length_of_bit_string)
# Compute p-Value
p_value = erfc(fabs(sObs) / sqrt(2))
# return a p_value and randomness result
return p_value, (p_value >= 0.01)
@staticmethod
2023-05-24 09:52:05 +02:00
def chi_square(binary_data: str):
2023-05-16 13:44:22 +02:00
2023-05-24 09:52:05 +02:00
observed_frequencies = [binary_data.count(48), binary_data.count(49)]
expected_probabilities = [0.5, 0.5] # Assuming equal probability for each bit value
total_observations = len(binary_data)
expected_frequencies = np.array(expected_probabilities) * total_observations
2023-05-16 13:44:22 +02:00
2023-05-24 09:52:05 +02:00
chi2_statistic, p_value = stats.chisquare(observed_frequencies, f_exp=expected_frequencies)
2023-05-16 13:44:22 +02:00
2023-05-24 09:52:05 +02:00
return p_value, p_value >= 0.01, chi2_statistic