diff --git a/src/math_tools.rs b/src/math_tools.rs index 7b3d609..f39f1cc 100644 --- a/src/math_tools.rs +++ b/src/math_tools.rs @@ -49,9 +49,7 @@ pub fn chi_square(data: &[u8]) -> f64 { }) .sum(); - // Normalize chi-square by the maximum possible value - let chi_square_max = (num_bins as f64) * (data.len() as f64 - 1.0); - chi_square / chi_square_max + chi_square } /// Calculates Chi Square of data #[pyfunction(name = "chi_square")] @@ -105,7 +103,7 @@ mod tests { // Expect chi-square to be relatively low for uniform random data assert!( - chi_square_value < 0.2, + chi_square_value < 300.0, "Chi-square for random data was too high: {}", chi_square_value ); @@ -117,9 +115,8 @@ mod tests { let non_random_data = vec![42u8; 4096]; let chi_square_value = chi_square(&non_random_data); - // Expect chi-square to be close to 1 for non-random data assert!( - chi_square_value > 0.8, + chi_square_value == 1044480.0, "Chi-square for non-random data was too low: {}", chi_square_value ); @@ -134,7 +131,7 @@ mod tests { // Check chi-square for partially random data assert!( - chi_square_value > 0.2 && chi_square_value < 0.8, + chi_square_value < 275000.0, "Chi-square for partial random data was outside expected range: {}", chi_square_value ); diff --git a/tests/test_math.py b/tests/test_math.py index 66cd65a..acba1a6 100644 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -25,10 +25,10 @@ def test_shannon_entropy(data: bytes, entropy: float): [ pytest.param(b"", 0, id="empty"), pytest.param(UNIFORM_DISTRIBUTION, 0.0, id="uniform distribution"), - pytest.param(NON_UNIFORM_DISTRIBUTION, 1.0, id="non uniform distribution"), + pytest.param(NON_UNIFORM_DISTRIBUTION, 65280.0, id="non uniform distribution"), pytest.param( UNIFORM_DISTRIBUTION + NON_UNIFORM_DISTRIBUTION, - 0.2495, + 32640.0, id="partially uniform distribution", ), ],