This project implements a Bayesian Markov Switching Model for analyzing financial time series data, with a particular focus on capturing the peculiar dynamics of stock returns. The model employs a full Bayesian approach to identify and characterize different market regimes, providing a robust framework for understanding market behavior.
Financial time series analysis presents several modeling challenges:
- Regime-Switching Behavior: Markets transition between distinct states (e.g., bull markets, bear markets)
- Volatility Dynamics: Returns generally show persistent volatility patterns and clustering
- Non-Normal Features: Returns often display fat tails and excess kurtosis
- Mean Reversion: Returns exhibit varying degrees of mean reversion across regimes
The project addresses these challenges through a Markov Switching Model that:
- Uses latent state variables to identify market regimes
- Implements regime-specific dynamics for returns and volatility
- Incorporates prior knowledge (or lack of) via Bayesian priors
- Provides full posterior distributions for all model parameters (thanks to the Bayesian approach)
The EDA component provides a detailed statistical analysis for understanding financial time series behavior:
- Statistical Tests: Tests including Jarque-Bera, Augmented Dickey-Fuller, and Ljung-Box
- Distribution Analysis: Examination of return distributions and normality
- Volatility Analysis: Investigation of volatility clustering, persistence, and leverage effects
- Time Series Properties: Analysis of autocorrelation patterns in returns and squared returns
Building on the insights from the EDA, the Markov Switching Model is implemented via a Bayesian approach:
- Hidden Markov Chain: Latent state variables following a Markov process
- Regime-Specific Parameters:
- Mean returns with autoregressive components
- Volatility dynamics with GARCH-like features
- Transition probabilities between regimes
- Prior Specifications:
- Beta priors for transition probabilities
- Normal priors for mean returns
- Half-Normal priors for volatility parameters
- Stationarity constraints for autoregressive components
The model implements a volatility modeling approach that:
- Combines base volatility levels with regime-specific dynamics
- Incorporates ARCH-like components responding to recent returns
- Includes a memory component with adaptive weighting
- Ensures stationarity through parameter constraints
The project provides tools for:
- MCMC sampling with NUTS algorithm
- Posterior predictive analysis
- Regime probability estimation
- Model diagnostics and convergence checks
- Visualization of regime classifications and parameter distributions
EDA_analysis.ipynb
: Comprehensive statistical analysis of financial time seriesMSModel_2Regimes.ipynb
: Core implementation of the Markov Switching Modelstock.py
: Stock data handling and preprocessingaux.py
: Utility functions for analysis and visualization
- Clone this repository:
git clone https://github.com/yourusername/markov_switching.git
cd markov_switching
- Install the required dependencies:
pip install -r requirements.txt