Skip to content

sequential-analysis

Sequential statistical inference in Python: tests and estimates that may be looked at after every observation and stopped at any time, with their error guarantees intact.

New here? The user guide answers the common questions directly, each with a program that runs as written: how to stop an A/B test early, how to get a confidence interval you may monitor, how to run Wald's SPRT, how to plan interim analyses of a trial, and how to detect a change in a stream.

The library has these kinds of methods:

  • Classical sequential tests: Wald's SPRT, Lorden's 2-SPRT, optimal Kiefer–Weiss plans, group sequential trials with spending functions.
  • Anytime-valid inference: mixture SPRTs, confidence sequences, nonparametric tests by betting, and A/B tests with e-values and false-discovery-rate control.
  • Change detection: CUSUM, Shiryaev–Roberts, nonparametric e-detectors and Bayesian online change points.
  • Filters and smoothers: Kalman (including a robust one), hidden Markov models and particle filters.
  • Forecasting: exponential smoothing, autoregressions and vector autoregressions, fitted on a history or learnt on the stream, with conformal intervals and anytime-valid comparisons.
  • Several series and events: lead-lag correlations and Granger tests, drifting regressions for pairs, Hawkes processes, event studies, and an as-of join of news to price bars.
  • Volatility: GARCH and GJR-GARCH with normal or Student t shocks, for variance forecasts and value at risk.

Every run can be checkpointed as plain JSON and resumed exactly. A run can also be resumed by the TypeScript executor, and the formats are specified.

pip install sequential-analysis            # pure Python
pip install "sequential-analysis[fast]"    # with numpy for the array-heavy procedures

A first run

from sequential_analysis import Run, run_sync
from sequential_analysis.procedures import Gaussian, TwoSPRT
from sequential_analysis.sources import gaussian

test = TwoSPRT(Gaussian(sigma=1.0), theta0=0.0, theta1=0.5, alpha0=0.05, alpha1=0.05)
run = Run(test)
run_sync(run, gaussian("x", mean=0.5, sd=1.0, seed=7))
print(run.state.decision, "after", run.t, "observations")

checkpoint = run.checkpoint()   # plain JSON: state, random state, buffers, provenance

A procedure is only a transition function: it takes a state and an observation and returns a new state and an output. Everything around it is kept apart:

  • adapters produce observations;
  • delivery orders and deduplicates them;
  • topology pairs or joins them;
  • the run commits each step, keeps the history digest and writes checkpoints;
  • consumers act on the outputs.

Where to go next