Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Regime switching Hidden Markov model

This example replicates the case study analyzing financial time series, specifically the daily difference in log price data of Google’s stock, referred to as returns rtr_t.

We’ll assume that at any given time tt the stock’s returns will follow one of two regimes: an independent random walk regime where rt∼N(α1,σ12)r_t \sim \mathcal{N}(\alpha_1, \sigma^2_1) and an autoregressive regime where rt∼N(α2+ρrt−1,σ22)r_t \sim \mathcal{N}(\alpha_2 + \rho r_{t-1}, \sigma_2^2). Being on either of the two regimes, st∈{0,1}s_t\in \{0, 1\}, will depend on the previous time’s regime st−1s_{t-1}, call these probabilities pst−1,stp_{s_{t-1}, s_{t}} for st−1,st∈{0,1}s_{t-1}, s_t \in \{0, 1\}. Set as parameters of the model p1,1p_{1,1} and p2,2p_{2,2} and define the complementary probabilities by definition: p1,2=1−p1,1p_{1,2} = 1-p_{1,1} and p2,1=1−p2,2p_{2,1} = 1-p_{2,2}. Since the regime at any time is unobserved, we instead carry over time the probability of belonging to either one regime as ξ1t+ξ2t=1\xi_{1t} + \xi_{2t} = 1. Finally, we need to model initial values, both for returns r0r_0 and probability of belonging to one of the two regimes ξ10\xi_{10}.

In the whole, our regime-switching model is defined by the likelihood

L(r∣α,ρ,σ2,p,r0,ξ10)=∏tξ1tη1t+(1−ξ1t)η2tξ1t=ξ1t−1η1tξ1t−1η1t+(1−ξ1t−1)η2t,\begin{split} L(\mathbf{r}|\alpha, \rho, \sigma^2, \mathbf{p}, r_0, \xi_{10}) &= \prod_t \xi_{1t}\eta_{1t} + (1-\xi_{1t})\eta_{2t} \\ \xi_{1t} &= \frac{\xi_{1t-1}\eta_{1t}}{\xi_{1t-1}\eta_{1t} + (1-\xi_{1t-1})\eta_{2t}}, \end{split}

where ηjt=pj,1\eta_{jt} = p_{j,1}, N(rt;α1,σ12)+pj,2\mathcal{N}(r_t;\alpha_1, \sigma_1^2) + p_{j,2}, and N(rt;α2+ρrt−1,σ22)\mathcal{N}(r_t; \alpha_2 + \rho r_{t-1}, \sigma_2^2) for j∈{0,1}j\in\{0, 1\}. And the priors of the parameters are:

α1,α2∼N(0,0.1)ρ∼N0(1,0.1)σ1,σ2∼C+(1)p1,1,p2,2∼Beta(10,2)r0∼N(0,0.1)ξ10∼Beta(2,2),\begin{split} \alpha_1, \alpha_2 &\sim \mathcal{N}(0, 0.1) \\ \rho &\sim \mathcal{N}^0(1, 0.1) \\ \sigma_1, \sigma_2 &\sim \mathcal{C}^+(1) \\ p_{1,1}, p_{2,2} &\sim \mathcal{Beta}(10, 2) \\ r_0 &\sim \mathcal{N}(0, 0.1) \\ \xi_{10} &\sim \mathcal{Beta}(2, 2), \end{split}

where N0\mathcal{N}^0 indicates the truncated at 0 Gaussian distribution and C+\mathcal{C}^+ the half-Cauchy distribution.

Notebook Cell
Runtime for NUTS 0 days 00:01:14.948354

                mean       std    median      5.0%     95.0%     n_eff     r_hat
  alpha[0]      0.06      0.08      0.06     -0.08      0.20  27417.83      1.00
  alpha[1]      0.01      0.10      0.01     -0.15      0.18  23835.13      1.00
      p[0]      2.71      0.41      2.69      2.08      3.39   8514.64      1.00
      p[1]      1.82      0.91      1.75      0.44      3.23   9584.95      1.00
       rho     -0.02      0.10     -0.01     -0.19      0.15  27372.76      1.00
  sigma[0]      1.04      0.05      1.04      0.97      1.12  21590.48      1.00
  sigma[1]      2.16      0.18      2.14      1.86      2.45  17614.21      1.00
      xi_0      0.50      1.04      0.47     -1.18      2.22  26116.97      1.00
       y_0      0.00      0.10      0.00     -0.17      0.16  28068.60      1.00

<Figure size 3456x1510.71 with 81 Axes>