You're analysing years of market returns, and one set of assumptions may not describe the whole period well. Quiet stretches and more turbulent ones can have different statistical behaviour. Changepoint detection gives us a way to look for the boundaries between such periods in a historical series. Those boundaries can help us examine the data in segments instead of treating every observation as part of one regime.
Those boundaries change which observations a fitted model must explain. We compare penalties with Pruned Exact Linear Time (PELT), which finds 45 changepoints over 41 years at penalty 20. Their alignment with known events helps interpret the segmentation, while the penalty controls how many boundaries the fit supports.
By the end of this post, you'll detect volatility regime shifts in S&P 500 data using PELT, compare it with Binary Segmentation, understand how the penalty parameter controls the trade-off between sensitivity and false positives, and see how detected changepoints align with real financial crises. We use PELT here because it finds the globally optimal set of changepoints in linear time and works offline on historical data, which is ideal for retrospective analysis of decades of returns. For real-time, streaming applications, Bayesian online changepoint detection updates regime beliefs as each new observation arrives.
The Data: 41 Years of S&P 500 Returns
We use daily S&P 500 data from January 1985 to April 2026: over 10,000 trading days covering Black Monday, the dot-com bubble, the Global Financial Crisis, COVID, the 2022 rate hike sell-off, the 2025 Liberation Day tariffs, and the 2026 Iran war. This is the same index used in the original R analysis we're translating, extended to cover the full modern era.

The colour-coded background shows volatility regimes detected by PELT: green for calm periods, red/yellow for turbulent ones. The algorithm picks up every major crisis without being told when they occurred.
Quick Win: Detecting Regime Shifts
Click the badge to run this yourself:
import numpy as np
import pandas as pd
import yfinance as yf
import ruptures as rpt
# Download 41 years of S&P 500 data
sp = yf.download("^GSPC", start="1984-12-25",
auto_adjust=True, progress=False)
sp = sp.loc["1985-01-02":]
close = sp["Close"].squeeze()
# Compute log returns
log_ret = np.log(close / close.shift(1)).dropna()
signal = log_ret.values.reshape(-1)
print(f"{len(signal)} trading days, {log_ret.index[0].date()} to {log_ret.index[-1].date()}")
10392 trading days, 1985-01-03 to 2026-04-02
Now the key part: fit PELT and detect changepoints.
# PELT with normal model (detects mean + variance shifts)
algo = rpt.Pelt(model="normal", min_size=10, jump=5).fit(signal)
breakpoints = algo.predict(pen=20)
n_changepoints = len(breakpoints) - 1
print(f"Detected {n_changepoints} changepoints")
Detected 45 changepoints
PELT found 45 points where the statistical character of daily returns shifted. Each one marks the boundary between two volatility regimes.
What Just Happened?
The Changepoint Problem
A changepoint is a point in time where the data-generating process changes. Before the changepoint, the data follows one distribution (say, low-volatility returns). After it, the data follows a different distribution (high-volatility returns). The goal is to find all such points, given only the data.
Formally, we're partitioning the time series into segments such that the data within each segment is statistically homogeneous, but adjacent segments differ.
The Cost Function Approach
Changepoint detection frames this as an optimisation problem. Define a cost function that measures how well a single distribution fits the data from time to time . For the normal model, this is the negative log-likelihood:
where is the sample variance of the segment. Low-variance segments have low cost; segments that mix two regimes have high cost because the combined variance is inflated.
The total cost for a segmentation with changepoints is:
where is the penalty per changepoint. Without the penalty, the optimal solution would put a changepoint at every observation. The penalty forces the algorithm to justify each additional changepoint by a sufficient reduction in cost.
Reading the Output
The predict() call returns a list of indices where the data shifts. Each index marks the end of a segment. The last element is always (the end of the series). So 45 changepoints means 46 segments, each with its own statistical character.

The regime colouring makes the structure visible at a glance. The 2008 financial crisis, COVID crash, and Black Monday are obvious high-volatility regimes (red). The long calm periods of the mid-1990s and 2013-2017 are green. The algorithm finds this structure purely from the data, with no knowledge of financial history.
Rolling Windows and Their Limits
A rolling standard deviation can show volatility trends, but it has two problems. First, it smears the boundary: the window gradually incorporates new-regime data, so the transition is blurred over the window length. Second, you have to choose the window size, which is itself a regime-dependent parameter. Changepoint detection gives you sharp boundaries and determines the regime structure from the data itself.
Going Deeper
PELT: Linear-Time Exact Changepoint Detection
The naive approach to finding optimal changepoints is where is the number of changepoints and is the series length. For 10,000 data points, this is impractical.
Killick, Fearnhead, and Eckley (2012) introduced PELT (Pruned Exact Linear Time), which achieves exact optimality with average complexity. The trick is a pruning rule: at each step, PELT checks whether a candidate changepoint can ever be part of the optimal solution. If not, it's discarded permanently. Under mild conditions (the penalty is at least logarithmic in ), most candidates are pruned early, giving linear runtime.
# PELT is the default and best choice for most problems
algo = rpt.Pelt(model="normal", min_size=10, jump=5).fit(signal)
breakpoints = algo.predict(pen=20)
The jump=5 parameter means PELT considers every 5th index as a candidate changepoint. This trades a tiny amount of precision for a 5x speed improvement on long series. For 10,000+ points, this is a practical necessity.
Binary Segmentation: The Fast Approximation
Binary Segmentation (Scott and Knott, 1974) takes a different approach: find the single best changepoint, split the data there, then recursively find the best changepoint in each half. It's but only approximate: it can miss changepoints that are only visible when nearby changepoints are already accounted for.
algo_bs = rpt.Binseg(model="normal", min_size=10, jump=5).fit(signal)
bkps_bs = algo_bs.predict(n_bkps=45) # fix the number of changepoints

With the same number of changepoints, PELT and Binary Segmentation produce similar but not identical segmentations. The key differences show up in transition periods (like 2001-2003) where PELT, being globally optimal, finds the true boundaries while BinSeg may misplace them by a few weeks.
The Penalty: Balancing Sensitivity and Specificity
The penalty parameter is the single most important choice. Too low, and you detect hundreds of spurious changepoints (noise). Too high, and you miss real regime shifts.

At , PELT finds over 700 changepoints (roughly one every two weeks). At , it finds only 8. Our choice of gives 45 changepoints, which aligns well with known financial events.
Common penalty selection methods:
| Method | Formula | When to use |
|---|---|---|
| BIC | Default for most problems; is the number of parameters per segment | |
| AIC | More liberal (more changepoints) | |
| Manual | Domain-specific | When you know roughly how many regimes to expect |
For 10,392 data points with a normal model (), BIC gives , which is close to our .
Regime Duration
The distribution of regime durations tells us something about market microstructure:

The median regime lasts about 158 trading days (~7 months), but the distribution is heavily right-skewed: a few calm regimes persist for years (the longest is 1,000 trading days, roughly 4 years), while crisis regimes tend to be short and sharp. The mean (226 days) exceeds the median because of these long calm tails.
Changepoints Aligned with Financial Events
The real test of a changepoint method is whether it finds meaningful structure. Here's PELT with a slightly higher penalty (, 24 changepoints) overlaid with known financial events:

The algorithm detects Black Monday (7 days early), the Lehman collapse (11 days early), the COVID crash (3 days early), and the 2025 Liberation Day tariffs (on the exact day). It "detects" these events early because the volatility regime actually shifts before the headline event. Markets often become turbulent in the days before the crash itself, as informed participants reposition.
The Iran war (February 2026) is labelled on the chart but PELT does not flag it as a separate regime change. By that point, the market was already in the turbulent regime triggered by Liberation Day tariffs, and the additional volatility from the conflict did not constitute a statistically distinct shift. For an online, real-time approach to the same problem, see our Bayesian online changepoint detection post, which reaches the same conclusion.
The Exponential Distribution Connection
Squared returns are a natural proxy for variance. If log returns are normally distributed with variance , then squared returns follow a scaled chi-squared distribution with one degree of freedom, which is well-approximated by an exponential. This means we can also frame changepoint detection as finding shifts in the rate parameter of an exponential distribution fitted to squared returns.

The exponential fit captures the overall shape. The histogram is dominated by near-zero values (calm days), with a long right tail of extreme moves (crises). Changepoints in the rate parameter of this exponential correspond to shifts between low-variance and high-variance regimes.
Changepoints vs HMMs: Hard Boundaries vs Soft Probabilities
Our HMM post tackled regime detection from a different angle: hidden Markov models assign each time step a probability of being in each regime, with smooth transitions. Changepoint detection gives hard boundaries with no transition period.
| Feature | Changepoint (PELT) | HMM |
|---|---|---|
| Boundaries | Hard (exact day) | Soft (probability of each regime) |
| Regime count | Penalty-controlled | Fixed by model specification |
| Stationarity | Assumes segments are stationary | Allows within-regime dynamics |
| Speed | with PELT | where = states |
| Best for | Post-hoc analysis, segmentation | Online filtering, forecasting |
Use PELT when you want to segment historical data into clean periods (for backtesting, risk measurement, or training separate models per regime). Use HMMs when you need real-time regime probabilities or when transitions are gradual.
Hyperparameter Choices
| Parameter | Value | Why |
|---|---|---|
| Data | S&P 500, 1985-2026 | Original R code index; 41 years covering major crises |
| Returns | Log returns | Standard in academic finance; continuous, symmetric |
| Model | normal | Detects changes in both mean and variance |
| Penalty | 20 | Close to BIC for n=10392; gives 45 changepoints aligned with events |
| min_size | 10 | Minimum 2 weeks between changepoints; avoids spurious detections |
| jump | 5 | Checks every 5th candidate; 5x speedup with negligible precision loss |
Where This Comes From
Killick, Fearnhead, and Eckley (2012): The PELT Paper
Rebecca Killick, Paul Fearnhead, and Idris Eckley introduced PELT in their 2012 paper "Optimal Detection of Changepoints with a Linear Computational Cost", published in the Journal of the American Statistical Association.
The key insight is a pruning inequality. At time , if adding a changepoint at some earlier time can never improve on the best segmentation up to , then can be permanently removed from consideration. Formally, if:
where is the optimal cost up to time , then can never be an optimal last changepoint for any future time . Under the condition that the cost function satisfies a "sufficient change" property, the number of candidates that survive pruning grows linearly with .
"The resulting method, which we call PELT, is shown through simulation studies to be substantially more accurate than existing methods while being computationally more efficient." -- Killick, Fearnhead & Eckley (2012)
The Broader Landscape
Changepoint detection has a long history. Scott and Knott (1974) introduced Binary Segmentation for grouping means in ANOVA. Bai and Perron (2003) developed efficient algorithms for multiple structural breaks in econometric models. Truong, Oudre, and Vayatis (2020) provide a comprehensive review of offline changepoint methods in their paper accompanying the ruptures library.
The field continues to evolve. Recent work includes Bayesian online changepoint detection (Adams and MacKay, 2007) for streaming data, and kernel-based methods that detect distributional changes beyond mean and variance.
Further Reading
- The PELT paper: Killick, Fearnhead & Eckley (2012), "Optimal Detection of Changepoints with a Linear Computational Cost", JASA
- Binary Segmentation: Scott & Knott (1974), "A Cluster Analysis Method for Grouping Means in the Analysis of Variance"
- Comprehensive review: Truong, Oudre & Vayatis (2020), "Selective Review of Offline Change Point Detection Methods"
- Econometric perspective: Bai & Perron (2003), "Computation and Analysis of Multiple Structural Change Models"
- Python implementation: ruptures documentation
Interactive Tools
- Drawdown Calculator: Analyse portfolio drawdowns that often coincide with the regime shifts detected here
- Normal Distribution Calculator: Compare the Gaussian return distributions across calm and turbulent regimes
- Chi-Square Goodness-of-Fit Test: Formally test whether observed returns in a proposed regime match a reference distribution
- Black-Scholes Calculator: See how volatility regime changes feed directly into option pricing
Related Posts
- Hidden Markov Models: When Clusters Have Memory (soft regime detection via HMMs)
- Cointegration Test for Pairs Trading in Python: Engle-Granger and Johansen (another time-series structural test)
- MCMC for Mixture Models: Inferring Earthquake Regimes (Bayesian regime detection in count data)
- From K-Means to GMM: Hard vs Soft Clustering (the hard vs soft boundary theme generalised)
- Bayesian Online Changepoint Detection in Python (the real-time, online companion to PELT on the same S&P 500 data)
- Fourier Transform for Time Series Forecasting (frequency-domain view of the same nonstationary series)
- Modern Portfolio Theory from Scratch: The Efficient Frontier (the regime shifts that break the stationary covariance a static efficient frontier assumes)
- Five Markets, 22 Years: Every Crisis from Price Data (Bayesian online changepoint detection across multiple asset classes)
- Cox Proportional Hazards in Python with lifelines (the hazard-function view of time-to-event, a complementary lens on when a regime ends)
- Time Series as Dynamical Systems: Delay Embedding (the state-space view of the same nonstationary series: reconstruct the attractor rather than segment the regimes)










