Skip to content

Commit 2fa0991

Browse files
CopilotEdgeTypE
andcommitted
Fix code review issues: simplify chi-square calculation and add p-value bounds check
Co-authored-by: EdgeTypE <34396598+EdgeTypE@users.noreply.github.com>
1 parent fc9853e commit 2fa0991

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

patternanalyzer/plugins/chi_square.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def run(self, data: BytesView, params: dict) -> TestResult:
5555
observed_bytes = len(counter)
5656
missing_bytes = 256 - observed_bytes
5757
if missing_bytes > 0:
58-
chi_square += missing_bytes * (expected ** 2 / expected)
58+
chi_square += missing_bytes * expected
5959

6060
# Degrees of freedom = 256 - 1 = 255
6161
df = 255
@@ -116,7 +116,7 @@ def finalize(self, params: dict) -> TestResult:
116116
observed_bytes = len(counter)
117117
missing_bytes = 256 - observed_bytes
118118
if missing_bytes > 0:
119-
chi_square += missing_bytes * (expected ** 2 / expected)
119+
chi_square += missing_bytes * expected
120120

121121
# Degrees of freedom = 256 - 1 = 255
122122
df = 255

patternanalyzer/plugins/permutation_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def run(self, data: BytesView, params: dict) -> TestResult:
9393
# Calculate p-value
9494
p_value = 1.0 - self._chi_square_cdf(chi_square, df)
9595

96+
# Ensure p_value is in valid range [0, 1]
97+
p_value = max(0.0, min(1.0, p_value))
98+
9699
# Determine if test passed
97100
alpha = float(params.get("alpha", 0.01))
98101
passed = p_value > alpha

0 commit comments

Comments
 (0)