⚑ Hamilton Markov-Switching Autoregressive Engine

A statistical machine learning and econometric inference engine developed by Ramprasad Atluri implementing the Hamilton (1989) forward filter and Kim (1994) backward smoothing algorithm for non-linear market regime detection.

πŸ“Œ Mathematical & Econometric Architecture

Financial asset returns exhibit state-dependent structural shifts in both conditional mean and conditional variance (heteroskedasticity). This model parametrizes market dynamics as a 3-State Markov-Switching Autoregressive model $\text{MS}(3)\text{-AR}(1)$:

ytβˆ’ΞΌSt=Ο•St(ytβˆ’1βˆ’ΞΌStβˆ’1)+Ο΅t,Ο΅t∼i.i.d. N(0,ΟƒSt2)y_t - \mu_{S_t} = \phi_{S_t} (y_{t-1} - \mu_{S_{t-1}}) + \epsilon_t, \quad \epsilon_t \sim \text{i.i.d.} \, \mathcal{N}(0, \sigma_{S_t}^2)

Where $S_t \in {0, 1, 2}$ represents the unobserved latent market regime governed by a first-order Markov chain with transition probability kernel $\mathbf{P}$:

P=(p00p01p02p10p11p12p20p21p22)=(0.96020.03480.00500.04890.92120.02990.03920.11850.8423)\mathbf{P} = \begin{pmatrix} p_{00} & p_{01} & p_{02} \\ p_{10} & p_{11} & p_{12} \\ p_{20} & p_{21} & p_{22} \end{pmatrix} = \begin{pmatrix} 0.9602 & 0.0348 & 0.0050 \\ 0.0489 & 0.9212 & 0.0299 \\ 0.0392 & 0.1185 & 0.8423 \end{pmatrix}

Unconditional Ergodic Distribution ($\boldsymbol{\pi}$):

The stationary unconditional distribution solves the eigensystem $(\mathbf{P}^\top - \mathbf{I})\boldsymbol{\pi} = \mathbf{0}$ subject to $\mathbf{1}^\top \boldsymbol{\pi} = 1$:

Ο€=[0.53420.35810.1077]β€…β€ŠβŸΉβ€…β€Š{P(Expansion)=53.4%P(Consolidation)=35.8%P(Turbulent Cascade)=10.8%\boldsymbol{\pi} = \begin{bmatrix} 0.5342 \\ 0.3581 \\ 0.1077 \end{bmatrix} \implies \begin{cases} \mathbb{P}(\text{Expansion}) = 53.4\% \\ \mathbb{P}(\text{Consolidation}) = 35.8\% \\ \mathbb{P}(\text{Turbulent Cascade}) = 10.8\% \end{cases}


πŸ”¬ Recursive Filter Formulation

1. Hamilton Forward Filter (1989)

The filtered inference $\hat{\xi}_{t|t} = \mathbb{P}(S_t = k \mid \mathcal{F}_t)$ is computed recursively:

ΞΎ^t∣tβˆ’1=P⊀ξ^tβˆ’1∣tβˆ’1\hat{\xi}_{t|t-1} = \mathbf{P}^\top \hat{\xi}_{t-1|t-1}

ΞΎ^t∣t=ΞΎ^t∣tβˆ’1βŠ™Ξ·t1⊀(ΞΎ^t∣tβˆ’1βŠ™Ξ·t)\hat{\xi}_{t|t} = \frac{\hat{\xi}_{t|t-1} \odot \boldsymbol{\eta}_t}{\mathbf{1}^\top (\hat{\xi}_{t|t-1} \odot \boldsymbol{\eta}_t)}

Where $\boldsymbol{\eta}_t = [f(y_t \mid S_t = 0), f(y_t \mid S_t = 1), f(y_t \mid S_t = 2)]^\top$ represents the conditional Gaussian density vector.

2. Kim Backward Smoother (1994)

Full-sample smoothed inferences $\hat{\xi}_{t|T} = \mathbb{P}(S_t = k \mid \mathcal{F}_T)$ conditioning on all past and future observations:

ΞΎ^t∣T=ΞΎ^t∣tβŠ™(P[ΞΎ^t+1∣T⊘ξ^t+1∣t])\hat{\xi}_{t|T} = \hat{\xi}_{t|t} \odot \left( \mathbf{P} \left[ \hat{\xi}_{t+1|T} \oslash \hat{\xi}_{t+1|t} \right] \right)


πŸš€ Quick Start in Python

from huggingface_hub import hf_hub_download
import numpy as np
import json

# Download calibrated parameter tensors
weights_path = hf_hub_download(
    repo_id="At1uri/markov-switching-autoregressive-engine",
    filename="calibrated_parameters.json"
)

with open(weights_path, "r") as f:
    params = json.load(f)

print("Transition Matrix P:\n", np.array(params["transition_matrix_P"]))
print("Log-Likelihood:", params["log_likelihood"])

Full Inference with hamilton_filter.py:

from hamilton_filter import HamiltonRegimeSwitchingFilter

# Initialize pre-calibrated engine
engine = HamiltonRegimeSwitchingFilter.load_pretrained()

# Example return series (e.g. daily S&P 500 or Bitcoin returns)
returns = np.array([0.004, -0.012, 0.003, -0.035, -0.018, 0.001])

# Run forward filter and Kim backward smoother
filtered_probs, predicted_probs = engine.forward_filter(returns)
smoothed_probs = engine.kim_smoother(filtered_probs, predicted_probs)

print("Current Filtered Regime Probabilities [Exp, Cons, Turb]:", filtered_probs[-1])
print("Smoothed Probabilities:", smoothed_probs[-1])

πŸ“Š Calibrated State Parameter Estimates

Metric State 0 (Expansion) State 1 (Consolidation) State 2 (Turbulent)
Drift ($\mu$) $+0.081% / \text{day}$ $+0.011% / \text{day}$ $-0.185% / \text{day}$
AR Coefficient ($\phi_1$) $+0.0421$ $-0.0894$ $+0.1582$
Annualized Volatility ($\sigma_{\text{ann}}$) $12.69%$ $22.22%$ $55.55%$
Expected Half-Life / Duration $25.12 \text{ days}$ $12.69 \text{ days}$ $6.34 \text{ days}$

βš–οΈ License

MIT License. Free for proprietary trading, risk management, and econometric research.

Downloads last month
1
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support