Sequential testing¶
anywatch.sequential contains the statistical engine of the package. All
tools work with observations in the interval \([0, 1]\), which covers binary
outcomes (correct or not, detected or not) and any bounded score after
rescaling.
Betting e-processes¶
BettingEProcess tests a hypothesis about the mean \(\mu\) of the stream.
alternative |
Null hypothesis | Alarm when |
|---|---|---|
"less" |
\(\mu \ge m\) | the mean is below \(m\) |
"greater" |
\(\mu \le m\) | the mean is above \(m\) |
"two-sided" |
\(\mu = m\) | the mean differs from \(m\) |
from anywatch.sequential import BettingEProcess
test = BettingEProcess(null_mean=0.9, alternative="less", alpha=0.05)
for batch in batches:
test.update(batch)
if test.rejected:
print("Evidence that the mean is below 0.9 at observation", test.rejection_time)
break
Useful attributes:
evalue: the current e-process value. Values far above one are evidence against the null.max_evalue,anytime_pvalue: the running maximum and the anytime-valid p-value \(\min(1, 1 / \max_t E_t)\).rejection_time,rejection_evalue,rejection_estimate: the state at the first crossing of \(1 / \alpha\).
Confidence sequences¶
Confidence sequences are the interval counterpart of e-processes.
from anywatch.sequential import EmpiricalBernsteinCS, BettingCS
cs = EmpiricalBernsteinCS(alpha=0.05)
cs.update(observations)
print(cs.interval)
EmpiricalBernsteinCShas a closed form and is very fast.BettingCSinverts a grid of betting tests and is usually tighter, especially for means close to zero or one, such as high sensitivities.
Both use a running intersection by default, so the bounds only move inward.
Batch analysis¶
sequential_mean_test runs an e-process over a complete array, which is
convenient for retrospective audits:
from anywatch.sequential import sequential_mean_test
result = sequential_mean_test(correct, null_mean=0.9)
print(result.rejected, result.anytime_pvalue)
Paired comparisons¶
PairedDifferenceEProcess compares two outcomes on the same cases, for
example the deployed model against a reference model, with an optional
non-inferiority margin:
from anywatch.sequential import PairedDifferenceEProcess
test = PairedDifferenceEProcess(margin=-0.02, alternative="less")
test.update(new_model_correct, reference_correct)