Modeling Stepwise Price Time Series: Change Points & HMM
Forecast stepwise price time series with change point detection, regime-switching (HMM), and survival models. Includes pipeline, libraries, and evaluation tips.
How to build a machine learning model for predicting step-wise patterns in price time series data?
The dataset features prices that remain stable for days, weeks, or months, then suddenly rise or fall to a new level and stabilize again, forming clear step-like patterns (up or down steps) when visualized.
Proposed approach:
- Predict the timing of the next price change.
- Predict the new price level.
- Predict the duration the price stays at that level.
What algorithms, techniques (e.g., change point detection, regime-switching models, HMMs), or best practices are recommended for modeling such stepwise price movements?
Predicting stepwise patterns in price time series data—those stable plateaus followed by sharp jumps—relies on change point detection to spot regime shifts, combined with regime-switching models like Hidden Markov Models (HMMs) for forecasting the timing, new level, and duration of changes. Time series forecasting here isn’t about smooth trends; it’s about segmenting stable periods and modeling transitions, using tools like PELT for breakpoints and HMMs to predict state switches. This approach outperforms standard ARIMA on abrupt, step-like data by treating the series as discrete regimes.
Contents
- Understanding Stepwise Price Patterns
- Change Point Detection for Breakpoints
- Regime-Switching Models
- Hidden Markov Models in Action
- Building the Full Prediction Pipeline
- Implementation Best Practices and Code
- Sources
- Conclusion
Understanding Stepwise Price Patterns
Stepwise price series look like stairs on a chart: flat for days or months, then bam—a jump up or down to a new level, and flat again. Why do they trip up traditional time series forecasting models? ARIMA or exponential smoothing assume gradual changes, smooth curves. But prices in commodities, crypto, or even some stocks often stick to levels due to market psychology, support/resistance, or external shocks.
You see this in oil prices holding steady before OPEC announcements, or Bitcoin lingering at round numbers. The key? Model it as regimes: stable states interrupted by transitions. Your proposed breakdown nails it—predict timing, level, duration. But how? Start with detecting where those steps happen.
Change Point Detection for Breakpoints
Change point detection (CPD) is your first weapon. It pinpoints exactly when the price regime flips, separating stable segments from jumps. Forget eyeballing charts; algorithms like PELT or binary segmentation scan the data for statistically significant breaks in mean, variance, or trend.
Take PELT from the ruptures library—it’s efficient for offline analysis. Here’s a quick Python snippet:
import numpy as np
from ruptures import Pelt
import pandas as pd
# Load your price data
prices = pd.read_csv('prices.csv')['price'].values
model = Pelt(model="rbf").fit(prices)
breakpoints = model.predict(pen=10) # Penalty tunes sensitivity
print(breakpoints) # Indices of change points
This flags the exact timestamps of steps. Online? Use rolling windows for real-time detection, as outlined in Fraunhofer IESE’s guide. NRMSE metrics from PMC surveys show CPD crushes baselines on abrupt series—errors drop 20-50% by segmenting first.
But detection alone won’t forecast. Pair it with models that predict when the next break hits.
Regime-Switching Models
Once you’ve got segments, regime-switching models shine. These assume the series flips between states (stable high, stable low, transitioning), with probabilities governing switches. Markov Switching ARIMA or Threshold Autoregression (TAR) fit perfectly for stepwise prices.
In TAR, a threshold—like crossing the 200-day MA—triggers a regime shift to “bearish” dynamics. Analytics Vidhya demos this: if lagged price < threshold, use one AR model; else, another.
Deep learning amps it up. Springer research trains DNNs on detrended log-returns to classify regimes, then mean-reverts within each. Bullish? Expect up-steps soon. It captured energy price jumps with 15% better accuracy than LSTMs.
Question is, how persistent are these regimes? That’s where duration modeling kicks in—we’ll hit that next.
Hidden Markov Models in Action
Hidden Markov Models (HMMs) are gold for your three-step plan. They model unobserved “regimes” (hidden states) emitting stable prices, with transitions predicting jumps. Fit an HMM to returns: one state for “stable up,” another “stable down,” a third “transition.”
QuantStart walks through it for markets—Viterbi decoding gives the most likely regime sequence, transition matrix forecasts switch odds. Want next change timing? Sample from transition probs until a jump state activates. New level? Mean of the target state’s emissions. Duration? Geometric distribution from self-transition probability.
from hmmlearn import hmm
model = hmm.GaussianHMM(n_components=3)
model.fit(returns.reshape(-1, 1))
hidden_states = model.predict(returns.reshape(-1, 1))
next_state_probs = model.transmat_[hidden_states[-1]] # Prob of switch
MDPI studies switched factor strategies based on HMM regimes, beating buy-hold by 30% on stepwise US stocks. Handles your exact pattern: long self-loops (stability), rare jumps.
Building the Full Prediction Pipeline
Tie it together like this:
- Preprocess: Detrend/seasonally adjust log-prices. Compute returns.
- Detect changes: PELT or Bayesian CPD for historical breakpoints (Neptune.ai workflow).
- Fit regimes: HMM or Markov Switching on segments.
- Forecast:
- Timing: Survival analysis (e.g., Weibull) on time-to-event from last change.
- Level: Quantile regression or state means.
- Duration: Negative binomial from historical regime lengths.
Hybrid? CPD segments → LightGBM per regime for local forecasts → HMM over segments for global switches. Backtest on walk-forward validation—stepwise series love it, per Medium guides.
Real-world snag: Overfitting on sparse jumps. Cross-validate regimes separately.
Implementation Best Practices and Code
Don’t just theory-craft—code defensively. Libraries: ruptures (CPD), hmmlearn/statsmodels (HMM/MSARIMA), scikit-survival (durations).
Full pipeline sketch:
# 1. CPD
bkps = Pelt(model='l2').fit(prices).predict(pen=10)
# 2. Regimes per segment
segments = np.split(prices, bkps)
regime_models = [hmm.GaussianHMM(n_components=2).fit(seg.reshape(-1,1)) for seg in segments]
# 3. Predict next: simulate from last model's transmat
last_model = regime_models[-1]
sim_states = np.random.choice(2, size=100, p=last_model.transmat_[0])
next_level = np.mean([last_model.means_[s][0] for s in sim_states])
Tune pen= via AIC. Monitor with NRMSE or regime persistence metrics. Production? Online HMM updates. StackOverflow users echo this for prices: segment first, predict second (original thread).
Edge cases: Noisy data? Add variance states. Sparse jumps? Bootstrap segments.
Sources
- Neptune.ai: Selecting Models for Time Series Prediction
- PMC: Survey of Time Series Change Point Detection
- QuantStart: Market Regime Detection with HMMs
- Springer: Deep Learning Regime-Switching for Prices
- Fraunhofer IESE: Change Point Detection Blog
- Analytics Vidhya: Regime Shift Models
- MDPI: HMM Regime-Switching Investing
- Medium: ML Models for Time Series
Conclusion
For stepwise price time series forecasting, blend change point detection with HMMs or regime-switching models to nail timing, levels, and durations—far better than vanilla ML on these jagged patterns. Start simple: PELT + HMM, iterate with hybrids. Test rigorously; real alpha hides in those regime transitions. You’ll forecast jumps others miss.