Stock Volatility Detection
Real-world example based on the ../examples-notebooks
notebook examples/06_real_world_example.ipynb. We use Fast-BOCPD to
monitor the US30 (Dow Jones Industrial Average) daily realized volatility
from 2008 to 2025 and automatically detect major regime shifts such as the
2008 financial crisis and COVID-19 crash.
Why volatility?
Data is naturally positive and skewed, making it a good showcase for the
GammaGammaobservation model.Regimes last from months to years, which stresses the hazard / run-length configuration.
Finance teams care about both historical back-testing and live alerting.
Dataset
examples/us30_vol.csvcontains normalized daily volatility (already scaled by price) from Jan-2008 to Jan-2025.We split chronologically: 2008–2020 for calibration and 2021–2025 for live simulation—no lookahead.
Training phase (2008–2020)
Estimate Gamma parameters. Method-of-moments gives a shape ≈ 1.06 and rate ≈ 314.3. We wrap those into
GammaGamma(alpha0=2, beta0=alpha0/rate, shape=shape)—a weak prior centred on the empirical rate.Hazard exploration. Compare
lambdain {80, 120, 150, 200}.lambda = 150balances sensitivity and stability;max_run_length = 3 × lambdahandles long calm stretches.Visualization. The notebook plots segment means and MAP run length to verify that large spikes (Lehman, COVID) trigger resets while the long 2010–2019 calm period is kept as a single regime.
Streaming deployment (2021–2025)
Fresh detector configured with the chosen hyperparameters. We call
fast_bocpd.utils.OnlineChangeDetectorwithmin_cp_prob = 0.3so alerts fire whenP(r_t = 0)crosses 30% or when the MAP run length drops sharply.Stream each day sequentially. Every detection logs the timestamp, posterior probability, and previous segment duration:
CHANGEPOINT DETECTED Date: 2020-03-16 Index: 271 P(CP): 85.0% Previous segment: 522 days
get_segments()andget_map_history()power dashboards that measure how long the market stayed calm vs. turbulent.
How to reuse
Replace
us30_vol.csvwith your own positive-valued series (e.g. demand, latency, volatility of another symbol).Redo the Gamma moment-fit and hazard sweep; the notebook exposes helper cells for re-fitting the model.
Plug your alerting backend into
OnlineChangeDetector’s stream loop—eachfast_bocpd.utils.Changepointcarries probability, segment length, and optional metadata for auditing.
Run the example
jupyter lab examples/06_real_world_example.ipynb
Key takeaways
Model choice matters. Positive, heavy-tailed volatility is better served by
GammaGammathan Gaussian models.Hazard tuning is crucial.
lambdaencodes expected regime duration; long calm periods require highermax_run_lengthto avoid false resets.Online detector = production ready.
min_cp_prob, cooldown, and metadata handling make deployments straightforward once the parameters are calibrated.