Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 40 ยท Anomalies and Applications

Change Point Detection

Detect structural breaks where a series changes level, trend or variance: CUSUM, a from-scratch binary segmentation with a penalty, rolling comparisons and what to do with the breaks.

An anomaly is a short-lived oddity; a change point is a moment where the process itself changes and stays changed. A product launch lifts sales permanently, a firmware update shifts a sensor's baseline, a pricing change alters the weekly pattern. Detecting these structural breaks matters for two reasons: they are often the event you want to know about, and they break forecasting models trained on the older regime.

Change points come in different flavours:

  • Mean (level) shift: the average jumps.
  • Trend change: the slope changes.
  • Variance change: the series becomes more or less volatile.
  • Seasonality change: the pattern itself changes.

This lesson focuses on level and variance changes, the most common, and shows two approaches you can write in a few lines of numpy, so you see what dedicated libraries do.

CUSUM: cumulative sum of deviations

If you subtract the overall mean and add up the deviations, the cumulative sum wanders near zero for a stable series. After a level shift it ramps in one direction, and the largest absolute value of the CUSUM marks the most likely break point.

Python CUSUM finds one level shift

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

CUSUM is simple and fast, and it is the basis of classic process-control charts. By itself it finds a single break. To find several, split the series and search each side again.

Binary segmentation with a penalty

A principled approach: for every candidate split, compare the cost of describing the segment with one mean against two means (the sum of squared deviations). Accept the best split if the gain beats a penalty, then recurse on each side. This is binary segmentation, the engine behind many change point libraries.

Python Binary segmentation from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The penalty controls sensitivity. A small penalty finds many tiny breaks (over-segmentation), a large one finds only dramatic shifts. A common choice is proportional to log(n) times the noise variance (a BIC-like rule); the best value is usually found by looking at results on data where you know the answer.

A series with no break

Always test the method on data with no change, to see how many false breaks it reports.

Python False alarms on a stable series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a penalty of 5, almost a third of the stable series produce a false break; at 20 there are none. A rule of thumb for unit-variance noise is a few times log(n) x variance (here log(300) is about 5.7, so 17 to 25). The segmentation example above uses 60 as a safety margin, because real noise is rarely perfectly normal, and genuine shifts of a few standard deviations still clear it easily.

Variance changes

A change in volatility leaves the mean alone, so apply the same idea to squared deviations (or absolute values).

Python Detecting a volatility change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Online monitoring

Everything above works on a complete history (offline). To catch changes as they happen, compare recent data with a reference window, or use a one-sided CUSUM that raises an alarm when the running sum of deviations above a slack value crosses a threshold. The trade-off is detection delay against false alarms: a faster alarm means more false ones.

What to do with a detected break

  • Forecasting: train on the data after the latest break (or add a level-shift dummy as an ARIMAX regressor); the old regime may mislead the model.
  • Interpretation: line breaks up with known events (releases, campaigns, outages) and look for an explanation before acting.
  • Anomalies versus breaks: a spike that reverts is an anomaly (see anomaly detection); a shift that persists is a change point. Check a window after the candidate to tell them apart.
  • Libraries: ruptures (PELT, binary segmentation, window methods) and Bayesian online change point detection are the usual tools outside this site's browser runner; the from-scratch code above shows the principle.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between an anomaly and a change point?

    After a change point the series stays in a new regime, while an anomaly reverts to normal.
  2. 2. Where does the CUSUM of mean-centred data usually reach its extreme?

    The cumulative deviations ramp one way after a shift, so the largest absolute value marks the break.
  3. 3. What does raising the penalty in binary segmentation do?

    A split must gain more than the penalty to be accepted, so higher penalties accept fewer splits.
  4. 4. Why test a detector on a series with no change?

    A detector that reports breaks in stable data cannot be trusted; the penalty must keep false alarms rare.

Summary

  • A change point is a lasting change in level, trend, variance or seasonality; it differs from a transient anomaly.
  • CUSUM finds one break; binary segmentation with a penalty finds several.
  • The penalty trades sensitivity for false alarms; calibrate it on stable data.
  • Variance changes use the same idea on squared deviations.
  • After a break, retrain on the new regime or add a level-shift regressor.

Next: Project: Retail Demand Forecast