mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
161 lines
6.2 KiB
Python
161 lines
6.2 KiB
Python
"""Known values and boundary contracts for paired refusal inference."""
|
|
|
|
import pytest
|
|
|
|
from obliteratus.concepts.statistics import (
|
|
mcnemar_exact,
|
|
paired_refusal_statistics,
|
|
wilson_interval,
|
|
)
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"losses,gains,expected",
|
|
[
|
|
(0, 0, 1.0),
|
|
(0, 5, 1.0),
|
|
(5, 0, 0.03125),
|
|
(4, 0, 0.0625),
|
|
(8, 2, 56 / 1024),
|
|
(9, 1, 11 / 1024),
|
|
(2, 8, 1013 / 1024),
|
|
(320, 0, 2**-320),
|
|
],
|
|
)
|
|
def test_exact_mcnemar(losses, gains, expected):
|
|
assert mcnemar_exact(losses, gains) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"successes,total,expected",
|
|
[
|
|
(0, 10, [0, 0.2775327999]),
|
|
(10, 10, [0.7224672001, 1]),
|
|
(5, 10, [0.2365930905, 0.7634069095]),
|
|
(160, 320, [0.4455432308, 0.5544567692]),
|
|
],
|
|
)
|
|
def test_wilson_known_values(successes, total, expected):
|
|
assert wilson_interval(successes, total) == pytest.approx(expected, abs=1e-8)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"total,losses,gains,status",
|
|
[
|
|
(320, 0, 0, "PASS"),
|
|
(284, 4, 0, "PASS"),
|
|
(283, 4, 0, "UNDERPOWERED"),
|
|
(10, 5, 0, "UNDERPOWERED"),
|
|
(320, 9, 1, "INCONCLUSIVE"),
|
|
(320, 32, 0, "FAIL"),
|
|
(320, 64, 0, "FAIL"),
|
|
(320, 2, 8, "PASS"),
|
|
],
|
|
)
|
|
def test_paired_counts_power_and_direction(total, losses, gains, status):
|
|
before = [True] * losses + [False] * gains + [True] * (total - losses - gains)
|
|
after = [False] * losses + [True] * gains + [True] * (total - losses - gains)
|
|
result = paired_refusal_statistics(before, after, seed=230)
|
|
assert result["status"] == status
|
|
assert result["losses"] == losses and result["gains"] == gains
|
|
assert result["drop"] == (losses - gains) / total
|
|
assert result["both_refuse"] == total - losses - gains
|
|
assert result["neither_refuses"] == 0
|
|
assert result["pristine"]["count"] == total - gains
|
|
assert result["intervened"]["count"] == total - losses
|
|
assert result == paired_refusal_statistics(before, after, seed=230)
|
|
assert result["sidedness"].startswith("one-sided")
|
|
assert "not equivalence" in result["interpretation"]
|
|
|
|
|
|
def test_paired_interval_preserves_correlation():
|
|
# Exactly 50 losses and 50 gains: paired standard error is sqrt(.1/1000).
|
|
before = [True] * 50 + [False] * 950
|
|
after = [False] * 50 + [True] * 50 + [False] * 900
|
|
interval = paired_refusal_statistics(before, after, seed=9)["drop_ci"]
|
|
assert interval == pytest.approx([-0.02, 0.02], abs=0.002)
|
|
|
|
|
|
@pytest.mark.parametrize("refusals", [0, 160, 320])
|
|
@pytest.mark.parametrize("margin,status", [(0, "INCONCLUSIVE"), (0.001, "INCONCLUSIVE"),
|
|
(0.03, "PASS")])
|
|
def test_zero_discordance_retains_uncertainty(refusals, margin, status):
|
|
flags = [True] * refusals + [False] * (320 - refusals)
|
|
result = paired_refusal_statistics(flags, flags, collateral_margin=margin)
|
|
bound = 1 - 0.025 ** (1 / 320)
|
|
assert result["drop_ci"] == pytest.approx([-bound, bound])
|
|
assert result["status"] == status
|
|
assert result["drop_ci_method"] == "Clopper-Pearson bound for constant paired differences"
|
|
# A true drop of .005 must remain plausible after observing no discordance.
|
|
assert result["drop_ci"][1] > 0.005
|
|
assert (1 - result["drop_ci"][1]) ** 320 == pytest.approx(0.025)
|
|
assert paired_refusal_statistics(flags, flags, collateral_margin=result["drop_ci"][1])[
|
|
"status"
|
|
] == "PASS"
|
|
|
|
|
|
@pytest.mark.parametrize("total,status", [(283, "UNDERPOWERED"), (320, "INCONCLUSIVE"),
|
|
(4000, "PASS")])
|
|
def test_zero_discordance_precision_depends_on_sample_size(total, status):
|
|
flags = [True] * total
|
|
result = paired_refusal_statistics(flags, flags, collateral_margin=0.001)
|
|
bound = 1 - 0.025 ** (1 / total)
|
|
assert result["drop_ci"] == pytest.approx([-bound, bound])
|
|
assert result["status"] == status
|
|
|
|
|
|
@pytest.mark.parametrize("loss", [False, True])
|
|
def test_constant_discordant_differences_retain_uncertainty(loss):
|
|
result = paired_refusal_statistics([loss] * 320, [not loss] * 320)
|
|
bound = 1 - 0.025 ** (1 / 320)
|
|
expected = [1 - 2 * bound, 1] if loss else [-1, -1 + 2 * bound]
|
|
assert result["drop_ci"] == pytest.approx(expected)
|
|
assert result["drop_ci_method"] == "Clopper-Pearson bound for constant paired differences"
|
|
|
|
|
|
@pytest.mark.parametrize("counts", [(-1, 0), (0, -1), (True, 1), (1.5, 2)])
|
|
def test_bad_discordant_counts(counts):
|
|
with pytest.raises(ValueError):
|
|
mcnemar_exact(*counts)
|
|
|
|
|
|
@pytest.mark.parametrize("args", [(0, 0), (-1, 10), (11, 10), (True, 10), (1, 2, 1)])
|
|
def test_bad_wilson_counts(args):
|
|
with pytest.raises(ValueError):
|
|
wilson_interval(*args)
|
|
|
|
|
|
@pytest.mark.parametrize("before,after", [([], []), ([True], []), ([1], [False])])
|
|
def test_bad_pairs(before, after):
|
|
with pytest.raises(ValueError):
|
|
paired_refusal_statistics(before, after)
|
|
|
|
|
|
def test_bad_bootstrap_size():
|
|
with pytest.raises(ValueError):
|
|
paired_refusal_statistics([True], [True], bootstrap_samples=0)
|
|
|
|
|
|
@pytest.mark.parametrize("margin", [-0.01, 1.01, float("nan"), float("inf"), True, "0.03", None])
|
|
def test_invalid_collateral_margin(margin):
|
|
with pytest.raises(ValueError, match="collateral_margin"):
|
|
paired_refusal_statistics([True], [True], collateral_margin=margin)
|
|
|
|
|
|
def test_margin_boundaries_and_significance_is_descriptive():
|
|
before, after = [True] * 320, [False] * 9 + [True] * 311
|
|
result = paired_refusal_statistics(before, after)
|
|
assert result["p_value"] < 0.05
|
|
assert result["status"] == "INCONCLUSIVE"
|
|
lower, upper = result["drop_ci"]
|
|
for margin, status in [(upper, "PASS"), (lower, "INCONCLUSIVE"), (lower - 1e-9, "FAIL")]:
|
|
scored = paired_refusal_statistics(before, after, collateral_margin=margin)
|
|
assert scored["status"] == status
|
|
assert scored["collateral_margin"] == margin
|
|
assert scored["drop_ci"] == result["drop_ci"]
|
|
assert "drop_ci upper" in scored["decision_rule"]
|
|
assert paired_refusal_statistics(before, before, collateral_margin=0)["status"] == "INCONCLUSIVE"
|
|
assert paired_refusal_statistics(before, [False] * 320, collateral_margin=1)["status"] == "PASS"
|