Files
2026-07-13 13:26:28 +08:00
..

Chapter 9: Model-Based Feature Extraction

The chapter reframes diagnostics as more than preprocessing checks. Stationarity tests, break diagnostics, and fractional differencing become feature generators in their own right, helping the reader think about persistence, structural instability, and memory preservation as inputs to downstream models rather than as one-time gates before modeling begins. It matters because many financial series are only usable once we understand what kind of temporal object they are and how much transformation is justified.

Hidden Markov model regime architecture

Regime models turn latent market states into filtered probabilities that can be used as features without looking into the future.

Learning Objectives

  • distinguish direct features from model-based features and judge when a fitted procedure adds useful information beyond direct transformations
  • use fitted procedures to extract forecasts, filtered states, residuals, conditional volatility, regime probabilities, and uncertainty summaries as downstream features
  • design a compact, interpretable set of model-based features from diagnostics, signal transforms, volatility models, uncertainty summaries, and regime models
  • enforce point-in-time correctness by fitting and selecting models within training windows, using filtered rather than future-informed outputs, and aligning refit cadence and online updates with the walk-forward protocol
  • transform asset-level temporal outputs into cross-sectional, benchmark-adjusted, pairwise, and universe-level features for multi-asset prediction tasks
  • distinguish between exploratory time-series methods that are useful for research diagnosis and deployable features that are safe for live, point-in-time use
  • use uncertainty and regime outputs primarily as conditioning features, and recognize when they should not be treated as stand-alone trading signals

Sections

9.1 Diagnostics and Stationarity Features

This section reframes diagnostics as more than preprocessing checks. Stationarity tests, break diagnostics, and fractional differencing become feature generators in their own right, helping the reader think about persistence, structural instability, and memory preservation as inputs to downstream models rather than as one-time gates before modeling begins. It matters because many financial series are only usable once we understand what kind of temporal object they are and how much transformation is justified.

  • 01_visual_diagnostics — This notebook demonstrates the complete diagnostic workflow for financial time series: visual inspection, stationarity tests, autocorrelation analysis, and rolling diagnostic features. Uses etfs, macro data.
  • 02_structural_breaks — This notebook demonstrates classical and ML-based methods for detecting structural breaks in financial time series. Uses etfs data.
  • 03_fractional_differencing — This notebook demonstrates fractional differentiation (FFD), a technique that achieves stationarity while preserving as much memory as possible. Uses etf_data, etfs data.

9.2 Transforming Signals to Uncover Hidden Structure

This section shows why rolling statistics are often too lossy for serious temporal feature engineering. Kalman filters, spectral methods, wavelets, and path signatures each recover a different hidden aspect of sequential structure: latent state, cycles, scale-localized behavior, and path geometry. Readers should care because this is where the chapter moves beyond familiar moving averages and rolling vol into genuinely richer representations of time series, while still keeping causal deployment constraints in view.

  • 04_kalman_filter — This notebook demonstrates the Kalman filter as a production feature extractor: level estimation, trend detection, innovation (surprise) signals, and dynamic hedge ratio estimation. Uses etfs data.
  • 05_spectral_features — This notebook demonstrates frequency-domain feature engineering: wavelet decomposition for multi-resolution analysis, rolling FFT for production spectral features, and Welch's method for robust power spectral density estimation. Uses etfs data.
  • 06_path_signatures — > Docker required: This notebook uses esig, which is an x86-only package > not included in the default environment. Run with: > bash > docker compose --profile py312 run --rm py312 python 09_model_based_features/06_path_signatures.py > Uses etfs data.

9.3 Volatility Features

This is one of the chapter's strongest sections because it treats volatility as the most forecastable part of the problem and shows how fitted models convert that predictability into usable features. ARIMA residuals, GARCH-family outputs, HAR coefficients, and roughness measures all become summaries of persistence, asymmetry, and horizon structure rather than isolated econometric artifacts. Readers should care because these are practical building blocks for both prediction and risk-sensitive downstream decisions.

  • 07_arima_features — This notebook demonstrates ARIMA as a feature extractor rather than a standalone forecaster. The key outputs — residuals, forecast values, and forecast uncertainty — feed into downstream ML pipelines.
  • 08_garch_volatility — This notebook extracts volatility features from GARCH family models: conditional volatility, persistence parameters, and leverage effects. Uses etfs, symbol_returns data.
  • 09_har_rough_volatility — This notebook covers multi-horizon volatility modeling and the Hurst exponent as features for ML trading systems. Uses etfs data.

9.4 Uncertainty Features

This section makes an important conceptual move: the model's uncertainty is itself informative. Posterior widths, forecast standard errors, and interval widths help distinguish between a strong estimate and a weakly identified one, even when the point forecast is the same. That matters in trading because signal strength, sizing, and interpretation should depend not only on what the model predicts, but also on how confident the model is.

  • 10_uncertainty_features — This notebook demonstrates Bayesian and frequentist approaches to extracting uncertainty features — posterior distributions and prediction intervals become ML inputs, not just diagnostics. Uses etfs data.

9.5 Regime Features

This section explains how changing market environments can be encoded as features, ranging from transparent threshold rules to HMMs, Markov-switching models, and distribution-based clustering. Its most valuable message is that regime information is usually better used as soft conditioning input than as a brittle hard switch between separate models. Readers should care because regime awareness often changes how other signals should be interpreted, especially near transitions and stress episodes.

  • 11_hmm_regimes — This notebook provides a thorough introduction to HMMs for financial regime detection, from first principles through production considerations. Uses etfs, macro data.
  • 12_wasserstein_regimes — This notebook implements the methodology from "Clustering Market Regimes Using the Wasserstein Distance" (Horvath et al., 2021). Instead of clustering on moment features (mean, variance, skewness), each time window is treated as an empirical distribution and clustered using optimal transport.
  • 13_regime_as_feature — This notebook demonstrates the regime-as-feature methodology: using regime probabilities as input features to ML models, rather than switching between specialized models based on detected regime. Uses etfs, macro data.

9.6 Cross-Sectional and Panel Features

Here the chapter bridges from asset-by-asset temporal modeling to the multi-asset workflow used later in the book. Raw temporal outputs become more useful once ranked across the universe, benchmark-adjusted, translated into pairwise states, or aggregated into market-wide summaries. This section matters because it turns model-based feature engineering from a single-series exercise into something usable for real cross-sectional prediction and portfolio construction.

  • 14_panel_features — This notebook demonstrates panel-level temporal features: pairwise relationships (cointegration, Kalman hedge ratios, O-U half-life) and cross-sectional transforms (ranking, relative features, universe aggregation). Uses etfs data.

Running the Notebooks

# From the repository root
uv run python 09_model_based_features/<notebook>.py

# Test mode (reduced data via Papermill)
uv run pytest tests/test_notebooks.py -v -k "09_model_based_features"

Runtime: 07_arima_features ~1-2 min, 10_uncertainty_features ~3 min (PyMC NUTS sampling); all other notebooks <40 s on a laptop.

06_path_signatures requires the ml4t-py312 Docker image (the esig library is x86-only and not in the default environment):

docker compose --profile py312 run --rm py312 \
    python 09_model_based_features/06_path_signatures.py

References