Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if there are no p values to correct #233

Merged
merged 4 commits into from Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions expan/core/correction.py
Expand Up @@ -3,15 +3,17 @@

def benjamini_hochberg(false_discovery_rate, original_p_values):
""" Benjamini-Hochberg procedure.

:param false_discovery_rate: proportion of significant results that are actually false positives
:type false_discovery_rate: float
:param original_p_values: p values from all the tests
:type original_p_values: list[float]

:return: new critical value (i.e. the corrected alpha)
:rtype: float
"""
assert original_p_values, "empty array of p values to analyse"

p_values_sorted = np.sort(np.asarray(original_p_values))
number_tests = len(original_p_values)
significant_ranks = [i for i, val in enumerate(p_values_sorted, 1) if val <= i * false_discovery_rate / number_tests]
Expand All @@ -21,13 +23,15 @@ def benjamini_hochberg(false_discovery_rate, original_p_values):

def bonferroni(false_positive_rate, original_p_values):
""" Bonferrnoi correction.

:param false_positive_rate: alpha value before correction
:type false_positive_rate: float
:param original_p_values: p values from all the tests
:type original_p_values: list[float]

:return: new critical value (i.e. the corrected alpha)
:rtype: float
"""
assert original_p_values, "empty array of p values to analyse"

return false_positive_rate / len(original_p_values)
10 changes: 9 additions & 1 deletion tests/tests_core/test_correction.py
@@ -1,5 +1,5 @@
import unittest
from expan.core.correction import *
from expan.core.correction import benjamini_hochberg, bonferroni


class CorrectionTestCase(unittest.TestCase):
Expand Down Expand Up @@ -28,6 +28,10 @@ def test_benjamini_hochberg_one_p_value(self):
corrected_alpha = benjamini_hochberg(false_discovery_rate, original_p_values)
self.assertAlmostEqual(corrected_alpha, 0.05)

def test_benjamini_hochberg_empty(self):
with self.assertRaises(AssertionError):
_ = benjamini_hochberg(0.5, [])

def test_bonferroni(self):
false_discovery_rate = 0.05
original_p_values = [0.1, 0.03, 0.02, 0.03]
Expand All @@ -41,3 +45,7 @@ def test_bonferroni_one_p_value(self):

corrected_alpha = bonferroni(false_discovery_rate, original_p_values)
self.assertAlmostEqual(corrected_alpha, 0.05)

def test_bonferroni_empty(self):
with self.assertRaises(AssertionError):
_ = bonferroni(0.5, [])