milia.anomaly.changepoint#
Change-point detection: when did a meter’s behaviour change?
Functions
|
Binary segmentation into a fixed number of changes through ruptures. |
|
Two-sided Page CUSUM change points on a standardised series. |
|
Exponentially weighted moving average of many standardised series, the alternative to |
|
The point of each series' first exceedance of a decision limit. |
|
Decision limit calibrated on series known not to change: the conformal quantile of their largest statistic. |
|
One-sided CUSUM statistic of many series at once, each standardised by its own baseline. |
|
Optimal segmentation by PELT (Killick, Fearnhead and Eckley, 2012) through ruptures. |
- milia.anomaly.changepoint.cusum(series, threshold=5.0, drift=0.5, baseline=None)#
Two-sided Page CUSUM change points on a standardised series.
The series is standardised by the mean and standard deviation of its first
baselinepoints (a quarter of the series by default); an alarm fires when either cumulative sum exceedsthreshold, and both sums reset. Needs no optional dependency.- Parameters:
- Return type:
- Returns:
Indices at which an alarm fired.
Examples
>>> series = np.r_[np.zeros(50), np.full(50, 5.0)] + np.random.default_rng(0).normal(size=100) >>> first = int(cusum(series, threshold=8.0)[0]) >>> 45 <= first <= 55 # the change is at index 50 True
- milia.anomaly.changepoint.pelt(series, penalty=10.0, model='rbf', minimum=2)#
Optimal segmentation by PELT (Killick, Fearnhead and Eckley, 2012) through ruptures.
- Parameters:
- Return type:
- Returns:
Indices where a new segment starts (the series end excluded).
Examples
>>> series = np.r_[np.zeros(50), np.full(50, 5.0)] >>> pelt(series, model="l2").tolist() [50]
- milia.anomaly.changepoint.binseg(series, changes=1, model='l2')#
Binary segmentation into a fixed number of changes through ruptures.
- Parameters:
- Return type:
- Returns:
Indices where a new segment starts (the series end excluded).
Examples
>>> binseg(np.r_[np.zeros(50), np.full(50, 5.0)]).tolist() [50]
- milia.anomaly.changepoint.paths(series, baseline, drift=0.5)#
One-sided CUSUM statistic of many series at once, each standardised by its own baseline.
Each row is standardised by its mean and standard deviation over its first
baselinepoints, and the statistic \(S_t = \max(0, S_{t-1} + z_t - k)\) is accumulated from the point after the baseline (Page, 1954). Unlikecusum(), the statistic is returned rather than reset at an alarm, so that a decision limit can be calibrated on it (pathlimit()).- Parameters:
- Return type:
- Returns:
The statistic, shape
(series, points - baseline); columntis pointbaseline + t.
Examples
>>> rng = np.random.default_rng(0) >>> series = rng.normal(1.0, 0.1, (2, 40)) >>> series[1, 25:] += 0.3 >>> statistic = paths(series, baseline=12) >>> statistic.shape, bool(statistic[1].max() > statistic[0].max()) ((2, 28), True)
- milia.anomaly.changepoint.firstalarms(statistic, limit, baseline=0)#
The point of each series’ first exceedance of a decision limit.
- Parameters:
- Return type:
- Returns:
The point of the first alarm per series, or
-1for a series never alarmed.
Examples
>>> firstalarms(np.array([[0.0, 2.0, 6.0], [0.0, 1.0, 2.0]]), limit=5.0, baseline=10).tolist() [12, -1]
- milia.anomaly.changepoint.pathlimit(statistic, alpha=0.1)#
Decision limit calibrated on series known not to change: the conformal quantile of their largest statistic.
A series exchangeable with the calibration series, and without a change, crosses the limit within the horizon with probability at most about
alpha. This replaces the nominal limit of a CUSUM, whose run length assumes independent observations with a known spread, by one that holds for the series at hand.- Parameters:
- Return type:
- Returns:
The calibrated limit.
Examples
>>> statistic = np.arange(20.0)[:, None] * np.ones((1, 3)) >>> pathlimit(statistic, alpha=0.1) 18.0
- milia.anomaly.changepoint.ewmapaths(series, baseline, smoothing=0.2)#
Exponentially weighted moving average of many standardised series, the alternative to
paths().Each row is standardised by its baseline as in
paths(), and the statistic \(E_t = \lambda z_t + (1 - \lambda) E_{t-1}\) starts at zero after the baseline (Roberts, 1959). It weighs recent points more and forgets old ones, so it reacts to a small shift more slowly than the CUSUM but recovers after a transient one.- Parameters:
- Return type:
- Returns:
The statistic, shape
(series, points - baseline); compare it withpathlimit()as forpaths().
Examples
>>> rng = np.random.default_rng(0) >>> series = rng.normal(1.0, 0.1, (2, 40)) >>> series[1, 25:] += 0.3 >>> statistic = ewmapaths(series, baseline=12) >>> bool(statistic[1, -1] > statistic[0, -1]) True