Use when segmenting time-series into regimes, detecting structural breaks, or constructing context sets for few-shot learning...
Comprehensive guide for detecting regime changes in financial time-series using Gaussian Process change-point detection (GP-CPD), essential for segmenting markets into stationary periods and improving trading strategies.
Activate this skill when:
A regime change (or change-point) is a point in time where the statistical properties of a time-series shift significantly.
Examples in Finance:
Why Detect Them?
A Gaussian Process defines a distribution over functions:
# GP is fully specified by mean and covariance functions
y ~ GP(μ(x), k(x, x'))
where:
- μ(x): mean function (often 0)
- k(x, x'): covariance (kernel) function
Matérn 3/2 Kernel (recommended for financial data):
k(r) = σ² * (1 + √3*r/ℓ) * exp(-√3*r/ℓ)
where:
- r = |x1 - x2|
- ℓ: length_scale (how quickly correlation decays)
- σ²: variance (overall scale)
Properties:
See IMPLEMENTATION.md for kernel implementations.
The Change-Point (CP) kernel models a transition between two GPs:
k_CP(x1, x2) = σ(x1) * σ(x2) * k1(x1, x2)
+ (1-σ(x1)) * (1-σ(x2)) * k2(x1, x2)
where σ(x) = sigmoid((x - t_cp) / sigma) is transition function
Key Insight:
See IMPLEMENTATION.md for code.
Compare two models:
Detection Steps:
severity = L_C / (L_M + L_C)severity ≥ threshold, declare change-pointSeverity Interpretation:
severity = 0.5: No evidence for change-point (models equally good)severity = 0.9: Strong evidence for change-pointseverity = 0.95: Very strong evidence for change-pointSee IMPLEMENTATION.md for full algorithm.
Recursively apply GP-CPD to segment entire time-series:
Process:
Constraints:
min_length: Minimum regime length (typically 5 days)max_length: Maximum regime length (21 or 63 days)See IMPLEMENTATION.md for implementation.
Create high-quality context sets for few-shot learning:
Strategy:
Performance Impact (from X-Trend paper):
Why It Works:
See IMPLEMENTATION.md for code.
lookback_window (ℓ_lbw):
- 21 days (1 month): Good balance of speed and robustness
- 63 days (3 months): More robust but slower detection
- Trade-off: Shorter = faster detection, Longer = less noise
threshold (ν):
- 0.90: Detect most regime changes (more sensitive)
- 0.95: Detect only strong regime changes (more specific)
- 0.99: Very conservative (few, strong changes only)
Recommendation:
- For max_length = 21: Use ν = 0.90
- For max_length = 63: Use ν = 0.95
min_length = 5: # Minimum 5 days for meaningful regime
max_length = 21 or 63:
- 21 (1-month): Shorter, more granular regimes
- 63 (3-month): Longer, more stable regimes
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
class FinancialCPD:
def __init__(self, lookback=21, threshold=0.9):
self.lookback = lookback
self.threshold = threshold
def detect_changepoint(self, prices):
"""Detect change-point in price window."""
gp_m, L_M = self.fit_matern_gp(prices)
t_cp, L_C = self.fit_changepoint_gp(prices)
severity = L_C / (L_M + L_C)
if severity >= self.threshold:
return t_cp, severity
else:
return None, severity
def segment(self, prices, min_len=5, max_len=63):
"""Segment entire time-series into regimes."""
# See IMPLEMENTATION.md for full code
See IMPLEMENTATION.md for complete implementation.
def visualize_regimes(prices, regimes):
"""Plot time-series with colored regime segments."""
# See IMPLEMENTATION.md for full visualization code
See IMPLEMENTATION.md for plotting code.
Identify regime changes that cause momentum losses:
See IMPLEMENTATION.md for implementation.
Choose trading strategy based on current regime characteristics:
See IMPLEMENTATION.md for code.
✅ Use Matérn 3/2 kernel for financial data (better than RBF or OU) ✅ Set reasonable lookback (21 days is good default) ✅ Enforce min/max lengths to avoid trivial or excessive regimes ✅ Validate on multiple assets to tune threshold ✅ Move past change-point to avoid corrupting next regime's representation ✅ Use for context construction in few-shot learning
❌ Don't use RBF kernel - too smooth for financial data ❌ Don't set lookback too small - noisy detections ❌ Don't set lookback too large - delayed detection ❌ Don't ignore severity - it indicates confidence ❌ Don't allow overlapping regimes - each point in one regime only
Based on X-Trend paper results:
Few-Shot Learning:
Why It Works:
When implementing GP-CPD:
L_C / (L_M + L_C)few-shot-learning-finance - Using CPD for context constructionfinancial-time-series - Returns and momentum factors to analyzex-trend-architecture - Attending over regime segmentsLast Updated: Based on X-Trend paper (March 2024) Skill Type: Domain Knowledge + Implementation Line Count: ~290 (under 500-line rule ✅)