Download tests/test_stats.py from bunyamineymen/rag-gate-stats-code: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/bunyamineymen/rag-gate-stats-code/resolve/main/tests/test_stats.py
- Command line
-
hf download hf://bunyamineymen/rag-gate-stats-code/tests/test_stats.py
-
curl -L -o test_stats.py https://huggingface.co/bunyamineymen/rag-gate-stats-code/resolve/main/tests/test_stats.py
1.78 kB
| import numpy as np | |
| from gatestats import gates, stats | |
| def test_bootstrap_lower_bound_covers_true_mean_at_nominal_rate(): | |
| rng = np.random.default_rng(1) | |
| X = rng.binomial(1, 0.3, size=(1000, 100)).astype(float) | |
| lb = stats.lower_bound(X @ stats.bootstrap_weights(100, 2000, rng).T, 0.05) | |
| assert 0.93 <= (lb <= 0.3).mean() <= 0.97 | |
| def test_two_sided_paired_ci_coverage(): | |
| rng = np.random.default_rng(2) | |
| hits = 0 | |
| for i in range(1000): | |
| a = rng.normal(0.5, 0.2, 100) | |
| b = a + rng.normal(0.01, 0.1, 100) | |
| _, lo, hi = stats.paired_bootstrap_ci(a, b, B=1000, seed=i) | |
| hits += lo <= 0.01 <= hi | |
| assert 0.93 <= hits / 1000 <= 0.97 | |
| def test_sign_flip_rejects_only_clear_effects(): | |
| rng = np.random.default_rng(3) | |
| flips = rng.choice([-1.0, 1.0], size=(2000, 50)) | |
| assert stats.sign_flip_pvalues(np.full(50, 0.1) + rng.normal(0, 0.05, 50), flips) < 0.01 | |
| assert stats.sign_flip_pvalues(rng.normal(-0.1, 0.05, 50), flips) > 0.5 | |
| def test_gates_on_identical_systems(): | |
| rng = np.random.default_rng(4) | |
| a = rng.binomial(1, 0.5, size=(200, 50)).astype(float) | |
| W = stats.bootstrap_weights(50, 1000, rng) | |
| assert gates.g3_paired(a, a, 0.01, W, 0.05).all() # zero paired variance: always PASS | |
| assert not gates.g3u_unpaired(a, a, 0.01, W, stats.bootstrap_weights(50, 1000, rng), 0.05).any() | |
| def test_wilson_holm_mde(): | |
| lo, hi = stats.wilson_ci(5, 10) | |
| assert abs(lo - 0.2366) < 1e-3 and abs(hi - 0.7634) < 1e-3 | |
| assert np.allclose(stats.holm([0.01, 0.04, 0.03]), [0.03, 0.06, 0.06]) | |
| assert stats.required_n(0.3, stats.mde(0.3, 100)) in (100, 101) | |
| assert abs(stats.noninferiority_pass_prob(-0.02, 0.02, 0.3, 100) - 0.05) < 1e-12 | |