PageHinkley: the same idea, units do not matter¶

PageHinkley compares every value with the average of the stream so far, measured in standard deviations, and adds up how far the values stay above it. Because the comparison is in standard deviations, the same settings work whether the stream is a cost in dollars or a tiny squared error.
First example¶
import numpy as np
from dense_armor.core.online.drift.page_hinkley import PageHinkley
rng = np.random.default_rng(0)
x = list(rng.normal(0, 1, 500)) + list(rng.normal(3, 1, 500))
for scale in (1.0, 1e-4):
det = PageHinkley(delta=0.05, threshold=20.0)
print(scale, next(i for i, v in enumerate(x) if det.update(scale * v).drift_detected))
In this example the first 500 values are normal noise, for instance the daily cost of an order after removing its usual level, and from the value 500 the mean moves up by 3 standard deviations. The detector fires at the value 506 in both runs, once with the signal as it is and once with the signal multiplied by \(10^{-4}\). The alarm does not move, because the test works in standard deviations.
How it works¶
The statistic is Page's cumulative sum. Each value is compared with the running mean, divided by the running standard deviation, and the accumulated deviation, minus the slack \(\delta\), is compared with its own running minimum:
\(\bar x_i\) and \(\sigma_i\) are the mean and the standard deviation of all the values since the start, not of a window. The first warmup values only feed them: the test starts after that. With two_sided=True a second total, with the sign changed, watches the falls. Xie et al. (2021) write the same construction as equation 2, with the log-likelihood ratio in place of the standardized deviation.
An alarm restarts only the total that fired. The mean and the standard deviation are kept, so a step that lasts keeps firing:
import numpy as np
from dense_armor.core.online.drift.page_hinkley import PageHinkley
rng = np.random.default_rng(0)
x = list(rng.normal(0, 1, 500)) + list(rng.normal(3, 1, 500))
det = PageHinkley(delta=0.05, threshold=20.0)
alarms = [i for i, v in enumerate(x) if det.update(v).drift_detected]
print(len(alarms), alarms[:4])
The same step gives 29 alarms in the 500 values after it, the first at 506, and none before it. To get one event per change, act on the first alarm, or start a new detector after it.
With the default settings, delta = 0.005 and threshold = 50, the first alarms on the same stream are later:
import numpy as np
from dense_armor.core.online.drift.page_hinkley import PageHinkley
rng = np.random.default_rng(0)
x = list(rng.normal(0, 1, 500)) + list(rng.normal(3, 1, 500))
det = PageHinkley()
print([i for i, v in enumerate(x) if det.update(v).drift_detected][:3])
Both directions¶
A fall of 3 standard deviations instead of a rise:
import numpy as np
from dense_armor.core.online.drift.page_hinkley import PageHinkley
rng = np.random.default_rng(0)
x = list(rng.normal(0, 1, 500)) + list(rng.normal(-3, 1, 500))
for two in (True, False):
det = PageHinkley(delta=0.05, threshold=20.0, two_sided=two)
print(two, next((i for i, v in enumerate(x) if det.update(v).drift_detected), None))
With two_sided=True the second total catches the fall at 504; with two_sided=False only the rises are watched, so there is no alarm (None). A value that is not a number does not move the totals: n_missing counts it.
The parameters¶
| Parameter | Default | What it does |
|---|---|---|
delta |
0.005 | the slack, in standard deviations; the classical choice is half the shift worth detecting |
threshold |
50.0 | the alarm threshold on the total above its running minimum, in standard deviations |
two_sided |
True |
if False, only rises are watched |
warmup |
30 | values before the first check; they feed the running mean and standard deviation |
For the same idea with a reference that is measured once and with a slack in the units of the noise see CUSUMDriftDetector.
API reference¶
page_hinkley ¶
Page-Hinkley test for a change in the mean of a scalar stream.
Page, E. S. (1954). Continuous inspection schemes. Biometrika 41(1-2), 100-115. The formulation used here is the log-likelihood-ratio CUSUM written down in Xie, Zou, Xie, Veeravalli (2021), "Sequential (Quickest) Change Detection: Classical Results and New Directions", Section II-C2: the log-likelihood ratio l(X) is accumulated and compared with its running minimum (eq. 2),
W_n = S_n - min_{0 <= k <= n} S_k, S_n = sum_{k=1}^{n} l(X_k),
and the detector alarms when W_n exceeds a threshold b (eq. 3, the maximum-likelihood form). With a Gaussian reference of unit variance the log-likelihood ratio reduces to (x - mu_ref) / sigma_ref - delta, where mu_ref is the running mean, sigma_ref the running standard deviation of the stream, and delta is the slack in scale units. Working in scale units makes delta and threshold independent of the stream units, so the same parameters detect the same change on x and on 1e-4 * x.
PageHinkley ¶
PageHinkley(
delta: float = 0.005,
threshold: float = 50.0,
two_sided: bool = True,
warmup: int = 30,
)
Bases: DriftDetector
Page-Hinkley test for a change in the mean of a scalar stream.
The running mean and the running standard deviation are those of all the
samples since the start, not of a window. After a flag only the total that
fired (and its minimum) restarts: the mean and the standard deviation are
kept, so a step that lasts keeps flagging every few samples (29 flags in
the 500 samples after a step of 3 standard deviations, with
delta=0.05, threshold=20). A value that is not finite is skipped and
counted in n_missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delta
|
float
|
reference slack, in scale units (multiples of the
running standard deviation). Classical choice: half the
shift size worth detecting. Default |
0.005
|
threshold
|
float
|
alarm threshold on the accumulated deviation from
its running minimum, in scale units. Default |
50.0
|
two_sided
|
bool
|
if |
True
|
warmup
|
int
|
number of samples before the first check. They are not
ignored: they feed the running mean and standard deviation.
Default |
30
|
Examples:
>>> from dense_armor.core.online.drift.page_hinkley import PageHinkley
>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> det = PageHinkley(delta=0.05, threshold=20.0)
>>> stream = list(rng.normal(0, 1, 500)) + list(rng.normal(2, 1, 500))
>>> for i, x in enumerate(stream):
... _ = det.update(x)
... if det.drift_detected:
... print(i)
... break
508
Source code in dense_armor/core/online/drift/page_hinkley.py
Details¶
- Source. Page, E. S. (1954), "Continuous inspection schemes", Biometrika 41(1-2), 100-115, for the cumulative sum. Hinkley, D. V. (1971), "Inference about the change-point from cumulative sum tests", Biometrika 58(3), 509-523, who turned Page's chart into an estimator of when the change happened. The formulation of the code is the one of Xie, Zou, Xie, Veeravalli (2021), Sequential (quickest) change detection: classical results and new directions, Section II-C2, equations 2 and 3.
- What is the choice of the library. Dividing by the running standard deviation is the choice of this library, so that the settings do not depend on the units of the stream.