mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Add optimization study O06: does a staged evaluation ladder (cheap diagnostics, a development battery, then full evaluation) save total cost without rejecting useful candidates? - Arms: full evaluation of every candidate (bounded reference), a development-only ladder, and a ladder with a stage-0 score; usefulness is decided only by full evaluation with every collateral endpoint. - Stage-0 scores must earn their role: predictor quality (Spearman, AUC) is reported, and separability or KL never substitutes for task success. - Extra development samples use an anytime-valid mixture test (or a single fixed-sample look); naive repeated peeking is refused. Null simulations: 0.0245 and 0.0405 at alpha 0.05 against 0.24 for peeking. - A seeded audit sample of exclusions is fully evaluated and every exclusion is reported. Rules are digest-bound; confirmation-exposed, test and confirmation rows are refused. - Verdict is "no improvement" unless every seed passes cost, false-rejection, task and collateral tolerances with an unchanged confirmation candidate. `obliteratus experiments screening`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
5.5 KiB
Python
136 lines
5.5 KiB
Python
"""Sequential and fixed-sample stage-1 decisions for calibrated screening (O06).
|
|
|
|
Stage 1 asks whether a candidate's development success rate ``p`` is below a
|
|
declared floor ``p0``. A candidate is screened out only on evidence that
|
|
``p < p0``; everything unresolved goes on to full evaluation.
|
|
|
|
``mixture-martingale`` (default) is anytime-valid. Under ``H0: p >= p0`` the
|
|
uniform-prior mixture likelihood ratio over alternatives ``q`` in ``[0, p0)``
|
|
|
|
M_n = (1 / p0) * integral_0^p0 q^s (1 - q)^f dq / (p0^s (1 - p0)^f)
|
|
|
|
is a non-negative supermartingale with ``M_0 = 1``, so by Ville's inequality
|
|
``P(sup_n M_n >= 1 / alpha) <= alpha``: rows may be added in batches, inspected
|
|
after every batch and allocated adaptively without inflating the error.
|
|
Early promotion uses the mirrored mixture over ``[p0, 1]``; a promotion error
|
|
only costs a full evaluation. The integrals are evaluated exactly through the
|
|
identity ``I_x(s + 1, f + 1) = P(Binomial(s + f + 1, x) >= s + 1)``.
|
|
|
|
``fixed-sample`` looks once, after ``max_rows`` rows, with the exact one-sided
|
|
binomial test; it is valid because it is inspected once.
|
|
|
|
``naive-peeking`` (a Wilson upper bound checked after every batch) is provided
|
|
only to demonstrate in simulations that repeated inspection of a fixed-sample
|
|
interval exceeds its nominal error; study specs refuse it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import random
|
|
from functools import lru_cache
|
|
|
|
from obliteratus.concepts.statistics import wilson_interval
|
|
|
|
METHODS = ("mixture-martingale", "fixed-sample")
|
|
SIMULATION_ONLY = ("naive-peeking",)
|
|
|
|
|
|
def _logsumexp(values):
|
|
top = max(values)
|
|
if top == -math.inf:
|
|
return -math.inf
|
|
return top + math.log(sum(math.exp(v - top) for v in values))
|
|
|
|
|
|
def _log_binom_term(n, k, p):
|
|
return (math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1)
|
|
+ k * math.log(p) + (n - k) * math.log1p(-p))
|
|
|
|
|
|
def _log_tail(n, low, high, p):
|
|
"""log P(low <= Binomial(n, p) <= high)."""
|
|
if low > high:
|
|
return -math.inf
|
|
return _logsumexp([_log_binom_term(n, k, p) for k in range(low, high + 1)])
|
|
|
|
|
|
def _log_beta(a, b):
|
|
return math.lgamma(a) + math.lgamma(b) - math.lgamma(a + b)
|
|
|
|
|
|
@lru_cache(maxsize=65536)
|
|
def log_evidence(successes, failures, p0):
|
|
"""``(log M_reject, log M_promote)`` for ``s`` successes and ``f`` failures."""
|
|
if not 0 < p0 < 1:
|
|
raise ValueError("p0 must lie strictly between 0 and 1")
|
|
s, f = successes, failures
|
|
n = s + f
|
|
null = s * math.log(p0) + f * math.log1p(-p0)
|
|
base = _log_beta(s + 1, f + 1) - null
|
|
below = _log_tail(n + 1, s + 1, n + 1, p0) # I_p0(s+1, f+1)
|
|
above = _log_tail(n + 1, 0, s, p0) # 1 - I_p0(s+1, f+1)
|
|
return base + below - math.log(p0), base + above - math.log1p(-p0)
|
|
|
|
|
|
def _exact_lower_p(successes, n, p0):
|
|
return math.exp(_log_tail(n, 0, successes, p0))
|
|
|
|
|
|
def decide(method, successes, n, *, p0, alpha, max_rows):
|
|
"""``"reject"``, ``"promote"`` or ``"continue"`` after ``n`` rows."""
|
|
if method == "mixture-martingale":
|
|
reject, promote = log_evidence(successes, n - successes, p0)
|
|
if reject >= math.log(1 / alpha):
|
|
return "reject"
|
|
if promote >= math.log(1 / alpha):
|
|
return "promote"
|
|
return "promote" if n >= max_rows else "continue"
|
|
if method == "fixed-sample":
|
|
if n < max_rows:
|
|
return "continue"
|
|
return "reject" if _exact_lower_p(successes, n, p0) <= alpha else "promote"
|
|
if method == "naive-peeking":
|
|
upper = wilson_interval(successes, n, confidence=1 - 2 * alpha)[1]
|
|
if upper < p0:
|
|
return "reject"
|
|
return "promote" if n >= max_rows else "continue"
|
|
raise ValueError(f"Unknown stage-1 method {method!r}; use one of {METHODS}")
|
|
|
|
|
|
def run_sequence(method, outcomes, *, p0, alpha, batch, max_rows):
|
|
"""Apply ``method`` to one ordered outcome stream; returns (decision, rows, successes)."""
|
|
successes = n = 0
|
|
first = max_rows if method == "fixed-sample" else batch
|
|
while True:
|
|
step = first if n == 0 else batch
|
|
chunk = outcomes[n:min(n + step, max_rows)]
|
|
n += len(chunk)
|
|
successes += sum(bool(x) for x in chunk)
|
|
verdict = decide(method, successes, n, p0=p0, alpha=alpha, max_rows=max_rows)
|
|
if verdict != "continue":
|
|
return verdict, n, successes
|
|
|
|
|
|
def simulate_null(method, *, p0=0.5, true_rate=None, alpha=0.05, batch=4, max_rows=120,
|
|
repetitions=2000, seed=0):
|
|
"""Monte Carlo rejection rate at ``true_rate`` (default ``p0``) with its standard error."""
|
|
rate = p0 if true_rate is None else true_rate
|
|
rng = random.Random(f"screening-null:{method}:{seed}")
|
|
rejected = 0
|
|
for _ in range(repetitions):
|
|
stream = [rng.random() < rate for _ in range(max_rows)]
|
|
rejected += run_sequence(method, stream, p0=p0, alpha=alpha, batch=batch,
|
|
max_rows=max_rows)[0] == "reject"
|
|
estimate = rejected / repetitions
|
|
return {"method": method, "p0": p0, "true_rate": rate, "alpha": alpha, "batch": batch,
|
|
"max_rows": max_rows, "looks": math.ceil(max_rows / batch), "repetitions":
|
|
repetitions, "seed": seed, "rejection_rate": estimate,
|
|
"mc_standard_error": math.sqrt(max(estimate * (1 - estimate), 1e-12) / repetitions),
|
|
"nominal_bound_holds": estimate <= alpha + 3 * math.sqrt(alpha * (1 - alpha)
|
|
/ repetitions)}
|
|
|
|
|
|
__all__ = ["METHODS", "SIMULATION_ONLY", "decide", "log_evidence", "run_sequence",
|
|
"simulate_null"]
|