Chi Square Test hinzugefügt
parent
ac332d70e5
commit
fe08489de2
|
@ -1,25 +1,30 @@
|
||||||
from math import fabs as fabs
|
from math import fabs as fabs
|
||||||
from math import sqrt as sqrt
|
from math import sqrt as sqrt
|
||||||
from scipy.special import erfc as erfc
|
from scipy.special import erfc as erfc
|
||||||
|
import numpy as np
|
||||||
|
from scipy import stats
|
||||||
|
|
||||||
|
|
||||||
class StartUPTest:
|
class StartUPTest:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def monobit_test(binary_data: str):
|
def monobit_test(binary_data: bytes):
|
||||||
|
length_of_bit_string = len(binary_data) * 8
|
||||||
length_of_bit_string = len(binary_data)
|
|
||||||
|
|
||||||
# Variable for S(n)
|
# Variable for S(n)
|
||||||
count = 0
|
count = 0
|
||||||
# Iterate each bit in the string and compute for S(n)
|
# Iterate each byte in the string and compute for S(n)
|
||||||
for bit in binary_data:
|
for byte in binary_data:
|
||||||
if bit == 48:
|
# Iterate each bit in the byte
|
||||||
# If bit is 0, then -1 from the S(n)
|
for i in range(8):
|
||||||
count -= 1
|
# Extract the i-th bit from the byte
|
||||||
elif bit == 49:
|
bit = (byte >> i) & 1
|
||||||
# If bit is 1, then +1 to the S(n)
|
if bit == 0:
|
||||||
count += 1
|
# If bit is 0, then -1 from the S(n)
|
||||||
|
count -= 1
|
||||||
|
else:
|
||||||
|
# If bit is 1, then +1 to the S(n)
|
||||||
|
count += 1
|
||||||
|
|
||||||
# Compute the test statistic
|
# Compute the test statistic
|
||||||
sObs = count / sqrt(length_of_bit_string)
|
sObs = count / sqrt(length_of_bit_string)
|
||||||
|
@ -84,3 +89,14 @@ class StartUPTest:
|
||||||
else:
|
else:
|
||||||
value = 0
|
value = 0
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def chi_square(byte_data: bytes):
|
||||||
|
expected_probabilities = np.full(256, 1/256) # Assuming 256 possible byte values
|
||||||
|
total_observations = len(byte_data)
|
||||||
|
observed_data, _ = np.histogram(list(byte_data), bins=np.arange(257))
|
||||||
|
expected_frequencies = expected_probabilities * total_observations
|
||||||
|
|
||||||
|
chi2_statistic, p_value = stats.chisquare(observed_data, f_exp=expected_frequencies)
|
||||||
|
|
||||||
|
return p_value, (p_value >= 0.01), chi2_statistic
|
||||||
|
|
Loading…
Reference in New Issue