Skip to content

Commit

Permalink
move correct_bonferroni into a new module and add correct_holm
Browse files Browse the repository at this point in the history
  • Loading branch information
qbarthelemy committed Oct 7, 2024
1 parent 43e4a12 commit a5e11d5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
3 changes: 2 additions & 1 deletion examples/correct_multiple_correlations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import numpy as np
from scipy.stats import pearsonr, spearmanr

from pypermut.misc import correct_bonferroni, print_results, print_pvals
from pypermut.correction import correct_bonferroni
from pypermut.misc import print_results, print_pvals
from pypermut.stats import permutation_corr


Expand Down
62 changes: 62 additions & 0 deletions pypermut/correction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Correction functions."""

import numpy as np


def correct_bonferroni(p_vals, n_tests=None):
"""Correct p-values by Bonferroni's method.
Correction for multiple tests, using Bonferroni's method [1]_:
multiply p-values by the number of tests.
Parameters
----------
p_vals : array, shape (n_vals,)
The p-values.
n_tests : None | int, default=None
Number of tests. If None, n_tests is set to n_vals.
Returns
-------
p_vals_corrected : array, shape (n_vals,)
The p-values corrected by Bonferroni's method.
References
----------
.. [1] https://en.wikipedia.org/wiki/Bonferroni_correction
"""
if n_tests is None:
n_tests = len(p_vals)
return p_vals * n_tests


def correct_holm(p_vals):
"""Correct p-values by Holm's step-down method.
Correction for multiple tests, using Holm's step-down method [1]_.
Parameters
----------
p_vals : array, shape (n_vals,)
The p-values.
Returns
-------
p_vals_corrected : array, shape (n_vals,)
The p-values corrected by Holm's method.
References
----------
.. [1] https://en.wikipedia.org/wiki/Holm%E2%80%93Bonferroni_method
"""
n_tests = len(p_vals)
sortind = np.argsort(p_vals)
p_vals = np.take(p_vals, sortind)

pvals_corrected_raw = p_vals * np.arange(n_tests, 0, -1)
pvals_corrected = np.maximum.accumulate(pvals_corrected_raw)
pvals_corrected_ = np.empty_like(pvals_corrected)
pvals_corrected_[sortind] = pvals_corrected

return pvals_corrected_
28 changes: 0 additions & 28 deletions pypermut/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,6 @@ def pvals_to_stars(
return stars


def correct_bonferroni(p_vals, n_tests=None):
"""Correct p-values by Bonferroni's method.
Correction for multiple tests, using Bonferroni's method:
multiply p-values by the number of tests.
Parameters
----------
p_vals : array, shape (n_vals,)
The p-values.
n_tests : None | int, default=None
Number of tests. If None, n_tests is set to n_vals.
Returns
-------
p_vals_corrected : array, shape (n_vals,)
The p-values corrected by Bonferroni's method.
References
----------
.. [1] https://en.wikipedia.org/wiki/Bonferroni_correction
"""
if n_tests is None:
n_tests = len(p_vals)
return p_vals * n_tests


def print_results(results, r_labels, stat_label):
"""Print results of several tests: statistic value and the p-value.
Expand Down

0 comments on commit a5e11d5

Please sign in to comment.