Discussing the article: "Feature Engineering for ML (Part 9): Structural Break Tests in Python"

 

Check out the new article: Feature Engineering for ML (Part 9): Structural Break Tests in Python.

We present a production‑ready implementation of AFML Chapter 17 structural break tests. The module includes Chu-Stinchcombe-White (one-/two-sided), Chow-type DFC, SADF across six models (linear, quadratic, sm poly 1, sm poly 2, sm exp, sm power), plus QADF (q, v) and CADF (q), returning bar-indexed scalar features. We address the book snippets' scaling issues and argument‑order pitfall, and show how a fixed lookback (L=504) bounds SADF cost to O(L²) per bar for regime detection.

The preceding articles in this Feature Engineering for ML series built features from the structure of time itself: Part 1 established fractional differentiation as a way to preserve long memory in a stationary series; Part 3 embedded the trading calendar into Fourier coordinates; and Part 5 compressed tick-level order flow into bar-indexed microstructural statistics. This article builds features from a qualitatively different question: not what the current bar looks like, but whether the data-generating process that produced it has recently changed.

Chapter 17 of López de Prado's Advances in Financial Machine Learning opens with a pointed observation. Structural breaks, the transition from one market regime to another, represent some of the best risk-adjusted trading opportunities precisely because most participants are caught off guard. A mean-reverting dynamic that gives way to momentum traps traders who continue to fade breakouts; a trending market that reverts catches traders who held directional positions too long. The actors on the losing side do not immediately recognise their mistake. They hold, they average down, and eventually they are stopped out. It is this forced liquidation that creates the edge. But identifying the transition in real time, rather than in retrospect, requires a formal statistical test.

The module afml.structural_breaks implements two families of such tests. The first, CUSUM tests, measure whether cumulative forecast errors deviate significantly from white noise. The second, explosiveness tests, detect exponential growth or collapse that is inconsistent with a random walk. This article covers both families. It adds two variants not in the original implementation and documents performance problems in the book's Python snippets that make them unusable on realistic series lengths. It closes by developing the chapter's implied link to strategy selection.


Author: Patrick Murimi Njoroge