"""The statistics behind every number on the page (analysis/stats/stats_core.py), against known answers.""" import sys import unittest from pathlib import Path import numpy as np sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "analysis/stats")) import stats_core as S # noqa: E402 class Stats(unittest.TestCase): def test_wilson(self): p, lo, hi = S.wilson(5, 10) # textbook: 0.2366 to 0.7634 self.assertAlmostEqual(lo, 0.2366, places=4) self.assertAlmostEqual(hi, 0.7634, places=4) self.assertEqual(S.wilson(0, 0), (None, None, None)) self.assertEqual(S.wilson(0, 20)[1], 0.0) def test_odds_ratio_and_fisher(self): or_, lo, hi, p, corr = S.odds_ratio(3, 1, 1, 3) # Fisher's lady tasting tea: two-sided p = 34/70 self.assertAlmostEqual(or_, 9.0) self.assertLess(lo, or_) self.assertGreater(hi, or_) self.assertAlmostEqual(p, 34 / 70) self.assertFalse(corr) self.assertTrue(S.odds_ratio(3, 0, 1, 4)[4]) # a zero cell gets the +0.5 correction def test_benjamini_hochberg(self): q = S.bh([0.01, 0.04, 0.03, 0.5]) self.assertEqual([round(x, 4) for x in q], [0.04, 0.0533, 0.0533, 0.5]) def test_mantel_haenszel_equals_the_odds_ratio_when_strata_agree(self): or_, lo, hi, p = S.mantel_haenszel([[10, 20, 5, 40], [20, 40, 10, 80]]) self.assertAlmostEqual(or_, 4.0) self.assertLess(p, 0.001) self.assertIsNone(S.mantel_haenszel([[0, 5, 0, 5]])) def test_logistic_ridge_recovers_a_known_effect(self): rng = np.random.default_rng(1) x = rng.integers(0, 2, 20000) y = (rng.random(20000) < 1 / (1 + np.exp(-(-1 + 1.2 * x)))).astype(float) b, se = S.logistic_ridge(np.column_stack([np.ones_like(x), x]), y, lam=1.0) self.assertAlmostEqual(b[1], 1.2, delta=0.1) self.assertTrue(np.all(se > 0)) if __name__ == "__main__": unittest.main()