Skip to content

AdvancedQuantitative MethodsPython

Run this module

cd "Quantitative Methods - Regime Detection"
python "regime_detection.py"

View source on GitHub


Market Regime Detection

Identifies distinct market states (bull/bear, low/high volatility) using statistical methods. Regime-aware strategies adapt parameters to the current market environment.

Functions

Function Description
moving_average_regime(prices, short, long) MA crossover bull/bear detection
volatility_regime(returns, window, n_regimes) Quantile-based volatility buckets
gaussian_mixture_regime(returns, n_regimes) GMM-based unsupervised regime detection
regime_stats(returns, labels) Per-regime return statistics

Methods

Moving Average Crossover

Classic technical approach: Bull when 50-day MA > 200-day MA (golden cross), Bear otherwise. Simple, interpretable, but lagging.

Volatility Regime

Rolling realized volatility classified into low/medium/high buckets using quantile thresholds. Useful for dynamic position sizing.

Gaussian Mixture Model (GMM)

Unsupervised learning: fit a mixture of Gaussians to the return distribution. Regime 0 = lowest mean (bear), Regime 1 = highest mean (bull). Requires scikit-learn.

Example

from regime_detection import gaussian_mixture_regime, regime_stats
import numpy as np

returns = np.random.normal(0.001, 0.015, 500)
result = gaussian_mixture_regime(returns, n_regimes=2)
stats = regime_stats(returns, result["labels"])

Applications

  • Strategy switching: Use momentum in bull regimes, mean-reversion in bear
  • Risk scaling: Reduce position sizes in high-volatility regimes
  • Macro overlay: Override signals when macro regime shifts

Continue in Quantitative Methods

  • Quantitative Methods - Bayesian Inference

    A strategy wins 7 of its first 10 trades. Is its true win rate 70%? Almost

  • Quantitative Methods - Bootstrap

    The bootstrap estimates the sampling distribution of any statistic by resampling the observed data with replacement — no normality assumption required. It is the honest way to put confidence intervals around backtest metrics like Sharpe ratio, mean return, or maximum drawdown.

  • Quantitative Methods - Cointegration

    Cointegration: two non-stationary series whose linear combination is stationary. Backbone of statistical arbitrage and pairs trading.

  • Quantitative Methods - Copulas

    This module demonstrates the concept of Copulas, specifically the Gaussian Copula, used in quantitative finance to model the dependency structure between multivariate random variables.

  • Quantitative Methods - Extreme Value Theory

    Most risk models assume returns are normally distributed. They are not —

  • Quantitative Methods - Factor Models

    Factor models explain asset returns as a linear combination of systematic factors plus a stock-specific residual. The Fama-French 3-Factor Model (1992) extended CAPM by adding two well-documented risk premia: the Size premium (SMB) and the Value premium (HML), dramatically improving the explanation of cross-sectional stock returns.

Browse all modules Learning paths