Recommended resources
The freely available resources this lesson will direct you to.
Welcome to our next lesson. In our previous session, we explored how to encode predictable, cyclical time patterns using sine and cosine transformations. We learned to represent time as a circle, ensuring our models understand the continuous nature of features like the hour of the day or month of the year. Now, we turn our attention from predictable cycles to unpredictable shocks: the sudden, abrupt shifts that fundamentally alter the behavior of a time series.
These shifts, known as structural breaks or regime changes, are critical moments. In finance, a market crash marks a transition from a low-volatility to a high-volatility regime. In system monitoring, a sudden increase in CPU usage can signal a new failure state. Ignoring these breaks forces a model to learn an "average" behavior that doesn't truly represent any of the underlying states.
This lesson will equip you with the tools to detect these events automatically. Our goal is to apply statistical methods, specifically the powerful PELT algorithm, to identify structural breaks in a time series. We will then explore how to transform these detected breakpoints into meaningful features that allow your models to adapt to changing environments.
When the Rules of the Game Change
Standard time series decomposition, which breaks a series into trend, seasonality, and noise, operates under an assumption of stability. As long as the underlying system behaves as expected, this decomposition is useful for forecasting. But what happens when a disruptive event occurs?
The following video provides an excellent high-level perspective on this problem, using compelling examples from finance and even personal life to illustrate why standard models can be "violently wrong" when a structural break occurs.
Time Series Analysis for Quant Finance
This video from Roman Paolucci's channel on quantitative finance provides a fantastic intuition for why structural breaks invalidate standard time series models.
Watch the segment from big regime changes, where the narrator explains that major shifts are not accounted for in typical trend-seasonality models. Then, jump to the section from the earnings event, which shows how a forecast becomes useless after an earnings surprise. Finally, consider the analogies presented in the bench press injury and the 2008 financial crisis. These examples powerfully illustrate the concept of a "data generating distribution" changing completely.
The key takeaway is that after a structural break, the underlying data-generating process itself has changed. The model needs to be aware that it's now operating under a new set of rules. Our job as feature engineers is to explicitly tell the model when these rule changes happen.
The Changepoint Detection Problem
A structural break is formally known as a changepoint. A changepoint is a point in time where the statistical properties of a time series—such as its mean, variance, or underlying trend—abruptly change. The goal of changepoint detection is to identify the locations of these points.

To understand the different kinds of changes we might look for, the following lecture notes provide a clear, formal introduction.
[PDF] MATH337: Changepoint Detection
These notes from a university course on changepoint detection offer precise definitions and visualizations of the core concepts.
Please read the section titled Introduction to changepoints (up to the start of "1.2.2 The biggest data challenge..."). Pay close attention to the visual examples of a change in mean, a change in variance, and a change in regression slope.
How do we find these points algorithmically? Most modern methods frame this as an optimization problem based on a cost function.
Imagine we're trying to segment a time series .
- We define a cost function, , which measures how well a single statistical model (e.g., a single mean and variance) fits the data segment from time to . A segment with stable properties will have a low cost, while a segment containing a changepoint will have a high cost.
- The total cost for a segmentation with changepoints is the sum of the costs of all segments, plus a penalty for each changepoint we introduce: Here, are the changepoint locations and is the penalty parameter.
- The goal is to find the number and locations of changepoints that minimize this total cost. The penalty is crucial; without it, the algorithm would simply declare every single data point a changepoint to achieve a cost of zero, which is a classic case of overfitting.
This approach is elegantly explained in the following blog post, which we will use as a primary guide.
Changepoint Detection: Finding Regime Shifts in Financial ...
This article from sesen.ai provides a superb, practical overview of changepoint detection, focusing on the PELT algorithm and its application to financial data.
First, read the introduction from the beginning to get a feel for the problem's importance. Then, dive into the section What Just Happened?, which formally defines the cost function approach and the role of the penalty parameter.
Algorithms for Finding Changepoints
With the optimization problem defined, how do we solve it? A brute-force search is computationally infeasible. Over the years, several efficient algorithms have been developed.
From CUSUM to General Tests
The earliest methods, like the Cumulative Sum (CUSUM) statistic, were designed to find a single change in mean. CUSUM works by iterating through all possible split points and finding the one that maximizes the standardized difference between the means of the two resulting segments. This idea can be generalized through the Likelihood Ratio Test (LRT) framework, which compares the likelihood of the data under a "no changepoint" model versus a "one changepoint" model. The math notes from LINK (
Multiple Changepoints: Greedy vs. Optimal
When multiple changepoints exist, things get more complex.
- Binary Segmentation (BinSeg): This is a greedy, "divide and conquer" algorithm. It first finds the single best changepoint in the entire series, splitting it in two. It then recursively repeats this process on the two new sub-segments until no more significant changepoints are found. It's fast (typically ), but because it makes local decisions, it's not guaranteed to find the true, globally optimal segmentation.
- Optimal Partitioning (OP): This dynamic programming approach is guaranteed to find the globally optimal solution that minimizes the penalized cost function. However, its computational complexity is , making it too slow for large datasets.
- Pruned Exact Linear Time (PELT): This is the state-of-the-art and the algorithm you should reach for first. PELT is a breakthrough because it finds the exact same optimal solution as OP but does so with an average complexity of — linear time. It achieves this through a clever pruning step. At each point in time , it proves that certain past candidate changepoints can never be part of any future optimal solution, and thus safely removes them from consideration. Your background in algorithms should give you an appreciation for the elegance of this optimization.
The sesen.ai article provides an excellent comparison of these methods.
Changepoint Detection: Finding Regime Shifts in Financial ...
Let's continue with the sesen.ai article to understand the differences between the main algorithms.
Read the section Going Deeper, focusing on the descriptions of PELT and Binary Segmentation. This will solidify why PELT is generally the superior choice.
Practical Implementation with ruptures
Let's now translate this theory into practice using the ruptures library in Python, a go-to tool for changepoint detection. The following video demonstrates its use in a meteorological context, but the principles are universal.
MetPy Mondays #247 - Change Point Detection with Ruptures
This MetPy Mondays tutorial provides a concise and clear demonstration of the ruptures library.
Watch from the beginning to get an introduction to the library. Then, focus on the code demonstration from applying PELT. Notice the scikit-learn-like API with .fit() and .predict() methods. The key parameter in the .predict() call is pen, which corresponds to our penalty \beta.
As you saw, the core workflow is straightforward:
- Instantiate an algorithm object, like
rpt.Pelt(). You specify amodelargument (e.g.,"l2"for change in mean,"normal"for change in mean/variance, or"rbf"for more general changes). - Fit the algorithm to your time series data:
algo.fit(signal). - Predict the changepoint locations using a penalty value:
result = algo.predict(pen=...).
The result is a list of integers representing the indices after which a change occurs.

The Crucial Role of the Penalty Parameter
The single most important decision you'll make is choosing the penalty value.
- Too low: The model will be oversensitive and detect many spurious changepoints in the noise.
- Too high: The model will be undersensitive and miss real, subtle regime shifts.
There is no single magic formula, but a common and well-justified starting point is the Bayesian Information Criterion (BIC) penalty, which for many common models is , where is the number of data points and is the number of parameters that change (e.g., for a change in mean and variance).
Changepoint Detection: Finding Regime Shifts in Financial ...
The sesen.ai article has a fantastic section on penalty selection.
Read the section The Penalty. The table comparing AIC, BIC, and Manual selection is especially useful.
Multivariate Changepoint Detection
A truly advanced technique is to detect changepoints across multiple time series simultaneously. A frontal passage in meteorology involves changes in both temperature and wind. A financial crisis involves simultaneous shifts in asset prices, volatility, and trading volume. By stacking these features into a single array, you can ask the algorithm to find points where the entire system changes state. This is often far more robust than looking at any single feature in isolation.
The MetPy Mondays video has an excellent demonstration of this powerful capability.
MetPy Mondays #247 - Change Point Detection with Ruptures
Let's return to the video to see how to apply changepoint detection to multiple features at once.
Watch the final segment from using multiple features. The key is to stack the individual time series into a NumPy array of shape (n_samples, n_features) and pass that to the .fit() method. Notice how the detected changepoints become more meaningful when both temperature and wind are considered together.
From Changepoints to Features
Once you have your list of changepoint indices, [t_1, t_2, ..., t_k], the final step is to encode this information as features for your downstream ML model.
-
Regime ID: Create a categorical feature that assigns an ID to each segment. All data points for
t < t_1get regime ID 0, points betweent_1andt_2get ID 1, and so on. This is the most direct way to tell your model that these are distinct periods. -
Time Since Last Break: Create a numerical feature that counts the time steps since the most recent changepoint occurred. This can capture "time-in-regime" dynamics, like a momentum effect that builds up and then resets.
-
Regime-Specific Statistics: For each segment defined by the changepoints, calculate summary statistics (e.g., mean, variance, trend) of the target or other variables. Then, create features where each data point is tagged with the statistics of the regime it belongs to. For example, you could create a
volatility_of_current_regimefeature. This allows the model to explicitly use the statistical properties of the current market state as predictors.
Interview Alert: You are tasked with building a model for credit default prediction. Historical data spans the 2008 financial crisis. How would you design a feature engineering strategy to account for the dramatically different economic environment during that period?
A top-tier answer would include:
- Hypothesis: State the hypothesis that the relationship between predictors (e.g., income, debt-to-income ratio) and default risk changes during a financial crisis. The data is non-stationary.
- Method: Propose using a changepoint detection algorithm on a key macroeconomic indicator, such as VIX (volatility index) or a credit spread index, to identify the start and end of the crisis period.
- Algorithm Choice: Specify the PELT algorithm, justifying it as both computationally efficient () and statistically exact (guaranteed to find the optimal segmentation for a given penalty).
- Implementation: Mention the need to tune the penalty parameter (e.g., using BIC or cross-validation) to find a sensible segmentation that aligns with known economic events.
- Feature Encoding: Describe how to encode the results. The most powerful approach is to create a "Regime ID" feature (e.g., Pre-Crisis, Crisis, Post-Crisis). This allows the model, especially tree-based models, to learn completely different decision rules for each period. You could also create interaction features between the regime ID and other key predictors. This is more effective than simply adding a boolean "is_crisis" flag, as it captures the full change in dynamics.
Conclusion
Today, we've added a powerful tool to our arsenal for handling non-stationary time series. We moved from the world of predictable cycles to the reality of sudden, structural shocks. By identifying these regime changes, we can create features that make our models more robust, adaptive, and aware of the context in which they operate.
Key Takeaways:
- Structural Breaks (Changepoints): These are abrupt shifts in the statistical properties of a time series, marking a change in the underlying data-generating process.
- The Cost Function Approach: Changepoint detection is framed as an optimization problem: find a segmentation that minimizes the sum of segment costs plus a penalty for complexity.
- The PELT Algorithm: PELT is the algorithm of choice for most applications. It provides a globally optimal solution in computationally efficient linear time, avoiding the trade-offs of greedy (BinSeg) or slow (OP) methods.
- Practical Implementation: The
ruptureslibrary provides a user-friendly,scikit-learn-style interface. Thepenaltyparameter is the most critical hyperparameter to tune. - Feature Engineering: Detected changepoints can be encoded as features like regime IDs, time since the last break, or by annotating data with regime-specific statistics.
In this lesson, we focused on detecting when volatility regimes change. In our next lesson, we will dive deep into measuring that volatility. We'll explore sophisticated techniques from finance, like GARCH models and realized volatility, to create precise features that quantify risk and market turbulence.