Skip to main content

Cookies on Macro by Mark.We use essential cookies for sign-in, locale, and security. With your permission, we also use optional cookies for preferences, for analytics, and for advertising measurement with the Meta (Facebook) Pixel. Read our cookie policy.

Macro by MarkMacro by Mark
  • Home
Loading…
Loading…
← Models
MacroConceptsPolicyModels

Markov-switching VAR Model

Regime-switching macro system where latent states govern recession and expansion dynamics through transition probabilities.

OverviewGraphMathReferences

Understand the mathematics

Follow the same equations and assumptions one section at a time or in a continuous reading.

Hover or focus a symbol defined in this model to find its exact matches. Select it to keep the highlight; Escape clears the selection.

The Hamilton Filter

The Hamilton filter is a forward recursion that computes the filtered regime probability P(st=m∣y1:t)P(s_t = m | y_{1:t})P(st​=m∣y1:t​) for each time period ttt and each regime mmm. It generalizes the Baum-Welch forward algorithm from the hidden Markov model literature to the time-series regression setting.

At each date ttt, the filter performs two operations: prediction (propagate yesterday's filtered probabilities through the transition matrix) and updating (combine the predicted probabilities with the likelihood of today's observation in each regime).

The filter also produces the model's log-likelihood as a byproduct. The marginal density of yty_tyt​ given past data is f(yt∣y1:t−1)=∑mξt∣t−1(m)f(yt∣st=m,y1:t−1)f(y_t | y_{1:t-1}) = \sum_m \xi_{t|t-1}^{(m)} f(y_t | s_t = m, y_{1:t-1})f(yt​∣y1:t−1​)=∑m​ξt∣t−1(m)​f(yt​∣st​=m,y1:t−1​), and the full log-likelihood is ∑tln⁡f(yt∣y1:t−1)\sum_t \ln f(y_t | y_{1:t-1})∑t​lnf(yt​∣y1:t−1​). The EM algorithm maximizes this likelihood.

f(yt∣st=m,y1:t−1)=(2π)−n/2∣Σ(m)∣−1/2exp⁡(−12ut(m)′(Σ(m))−1ut(m))f(y_t | s_t = m, y_{1:t-1}) = (2\pi)^{-n/2} |\Sigma^{(m)}|^{-1/2} \exp\left(-\frac{1}{2} u_t^{(m)\prime} (\Sigma^{(m)})^{-1} u_t^{(m)}\right)f(yt​∣st​=m,y1:t−1​)=(2π)−n/2∣Σ(m)∣−1/2exp(−21​ut(m)′​(Σ(m))−1ut(m)​)
sts_tst​Σ(m)\Sigma^{(m)}Σ(m)

Gaussian density of yty_tyt​ conditional on regime mmm, where ut(m)=yt−c(m)−A1(m)yt−1−…−Ap(m)yt−pu_t^{(m)} = y_t - c^{(m)} - A_1^{(m)} y_{t-1} - \ldots - A_p^{(m)} y_{t-p}ut(m)​=yt​−c(m)−A1(m)​yt−1​−…−Ap(m)​yt−p​.

ξt∣t−1(m)=∑i=1Mpim ξt−1∣t−1(i)\xi_{t|t-1}^{(m)} = \sum_{i=1}^{M} p_{im} \, \xi_{t-1|t-1}^{(i)}ξt∣t−1(m)​=i=1∑M​pim​ξt−1∣t−1(i)​

Prediction step: the probability of being in regime mmm at time ttt, before seeing yty_tyt​, is the transition-weighted sum of yesterday's filtered probabilities.

ξt∣t(m)=ξt∣t−1(m)⋅f(yt∣st=m,y1:t−1)∑j=1Mξt∣t−1(j)⋅f(yt∣st=j,y1:t−1)\xi_{t|t}^{(m)} = \frac{\xi_{t|t-1}^{(m)} \cdot f(y_t | s_t = m, y_{1:t-1})}{\sum_{j=1}^M \xi_{t|t-1}^{(j)} \cdot f(y_t | s_t = j, y_{1:t-1})}ξt∣t(m)​=∑j=1M​ξt∣t−1(j)​⋅f(yt​∣st​=j,y1:t−1​)ξt∣t−1(m)​⋅f(yt​∣st​=m,y1:t−1​)​
sts_tst​

Update step: Bayes' rule. The denominator is the marginal likelihood of yty_tyt​ given past data.

ln⁡L=∑t=1Tln⁡(∑m=1Mξt∣t−1(m)⋅f(yt∣st=m,y1:t−1))\ln L = \sum_{t=1}^{T} \ln \left( \sum_{m=1}^{M} \xi_{t|t-1}^{(m)} \cdot f(y_t | s_t = m, y_{1:t-1}) \right)lnL=t=1∑T​ln(m=1∑M​ξt∣t−1(m)​⋅f(yt​∣st​=m,y1:t−1​))
TTTsts_tst​

Log-likelihood of the observed data. The marginal density at each t integrates out the latent regime.

Initialize with ergodic probabilities

The ergodic (stationary) distribution of the Markov chain provides a natural uninformative starting point.
ξ0∣0=π,where π′P=π′,  π′1=1\xi_{0|0} = \pi, \quad \text{where } \pi' P = \pi', \; \pi' \mathbf{1} = 1ξ0∣0​=π,where π′P=π′,π′1=1
PPP

Predict: propagate through the transition matrix

Matrix multiplication: ξt∣t−1=P′ξt−1∣t−1\xi_{t|t-1} = P' \xi_{t-1|t-1}ξt∣t−1​=P′ξt−1∣t−1​. This is the Chapman-Kolmogorov equation applied one step forward.
ξt∣t−1(m)=∑i=1Mpim ξt−1∣t−1(i),m=1,…,M\xi_{t|t-1}^{(m)} = \sum_{i=1}^M p_{im} \, \xi_{t-1|t-1}^{(i)}, \quad m = 1, \ldots, Mξt∣t−1(m)​=i=1∑M​pim​ξt−1∣t−1(i)​,m=1,…,M

Evaluate the regime-conditional likelihoods

Compute the Gaussian density of yty_tyt​ under each regime's parameters. This is the only step that touches the data.
fm≡f(yt∣st=m,y1:t−1)for each mf_m \equiv f(y_t | s_t = m, y_{1:t-1}) \quad \text{for each } mfm​≡f(yt​∣st​=m,y1:t−1​)for each m
sts_tst​

Update: apply Bayes' rule

The posterior probability of regime m at time t, given data through t. The denominator is the predictive likelihood used for the log-likelihood computation.
ξt∣t(m)=ξt∣t−1(m)⋅fm∑j=1Mξt∣t−1(j)⋅fj\xi_{t|t}^{(m)} = \frac{\xi_{t|t-1}^{(m)} \cdot f_m}{\sum_{j=1}^M \xi_{t|t-1}^{(j)} \cdot f_j}ξt∣t(m)​=∑j=1M​ξt∣t−1(j)​⋅fj​ξt∣t−1(m)​⋅fm​​

Accumulate the log-likelihood

The predictive likelihood at each date is a byproduct of the filter. Summing gives the full log-likelihood needed for MLE.
ℓt=ln⁡(∑m=1Mξt∣t−1(m)⋅fm),ln⁡L=∑t=1Tℓt\ell_t = \ln \left( \sum_{m=1}^M \xi_{t|t-1}^{(m)} \cdot f_m \right), \quad \ln L = \sum_{t=1}^T \ell_tℓt​=ln(m=1∑M​ξt∣t−1(m)​⋅fm​),lnL=t=1∑T​ℓt​
TTT

Store filtered probabilities for smoothing

The smoothing pass needs both the filtered and predicted probabilities at every date.
Save {ξt∣t}t=1T and {ξt∣t−1}t=1T\text{Save } \{\xi_{t|t}\}_{t=1}^T \text{ and } \{\xi_{t|t-1}\}_{t=1}^TSave {ξt∣t​}t=1T​ and {ξt∣t−1​}t=1T​
ξt∣t\xi_{t|t}ξt∣t​TTT

Transition checks

Sampled dynamics
zt=G(zt−1;θ)+ηt,yt=Hzt+εtz_t=G(z_{t-1};\theta)+\eta_t,\qquad y_t=H z_t+\varepsilon_tzt​=G(zt−1​;θ)+ηt​,yt​=Hzt​+εt​

The state is updated at observed dates. A line between dates is a display convention unless the model specifies behavior inside the interval.

Read adjacent points as adjacent sample calculations. Smoothness is a property of the estimator, not of the chart line.

Do not infer within-period behavior from a sampled estimator.

Variable timing

Latent state

zt=G(zt−1;θ)+ηtz_t=G(z_{t-1};\theta)+\eta_tzt​=G(zt−1​;θ)+ηt​

The state updates at sample dates. The visible line is only as smooth as the estimated state sequence.

Observation equation

yt=Hzt+εty_t=Hz_t+\varepsilon_tyt​=Hzt​+εt​

Observed series can move differently from latent states because measurement noise and loadings matter.

Keep going

Read next

Continue with a related concept, model, or example.

Theory model

IS-LM Model

a well-connected theoretical model two steps from here

New angle: theoretical model →
Macro by Mark
Help
Privacy PolicyTerms of UseEthics and Compliance

© 2026 Mark Jayson Nation LLC