"""Stage-1 decision procedures of calibrated screening: error guarantees under peeking. Null simulations at ``p = p0``: the anytime-valid mixture martingale and the single-look fixed-sample test keep the rejection rate within ``alpha`` plus three Monte Carlo standard errors, while re-checking a fixed-sample Wilson interval after every batch exceeds it. """ from __future__ import annotations import math import pytest from obliteratus.analysis.screening_sequential import ( decide, log_evidence, run_sequence, simulate_null, ) pytestmark = pytest.mark.cpu ALPHA = 0.05 REPS = 2000 BOUND = ALPHA + 3 * math.sqrt(ALPHA * (1 - ALPHA) / REPS) @pytest.mark.parametrize("method", ["mixture-martingale", "fixed-sample"]) def test_valid_procedures_hold_alpha_on_null_simulations(method): found = simulate_null(method, p0=0.5, alpha=ALPHA, batch=4, max_rows=120, repetitions=REPS, seed=1) assert found["looks"] == 30 and found["rejection_rate"] <= BOUND assert found["nominal_bound_holds"] # Above the floor (the composite null) the error is smaller still. above = simulate_null(method, p0=0.5, true_rate=0.6, alpha=ALPHA, repetitions=500, seed=2) assert above["rejection_rate"] <= BOUND def test_naive_repeated_peeking_exceeds_alpha(): found = simulate_null("naive-peeking", p0=0.5, alpha=ALPHA, batch=4, max_rows=120, repetitions=REPS, seed=1) assert found["rejection_rate"] > BOUND and not found["nominal_bound_holds"] def test_sequential_test_has_power_and_stops_early(): found = simulate_null("mixture-martingale", p0=0.5, true_rate=0.1, repetitions=300, seed=3) assert found["rejection_rate"] > 0.95 decision, rows, successes = run_sequence("mixture-martingale", [False] * 40, p0=0.5, alpha=ALPHA, batch=4, max_rows=40) assert decision == "reject" and rows < 16 and successes == 0 decision, rows, _ = run_sequence("mixture-martingale", [True] * 40, p0=0.5, alpha=ALPHA, batch=4, max_rows=40) assert decision == "promote" and rows < 16 decision, rows, _ = run_sequence("fixed-sample", [False] * 40, p0=0.5, alpha=ALPHA, batch=4, max_rows=40) assert decision == "reject" and rows == 40 def test_evidence_and_decisions_are_well_defined(): reject, promote = log_evidence(0, 0, 0.3) assert reject == pytest.approx(0.0, abs=1e-9) and promote == pytest.approx(0.0, abs=1e-9) assert decide("mixture-martingale", 3, 6, p0=0.5, alpha=ALPHA, max_rows=6) == "promote" assert decide("fixed-sample", 0, 3, p0=0.5, alpha=ALPHA, max_rows=6) == "continue" assert decide("naive-peeking", 10, 10, p0=0.5, alpha=ALPHA, max_rows=10) == "promote" with pytest.raises(ValueError, match="strictly between"): log_evidence(1, 1, 1.0) with pytest.raises(ValueError, match="Unknown stage-1 method"): decide("bayes", 1, 2, p0=0.5, alpha=ALPHA, max_rows=4)