Skip to content

Change detection

sequential_analysis.procedures.cusum

Page's CUSUM change-point detector.

CUSUM

Bases: Procedure

W_n = max(0, W_{n-1} + log f_theta1(X_n)/f_theta0(X_n)); alarm when W_n >= threshold.

sequential_analysis.procedures.shiryaev_roberts

The Shiryaev-Roberts change-point detector.

R_n = (1 + R_{n-1}) * f_theta1(X_n) / f_theta0(X_n),   R_0 = 0,

alarm at the first n with R_n >= A. Before the change R_n - n is a martingale, so the average run length to a false alarm is at least A (Pollak 1985). Unlike CUSUM, the statistic is a sum over all possible change points rather than a maximum; it is optimal for detecting a change that happens after a long stationary stretch.

The statistic is kept on the log scale. R_0 = 0 has no logarithm, so the state stores log_r = None until the first observation.

sequential_analysis.procedures.edetector

Nonparametric change detection with e-detectors (Shin, Ramdas and Rinaldo 2024).

CUSUM and Shiryaev-Roberts need the density before and after the change. An e-detector needs only a statement about the data before the change -- "the mean is m and the values lie in [lo, hi]", "the median is q0" -- and guarantees the average run length to a false alarm, whatever the distribution otherwise is.

For a bet lambda, a factor L_i = 1 + lambda Y_i with E[Y_i | past] = 0 before the change has conditional mean one. The product of the factors from a candidate change point j onwards is an e-process that grows only after a real change, and the detectors combine all candidate j:

Shiryaev-Roberts type   M_t = L_t (M_{t-1} + 1)        = sum_j prod_{i=j..t} L_i
CUSUM type              M_t = L_t max(M_{t-1}, 1)      = max_j prod_{i=j..t} L_i

Before the change E[M_t | past] <= M_{t-1} + 1, so M_t - t is a supermartingale, and by optional stopping the first t with M_t >= A has E[tau] >= A: the average run length to a false alarm is at least arl. The CUSUM type is never larger than the Shiryaev-Roberts type, so the same holds for it. As the size of the change is unknown, the detector mixes a geometric grid of bets (each with its own recursion, equal weights), which keeps the guarantee: a weighted average of e-detectors is an e-detector.

MeanEDetector: values in [lo, hi] with mean m before the change; detects a change of the mean. QuantileEDetector: q0 is the p-quantile before the change (P(X <= q0) = p); detects a change of the quantile, for data of any shape, heavy tails included. direction is "increase", "decrease" or "both".

The two types have the same guarantee but behave differently: the Shiryaev-Roberts type grows like t on average before the change, so its run length to a false alarm is spread out -- with arl=1000, 14% of simulated runs on skewed steady-state data alarmed within 400 observations -- while the CUSUM type stays near one and alarmed in 0.3 to 0.7% of them, in exchange for a somewhat later detection. Choose CUSUM when early false alarms are costly.

The reported change estimate is the start of the dominant bet's product under the CUSUM rule (the last time its statistic was below one).

Cost per step (PERFORMANCE.md): O(grid) and a constant state, 6 µs for the default grid of 10 bets per direction, both directions.

MeanEDetector

Bases: _EDetector

Detects a change of the mean of values in [lo, hi] whose mean is m before the change.

QuantileEDetector

Bases: _EDetector

Detects a change of the p-quantile, q0 before the change, for data of any distribution.

The score is p - 1{X <= q0}: positive on average once the quantile has moved above q0. Before the change P(X <= q0) = p is assumed (no atom of probability at q0).

sequential_analysis.procedures.bocpd

Bayesian online change-point detection (Adams and MacKay 2007).

The data come in segments; before each observation after the first, a new segment starts with probability hazard, independently, and the parameters of each segment are drawn afresh from a conjugate prior. The run length L_t -- how many observations the current segment holds, including y_t -- then has an exactly computable posterior: either the segment grows,

P(L_t = l + 1, y_1..t)  =  P(L_{t-1} = l, y_1..t-1) (1 - hazard) p(y_t | the l observations of the segment),

or a new one starts with y_t,

P(L_t = 1, y_1..t)  =  hazard p(y_1..t-1) p(y_t | prior).

Unlike CUSUM, nothing about the parameters before or after the change is assumed known: unknown means and variances, unknown rates. The output says how probable it is that a new segment began with this observation (change_probability), the most probable run length, and the evidence log p(y_1..y_t). With alarm_probability set, the procedure stops once the probability that a change happened within the last alarm_window observations reaches it.

Conjugate models: NormalGammaModel (normal data, unknown mean and variance; Student t predictive), PoissonGammaModel (counts; negative binomial predictive), BetaBernoulliModel (0/1 data).

The exact posterior has one hypothesis per possible run length, so it grows with the run. Hypotheses below prune probability are dropped and at most max_hypotheses are kept, the rest renormalised: the posterior and the evidence are then approximations (in the tests, dropping below 1e-12 changes the change probabilities by less than 1e-10; keeping 100 hypotheses for 200 observations, by 1e-5 and the log evidence by 1e-3; keeping 50, by 4e-4 and 0.02). prune = 0 and max_hypotheses = None keep the posterior exact.

Cost per step (PERFORMANCE.md): O(number of hypotheses kept), 0.3 ms after 100 observations and 4 ms with 2000 hypotheses; about 50 bytes per hypothesis in the checkpoint.

NormalGammaModel

Normal data with unknown mean and precision: mean | tau ~ N(mu0, 1/(kappa0 tau)), tau ~ Gamma(alpha0, beta0).

log_predictive(stats, y)

Student t with 2 alpha degrees of freedom, location mu, scale^2 = beta (kappa + 1) / (alpha kappa).

PoissonGammaModel

Counts with unknown rate: rate ~ Gamma(shape, rate).

log_predictive(stats, y)

Negative binomial: Gamma(r + y) / (Gamma(r) y!) (b / (b + 1))^r (1 / (b + 1))^y.

BetaBernoulliModel

0/1 data with unknown success probability ~ Beta(a, b).

BayesianChangePoint

Bases: Procedure

Bayesian online change-point detection with a conjugate model; see the module docstring.

run_length_distribution(state)

P(L_n = l | y_1..y_n) for the run lengths kept.