Where did the behaviour of a sequence actually change? A temperature sensor may shift after maintenance. A manufacturing process may drift into a new regime. A network metric may jump after a deployment. A student may look at a graph and say, “Something changed around here.” PELT turns that intuition into an exact penalised segmentation problem.
This article teaches Pruned Exact Linear Time change-point detection from beginner visual reasoning to professional offline signal analysis. It stays in the algorithm lane: the goal is to understand how a sequence is partitioned into segments, why dynamic programming is exact, when pruning is safe, and why the phrase “linear time” needs conditions rather than blind repetition.
Quick Read
- A change point marks a location where the statistical behaviour of a sequence changes.
- PELT solves a penalised optimisation problem: fit segments well, but charge a penalty for every added change point.
- The underlying recurrence is dynamic programming over all possible previous change locations.
- PELT adds a pruning rule that discards candidate previous change points that can no longer be optimal.
- The solution remains exact for the chosen cost and penalty; pruning changes the search effort, not the objective.
- Near-linear or linear expected behaviour arises under conditions described in the original analysis. Worst-case reasoning still matters.
- Professional use depends heavily on the cost model, penalty, minimum segment length, autocorrelation, outliers, trend and validation method.
1. Beginner Level: A Sequence with Regimes
Consider the sequence:
2.0, 2.1, 1.9, 2.0, 7.8, 8.1, 8.0, 7.9
The first four values are near 2 and the last four are near 8. A natural model has one change point between positions 4 and 5. But real data is noisy, and a sufficiently flexible model could place a change at almost every observation. We therefore need two forces:
- fit: segments should explain the data well;
- parsimony: unnecessary change points should cost something.
PELT balances them through a penalised objective.
2. The Objective: Segment Cost Plus Penalty
Suppose change points divide observations into segments. Let C(a, b) be the cost of modelling observations from index a up to b as one segment. For a simple change-in-mean model, C might be the sum of squared deviations from that segment’s mean.
If a segmentation contains m change points, a common objective is:
total cost = sum of segment costs + β × m
β is the penalty. A small penalty encourages more segments. A large penalty discourages changes. This already exposes one of the most important professional lessons: the algorithm does not magically decide what “important change” means. That meaning enters through the cost model, penalty and data assumptions.
3. Dynamic Programming Gives the Exact Optimum
Let F(t) be the minimum penalised cost for segmenting the first t observations. If the final segment begins immediately after position s, then the best solution ending at t is the best solution up to s plus the cost of the final segment plus the penalty.
A simplified recurrence is:
F(t) = min over s < t of:
F(s) + C(s + 1, t) + beta
If we evaluate every possible s for every t, the method is exact but can become quadratic in sequence length. PELT keeps the exact objective while trying to avoid carrying hopeless candidates forward.
4. What PELT Prunes
At each endpoint t, dynamic programming maintains candidate positions that could be the previous change point. PELT uses a mathematical inequality on segment costs to show that some candidates cannot become optimal later. Those candidates are removed permanently.
The key idea is general: do not merely search faster—prove that part of the search space is dominated and can never win. When the pruning condition holds, removing a candidate does not approximate the answer. It saves computation while preserving the exact optimum for the specified objective.
5. A Small Hand Calculation
Take six observations:
1, 1, 1, 5, 5, 5
Using squared error around each segment mean, the one-segment model has a mean of 3 and a relatively large total error. Splitting after the third observation produces two perfectly constant segments with zero within-segment squared error, but it pays one change penalty.
If β is smaller than the error saved by splitting, the two-segment solution wins. If β is larger, the one-segment solution wins. This is why “where are the change points?” cannot be separated from “what penalty defines a worthwhile extra segment?”
6. Pseudocode: Separate the Exact DP from the Pruning Idea
F[0] = -beta
candidates = {0}
for t in 1..n:
evaluate for every s in candidates:
value(s) = F[s] + cost(s, t) + beta
choose s_best with minimum value
F[t] = value(s_best)
remember s_best for backtracking
remove candidate s when the pruning inequality
proves that s cannot be optimal for any later endpoint
add t to candidates
backtrack from n to recover change points
Exact indexing and constants vary between formulations and implementations. When learning PELT, first understand the optimal-partitioning recurrence, then the pruning proof, then a library implementation. Trying to memorise one code listing before those three layers are clear usually produces fragile understanding.
7. Practical Python with ruptures
import numpy as np
import ruptures as rpt
rng = np.random.default_rng(7)
signal = np.concatenate([
rng.normal(0.0, 0.5, 200),
rng.normal(3.0, 0.5, 180),
rng.normal(-1.0, 0.5, 220),
])
algo = rpt.Pelt(
model="l2",
min_size=10,
jump=1,
).fit(signal)
breakpoints = algo.predict(pen=8)
print(breakpoints)
In the ruptures library, model="l2" is appropriate for a piecewise-constant mean under squared-error-style modelling. Other cost functions target different kinds of changes. min_size and jump also change the search domain, so they must be chosen deliberately rather than treated as decorative options.
8. Cost Functions Change the Question
- L2 / squared-error cost: useful for changes in mean under roughly Gaussian-style noise.
- L1-style costs: can reduce sensitivity to large residuals.
- Gaussian costs: can model changes involving variance or covariance assumptions.
- Autoregressive costs: can account for within-segment temporal dependence.
- Kernel costs: can target broader distributional changes without restricting the difference to a single simple parameter.
A learner who changes the cost function has changed the statistical question. PELT is the search strategy around that objective; it is not itself a universal definition of change.
9. Penalties: Under-Segmenting and Over-Segmenting
Too small a penalty can interpret random fluctuations as real structural changes. Too large a penalty can merge genuinely different regimes. Information-criterion-inspired penalties such as BIC or modified BIC are common in some models, but there is no universal penalty that is optimal for every signal and application.
For professional work, inspect a penalty path: run the detector across a reasonable range of β values and observe how the number and location of change points evolve. Stable change points that persist over a range can be more convincing than a single result produced by one arbitrary constant.
10. What “Linear Time” Really Means
The original PELT paper establishes linear computational cost under stated conditions, especially in settings where the number of change points grows appropriately with sequence length. That does not mean every dataset, every cost function and every implementation takes exactly O(n) time.
A careful engineer distinguishes:
- the worst-case search space;
- the theoretical conditions under which pruning gives linear expected behaviour;
- the cost of evaluating one segment;
- implementation choices such as subsampling candidate endpoints;
- the empirical runtime on representative data.
The current ruptures documentation makes the same point: complexity depends on both the number of observations and the cost of the segment model, and parameters such as min_size and jump can reduce computation by shrinking the candidate grid.
11. Offline Is Not Online
PELT is fundamentally an offline segmentation method: it sees the sequence being analysed and optimises a segmentation over it. That is different from online change detection, where an algorithm must raise an alarm as new observations arrive and cannot use future data.
This distinction matters in applications. Segmenting last month’s manufacturing data is an offline task. Detecting a fault within seconds of its occurrence is an online task. The statistical objective, latency requirement and evaluation metrics are different.
12. Failure Modes Strong Learners Should Recognise
- Trend mistaken for steps: a smooth drift may be approximated by many artificial change points.
- Seasonality ignored: periodic structure may trigger false changes.
- Autocorrelation ignored: dependent noise can make IID-based costs overconfident.
- Outliers: a few extreme points can create spurious segments under sensitive costs.
- Short regimes: minimum segment length may hide genuine brief events.
- Penalty instability: conclusions change dramatically under small β adjustments.
- Wrong cost model: detecting mean shifts when the real change is in variance or dependence.
- Post-selection storytelling: interpreting every detected boundary as a causal event without external evidence.
13. Validation Is a Separate Algorithmic Job
On simulated data, compare estimated change points with known ground truth. Useful metrics include absolute timing error, precision/recall within a tolerance window and Hausdorff-style distances between true and estimated breakpoint sets.
On real data, ground truth may not exist. Then use sensitivity analysis, held-out operational events, alternative cost models and domain evidence. A result that appears only under one penalty and vanishes under small modelling changes deserves less confidence than a stable boundary supported by independent evidence.
14. Professional Engineering Checklist
- Define the kind of change the cost function is supposed to detect.
- Scale or transform variables appropriately before multivariate segmentation.
- Choose a minimum segment length that matches the physical or business process.
- Explore penalties rather than hiding one unexplained constant in code.
- Benchmark runtime with realistic n, dimensionality and cost functions.
- Test robustness to outliers, missingness, trend and autocorrelation.
- Separate change detection from causal attribution.
- Record the exact cost, penalty, library version and preprocessing steps so results can be reproduced.
15. Learning Progression: Beginner to Professional
- Beginner: identify obvious regime changes by eye and compute segment means.
- Intermediate: implement optimal partitioning without pruning for a short sequence and inspect the DP table.
- Advanced: study PELT’s pruning inequality and compare candidate-set size over time.
- Professional: build a validation harness across penalties, costs, synthetic regimes, outliers and autocorrelated noise; then benchmark against the actual application’s error costs.
16. Practice Problems
- For the sequence 1,1,1,5,5,5, compute the one-segment squared error and compare it with a split after index 3.
- Increase β gradually and record how many change points remain.
- Create a sequence with a smooth linear trend but no step change. Observe what happens under an L2 piecewise-constant model.
- Add one extreme outlier and compare L2 with a more robust cost.
- Measure candidate-set size at each PELT iteration on several synthetic signals.
- Compare PELT with full dynamic programming on small n and verify that both return the same optimum for the same objective.
- Generate autocorrelated noise and test whether an IID cost produces too many changes.
17. Sources and Further Reading
- Killick, Fearnhead & Eckley (2012), “Optimal Detection of Changepoints With a Linear Computational Cost”.
- Author preprint of the PELT paper.
- ruptures documentation: PELT.
- ruptures user guide for offline change-point algorithms and cost models.
- PRIMM: structured programming pedagogy.
- Worked examples and metacognitive scaffolding in programming education.
Final idea: PELT is valuable not because “linear” sounds fast, but because it demonstrates a deeper algorithmic move: formulate the exact optimisation problem first, derive the dynamic program, and then prune only what mathematics proves cannot matter. Speed follows from justified elimination, not from guessing less carefully.
