This repository provides a complete Python implementation of a Bayesian framework for estimating nonlinear asset pricing models that incorporate regime-switching and stochastic volatility. The model integrates a Hidden Markov Model (HMM) for discrete regime states, nonlinear stochastic volatility dynamics, and a Particle Markov Chain Monte Carlo (PMCMC) algorithm for joint state-parameter inference. We employ an Unscented Kalman Filter (UKF)-based filtering procedure to adaptively estimate latent volatility under uncertainty in both parameters and regimes.
The provided code offers:
-
Nonlinear State-Space Representation:
Handles complex observation models relating latent volatility and regimes to observed prices. -
Regime-Switching Dynamics (HMM):
Integrates discrete state transitions to capture shifts in market conditions, sentiment, or liquidity. -
Stochastic Volatility Modeling:
Employs a mean-reverting stochastic volatility process (Heston-type dynamics) conditioned on regimes. -
Unscented Kalman Filter (UKF):
Efficient approximation of nonlinear filtering distributions for the continuous state (volatility). -
Particle MCMC (PMCMC) for Parameter Estimation:
Joint inference over model parameters and latent states via a combination of particle filtering and MCMC sampling steps.
This project is intended for quantitative researchers and practitioners interested in advanced Bayesian methods for financial time series and state-space modeling.
We consider a discrete-time asset price series
-
Regime Variable
$$R_t$$ :
A latent Markov chain representing market regimes:
The regime influences the parameters governing the price and volatility evolution.
-
Stochastic Volatility State
$$v_t$$ :
Let$$v_t > 0$$ denote the latent volatility level at time$$t$$ . We assume a mean-reverting square-root process (Heston-type):
- Observation Model (Prices): We consider log-returns:
$$
r_t = \log\left(\frac{S_t}{S_{t-1}}\right).
$$
In regime
Thus:
The parameters
2. Hidden Markov Model (HMM)
The regime process
The joint distribution of $$ {R_t} $$ given $$ \Pi $$ is:
where
Define the continuous latent state
- State Transition:
- Observation Model:
This is a nonlinear state-space model with discrete switching (HMM). The nonlinearity arises from the volatility dynamics and lognormal price observation.
We place priors on model parameters. For each regime $$ i $$:
-
$$\kappa_{v,i}$$ may have a Gamma prior:$$\kappa_{v,i} \sim \Gamma(a_\kappa,b_\kappa)$$ . -
$$\theta_{v,i}$$ may have a Gaussian prior:$$\theta_{v,i} \sim N(\mu_\theta,\sigma_\theta^2)$$ . -
$$\sigma_{v,i}$$ may have a Half-Cauchy prior to ensure positivity and heavy-tailed flexibility. -
$$\mu_i \sim N(0,\sigma_\mu^2)$$ , and$$\sigma_{level,i}$$ also from a Half-Cauchy.
For the regime transition matrix
The resulting joint posterior is:
Given known parameters $$ \Theta $$, we seek the filtering distribution:
Since
UKF Steps per Regime:
-
Sigma Points: Generate a set of sigma points
$${\chi^{(j)}_{t-1}}$$ from the Gaussian distribution at time$$t-1$$ . -
Time Update: Propagate each sigma point through the nonlinear state equation:
Compute the predicted mean and covariance.
-
Measurement Update: Propagate sigma points through the observation model:
$$ y^{(j)}t = h{R_t}(\chi^{(j)}_t), $$
update state mean and covariance using the observed
$$S_t$$ .
Since
With uncertain parameters
Approach: Particle MCMC (Andrieu et al., 2010)
-
Particle Filter for Latent States:
Given a candidate parameter set
$$\Theta$$ , we run a particle filter to approximate:
The particle filter simulates ( (v_t^{(n)}, R_t^{(n)}) ) for ( n=1,\ldots,N ) particles, resampling at each step. The likelihood estimate is the product of normalized weights:
We have a current parameter draw ( \Theta^{(m)} ). Propose ( \Theta^* ) from a proposal distribution ( q(\Theta^* \mid \Theta^{(m)}) ). Accept or reject based on the posterior ratio:
If ( U \sim \text{Uniform}(0,1) ), accept if ( U < \min(1, A) ), otherwise reject and set ( \Theta^{(m+1)} = \Theta^{(m)} ).
After burn-in, the chain ( {\Theta^{(m)}} ) approximates the posterior distribution over parameters.
The code is organized as follows:
bayesian_nonlinear_pricing/
__init__.py # Package initialization
data_generation.py # Synthetic data generation
model_params.py # Parameter management, priors, structure
hmm.py # Hidden Markov Model utilities (forward-backward, sampling)
statespace.py # Nonlinear state & observation models
filters.py # Regime-Switching UKF implementation
inference.py # Particle filter for latent states
utils.py # Numeric utilities (sigma points, density evaluation)
pmcmc.py # Particle MCMC integration for parameter inference
main.py # Example script: simulate data, run PMCMC, estimate states
-
Data Generation:
Usedata_generation.pyto produce synthetic $$ (S_t, R_t, v_t) $$ from known parameters. This serves as ground truth for testing inference methods. -
Initialization:
Draw initial parameters from specified priors inmodel_params.py. -
PMCMC Inference:
pmcmc.pyruns a PMCMC algorithm. Each iteration:- Propose new parameters $$ \Theta^* $$.
- Run particle filter to compute $$ \hat{p}(S_{1:T} \mid \Theta^*) $$.
- Accept/reject the proposal, forming a posterior chain.
-
Filtering with UKF:
Once parameters are estimated, usefilters.py(RegimeSwitchingUKF) to produce smoothed volatility and regime estimates given the posterior means or samples of $$ \Theta $$.
Requirements: Python 3, numpy, scipy.
cd bayesian_nonlinear_pricing
python3 main.pyThe main.py script:
- Generates synthetic data.
- Samples initial parameters from priors.
- Runs PMCMC for a specified number of iterations.
- Retrieves posterior parameter estimates.
- Performs UKF-based state filtering with the estimated parameters.
-
Parameter Estimates:
The PMCMC chain provides draws from the posterior of $$ \Theta $$. Summaries (mean, median, credible intervals) characterize the uncertainty around model parameters. -
Volatility Filtering:
The final UKF step provides an estimated volatility path $$ \hat{v}_t $$ that can be compared to the true simulated volatility for validation. -
Regime Probabilities:
At each time $$ t$$, the filtered regime probabilities $$ P(R_t=i \mid S_{1:t}) $$ reveal how the algorithm identifies shifts in market states.
-
Multi-Factor Volatility Models: Extend $$ \mathbf{x}_t $$ to include multiple state factors (e.g., jump intensities, risk premia). The UKF and particle filters generalize naturally to higher dimensions.
-
Non-Gaussian Observation Models: If returns exhibit heavy tails or skewness, replace Gaussian assumptions with more flexible distributions (e.g., Student-t), requiring appropriate filtering methods (e.g., particle filters only).
-
Time-Varying Regime Dynamics: Consider more complex regime dynamics or Bayesian updating of the HMM parameters over time.
-
Efficient Proposals and Tuning: The current PMCMC uses simplistic random-walk proposals. Advanced strategies (e.g., adaptive MCMC, Hamiltonian Monte Carlo) could improve mixing and efficiency.
-
Particle MCMC:
- Andrieu, C., Doucet, A., & Holenstein, R. (2010). Particle Markov chain Monte Carlo methods. J. Royal Statistical Society: Series B, 72(3), 269–342.
-
Heston Volatility Model:
- Heston, S. L. (1993). A closed-form solution for options with stochastic volatility. Review of Financial Studies, 6(2), 327–343.
-
Unscented Kalman Filter:
- Julier, S.J., & Uhlmann, J.K. (1997). A new extension of the Kalman filter to nonlinear systems. Proceedings of AeroSense: The 11th Int. Symp. on Aerospace/Defense Sensing, Simulation and Controls.
-
Hidden Markov Models in Finance:
- Hamilton, J.D. (1989). A new approach to the economic analysis of nonstationary time series and the business cycle. Econometrica, 57(2), 357–384.