Skip to content

Commit

Permalink
feat(math): do not normalize chi_square value.
Browse files Browse the repository at this point in the history
  • Loading branch information
qkaiser committed Oct 27, 2024
1 parent 7adbdf9 commit 6bf4da7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 4 additions & 7 deletions src/math_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
Expand All @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
],
Expand Down

0 comments on commit 6bf4da7

Please sign in to comment.