Fix(stats): Correct p-value calculation in permutation test#8
Merged
sarthakpati merged 1 commit intoIUCompPath:mainfrom Jul 31, 2025
Merged
Fix(stats): Correct p-value calculation in permutation test#8sarthakpati merged 1 commit intoIUCompPath:mainfrom
sarthakpati merged 1 commit intoIUCompPath:mainfrom
Conversation
Collaborator
Author
|
This code should be good to go and can be tested under normal tests. |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a critical bug in the perform_permutation_test method where the p-value calculation used an incorrect one-sided test that could fail to detect statistically significant differences when the second method outperformed the first.
- Corrects the permutation test to use a proper two-sided test by comparing absolute differences
- Ensures the p-value matrix is symmetric as it should be for pairwise comparisons
- Improves code efficiency with boolean indexing and better variable management
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request addresses a bug in the
perform_permutation_testmethod where the p-value was calculated using an incorrect one-sided test. This resulted in unreliable significance testing, potentially masking real differences between methods.The Bug: Incorrect One-Sided Test Logic
The previous implementation calculated the observed difference in summed ranks as
diff_ranks = arr_i.sum() - arr_j.sum()and then counted how many permuted differences were smaller (diff_ranks_rand < diff_ranks).This logic only worked correctly if
method_iwas superior tomethod_j(resulting in a large negativediff_ranks). Ifmethod_jwas superior tomethod_i,diff_rankswould be a large positive number. The conditiondiff_ranks_rand < diff_rankswould then be met by almost all permutations, leading to an erroneously high p-value (e.g., > 0.5) and a failure to detect a statistically significant difference.The Fix: Implementing a Standard Two-Sided Test
The permutation test has been corrected to use a standard two-sided test, which is the correct approach for determining if two methods are significantly different, regardless of the direction of the difference.
The changes are as follows:
observed_diff = abs(arr_i.sum() - arr_j.sum())if abs(permuted_diff) >= observed_diff:self.pvals) is now correctly populated to be symmetric (self.pvals[i, j] = self.pvals[j, i]), as the significance of the difference between method A and B is identical to that between B and A.self.pvalsmatrix is now initialized withnp.ones(), which is more semantically correct, as the default state (including the diagonal) represents no significant difference (p=1.0).