13  Hidden Markov Models and Regime Switching

Part III. Nonstationary and Seasonal Models
Chapter goal: learn how to model sequence data whose behavior changes across unobserved states. We develop hidden Markov models, forward filtering, backward smoothing, Viterbi decoding, Baum–Welch estimation, Gaussian emissions, regime-switching time series, forecasting, diagnostics, and AI-assisted model critique.

13.1 Learning objectives

After studying this chapter, you should be able to:

  1. explain why hidden regimes are useful for time series and sequence data;
  2. distinguish an observed sequence from an unobserved state sequence;
  3. write the joint probability of a hidden Markov model;
  4. implement the forward algorithm for likelihood evaluation;
  5. implement backward smoothing probabilities;
  6. use the Viterbi algorithm to decode the most likely hidden path;
  7. describe the Baum–Welch algorithm as an expectation–maximization method;
  8. fit and interpret a Gaussian HMM for regime-switching time series;
  9. compare HMMs with Kalman filters, ARIMA models, and recurrent neural networks;
  10. use AI tools to audit assumptions, code, diagnostics, and interpretation without outsourcing mathematical verification.

13.2 11.1 Why hidden regimes?

Many time series do not behave the same way at all times. A financial return series may alternate between calm and volatile periods. A machine sensor may switch between normal, warning, and failure modes. A biological signal may move between resting and active states. A user’s behavior sequence may switch between browsing, comparing, and purchasing modes.

A single stationary ARMA model may be too rigid for such data. Instead of assuming one global dependence structure, we allow the sequence to move through hidden regimes:

\[ S_t \in \{1,2,\ldots,K\}, \]

where \(S_t\) is not directly observed. The observed value \(Y_t\) is generated from a distribution that depends on \(S_t\).

For example, a two-regime return model might be

\[ Y_t \mid S_t=1 \sim N(0,\sigma_1^2), \qquad Y_t \mid S_t=2 \sim N(0,\sigma_2^2), \]

where \(\sigma_2^2 \gg \sigma_1^2\). State 1 represents a low-volatility regime and state 2 represents a high-volatility regime.

NoteThe hidden-state viewpoint

An HMM separates two questions:

  1. State dynamics: how the hidden regime evolves over time.
  2. Emission model: how the observed data are generated once the state is known.

This is the discrete-state analogue of the state-space model from Chapter 10.

13.3 11.2 Markov chains for latent states

The hidden state sequence \(S_1,S_2,\ldots,S_T\) is usually modeled as a Markov chain:

\[ P(S_t=j\mid S_{1:t-1})=P(S_t=j\mid S_{t-1}). \]

For \(K\) states, define the transition matrix

\[ \mathbf{A}=(a_{ij})_{i,j=1}^K, \qquad a_{ij}=P(S_t=j\mid S_{t-1}=i). \]

Each row of \(\mathbf{A}\) is a probability vector:

\[ a_{ij}\geq 0, \qquad \sum_{j=1}^K a_{ij}=1. \]

The initial distribution is

\[ \boldsymbol{\pi}=(\pi_1,\ldots,\pi_K), \qquad \pi_j=P(S_1=j). \]

For a two-state model,

\[ \mathbf{A}=\begin{bmatrix} p_{11} & 1-p_{11}\\ 1-p_{22} & p_{22} \end{bmatrix}. \]

Large values of \(p_{11}\) and \(p_{22}\) make regimes persistent. In applications, persistence is often important: volatility clusters, machines remain in a degraded state for some time, and users stay in a behavioral mode for several steps.

13.3.1 Expected regime duration

If state \(j\) has self-transition probability \(a_{jj}\), then the duration spent in state \(j\) before leaving is geometrically distributed:

\[ P(D_j=d)=a_{jj}^{d-1}(1-a_{jj}), \qquad d=1,2,\ldots. \]

Therefore,

\[ E[D_j]=\frac{1}{1-a_{jj}}. \]

This gives a useful interpretation of transition probabilities. If \(a_{jj}=0.95\), the expected duration is \(20\) time steps.

13.4 11.3 Hidden Markov model definition

A hidden Markov model has three pieces:

\[ \lambda=(\boldsymbol{\pi},\mathbf{A},\mathbf{B}), \]

where \(\boldsymbol{\pi}\) is the initial distribution, \(\mathbf{A}\) is the transition matrix, and \(\mathbf{B}\) describes the emission distributions.

For observations \(Y_1,\ldots,Y_T\), the model assumptions are:

  1. The hidden states form a Markov chain:

\[ P(S_t\mid S_{1:t-1})=P(S_t\mid S_{t-1}). \]

  1. Given the current state, the current observation is conditionally independent of all other variables:

\[ P(Y_t\mid S_{1:T},Y_{1:t-1},Y_{t+1:T})=P(Y_t\mid S_t). \]

  1. The emission density or probability mass function for state \(j\) is

\[ b_j(y)=p(y\mid S_t=j). \]

The joint probability of one hidden path \(s_{1:T}\) and observed sequence \(y_{1:T}\) is

\[ P(s_{1:T},y_{1:T}) =\pi_{s_1} b_{s_1}(y_1) \prod_{t=2}^T a_{s_{t-1},s_t} b_{s_t}(y_t). \]

This single formula is the source of the main algorithms in this chapter.

TipHMMs for different data types

The emission model can be changed depending on the observation type:

  • categorical sequence: \(b_j(y)\) is a categorical distribution;
  • count sequence: \(b_j(y)\) may be Poisson or negative binomial;
  • real-valued time series: \(b_j(y)\) may be Gaussian or Student-\(t\);
  • multivariate sensor data: \(b_j(\mathbf{y})\) may be multivariate Gaussian;
  • text or event sequences: emissions may be learned embeddings or neural distributions.

13.5 11.4 The three computational problems

For a given HMM, there are three classical computational tasks.

13.5.1 Problem 1: Evaluation

Compute the likelihood

\[ p(y_{1:T}\mid \lambda) =\sum_{s_1=1}^K\cdots\sum_{s_T=1}^K P(s_{1:T},y_{1:T}\mid \lambda). \]

A direct sum over all possible hidden paths has \(K^T\) terms, which is impossible for large \(T\). The forward algorithm computes the same likelihood in \(O(TK^2)\) time.

13.5.2 Problem 2: Decoding

Find the most likely hidden path:

\[ \widehat{s}_{1:T} =\arg\max_{s_{1:T}} P(s_{1:T}\mid y_{1:T},\lambda). \]

The Viterbi algorithm solves a closely related dynamic-programming problem efficiently.

13.5.3 Problem 3: Learning

Estimate the parameters \(\lambda=(\boldsymbol{\pi},\mathbf{A},\mathbf{B})\) from data. When the states are hidden, maximum likelihood is usually solved by the Baum–Welch algorithm, an expectation–maximization method.

13.6 11.5 Forward algorithm: filtering and likelihood

Define the forward variable

\[ \alpha_t(j)=P(y_1,\ldots,y_t,S_t=j\mid \lambda). \]

This is the joint probability of observing the data through time \(t\) and ending in state \(j\).

13.6.1 Initialization

\[ \alpha_1(j)=\pi_j b_j(y_1), \qquad j=1,\ldots,K. \]

13.6.2 Recursion

For \(t=2,\ldots,T\),

\[ \alpha_t(j)=b_j(y_t)\sum_{i=1}^K \alpha_{t-1}(i)a_{ij}. \]

13.6.3 Likelihood

At the end,

\[ p(y_{1:T}\mid \lambda)=\sum_{j=1}^K \alpha_T(j). \]

The recursion is dynamic programming. Instead of enumerating all paths, it aggregates all paths that end in the same current state.

ImportantScaling is necessary

For long sequences, \(\alpha_t(j)\) becomes extremely small. In practice we use scaled forward probabilities or log probabilities. Without scaling, numerical underflow can make the likelihood appear to be zero.

13.6.4 Scaled forward recursion

Let \(\widetilde{\alpha}_t(j)\) be the unnormalized forward update. Define

\[ c_t=\sum_{j=1}^K \widetilde{\alpha}_t(j), \qquad \widehat{\alpha}_t(j)=\frac{\widetilde{\alpha}_t(j)}{c_t}. \]

Then \(\widehat{\alpha}_t(j)\) is a normalized filtering distribution:

\[ \widehat{\alpha}_t(j)=P(S_t=j\mid y_{1:t},\lambda). \]

The log-likelihood is recovered as

\[ \log p(y_{1:T}\mid \lambda)=\sum_{t=1}^T \log c_t. \]

This scaled form is the version students should implement first.

13.7 11.6 Backward algorithm and smoothing

The forward distribution uses observations through time \(t\). Sometimes we want to estimate the state at time \(t\) using the full sample \(y_{1:T}\). This is smoothing.

Define the backward variable

\[ \beta_t(i)=P(y_{t+1:T}\mid S_t=i,\lambda). \]

13.7.1 Initialization

\[ \beta_T(i)=1, \qquad i=1,\ldots,K. \]

13.7.2 Recursion

For \(t=T-1,T-2,\ldots,1\),

\[ \beta_t(i)=\sum_{j=1}^K a_{ij} b_j(y_{t+1})\beta_{t+1}(j). \]

The smoothed state probability is

\[ \gamma_t(j) =P(S_t=j\mid y_{1:T},\lambda) =\frac{\alpha_t(j)\beta_t(j)}{\sum_{\ell=1}^K \alpha_t(\ell)\beta_t(\ell)}. \]

Filtering answers: “What state do we believe now using data so far?”
Smoothing answers: “What state do we believe then after seeing the whole sequence?”

13.8 11.7 Two-slice probabilities

For learning transition probabilities, we need the posterior probability of consecutive state pairs:

\[ \xi_t(i,j)=P(S_t=i,S_{t+1}=j\mid y_{1:T},\lambda). \]

Using the forward and backward variables,

\[ \xi_t(i,j) =\frac{\alpha_t(i)a_{ij}b_j(y_{t+1})\beta_{t+1}(j)} {\sum_{r=1}^K\sum_{s=1}^K \alpha_t(r)a_{rs}b_s(y_{t+1})\beta_{t+1}(s)}. \]

The numerator is the probability of:

  1. explaining the past and ending in state \(i\) at time \(t\);
  2. transitioning from \(i\) to \(j\);
  3. emitting \(y_{t+1}\) from state \(j\);
  4. explaining the future after time \(t+1\).

13.9 11.8 Viterbi decoding

The Viterbi algorithm finds the most likely state path under the model. Define

\[ \delta_t(j)=\max_{s_{1:t-1}} P(s_{1:t-1},S_t=j,y_{1:t}\mid \lambda). \]

This is the probability of the most likely partial path ending in state \(j\) at time \(t\).

13.9.1 Initialization

\[ \delta_1(j)=\pi_j b_j(y_1). \]

13.9.2 Recursion

For \(t=2,\ldots,T\),

\[ \delta_t(j)=b_j(y_t)\max_{i=1,\ldots,K}\delta_{t-1}(i)a_{ij}. \]

We also store the maximizing previous state:

\[ \psi_t(j)=\arg\max_{i=1,\ldots,K}\delta_{t-1}(i)a_{ij}. \]

13.9.3 Backtracking

The final state is

\[ \widehat{s}_T=\arg\max_j \delta_T(j). \]

Then recursively,

\[ \widehat{s}_t=\psi_{t+1}(\widehat{s}_{t+1}), \qquad t=T-1,\ldots,1. \]

In numerical work, we implement Viterbi in log scale:

\[ \log \delta_t(j)=\log b_j(y_t)+\max_i\left\{\log \delta_{t-1}(i)+\log a_{ij}\right\}. \]

WarningViterbi path vs smoothed state probabilities

The Viterbi path is the single most likely complete path. The smoothed state probabilities give marginal probabilities at each time. They are not the same object. A path formed by taking \(\arg\max_j \gamma_t(j)\) at every time may not be the globally most likely path.

13.10 11.9 Baum–Welch learning as EM

Suppose the parameters are unknown. If the hidden states were observed, estimating the transition probabilities would be easy:

\[ \widehat{a}_{ij} =\frac{\#\{t:S_t=i,S_{t+1}=j\}} {\#\{t:S_t=i\}}. \]

But the states are hidden. The expectation–maximization idea is to replace unobserved counts by expected counts under the current model.

13.10.1 E-step

Run forward–backward under the current parameter values and compute

\[ \gamma_t(j)=P(S_t=j\mid y_{1:T},\lambda^{old}), \]

and

\[ \xi_t(i,j)=P(S_t=i,S_{t+1}=j\mid y_{1:T},\lambda^{old}). \]

13.10.2 M-step for transition probabilities

Update

\[ \pi_j^{new}=\gamma_1(j), \]

and

\[ a_{ij}^{new} =\frac{\sum_{t=1}^{T-1}\xi_t(i,j)} {\sum_{t=1}^{T-1}\gamma_t(i)}. \]

13.10.3 M-step for Gaussian emissions

For a Gaussian HMM with

\[ Y_t\mid S_t=j\sim N(\mu_j,\sigma_j^2), \]

the weighted maximum likelihood updates are

\[ \mu_j^{new} =\frac{\sum_{t=1}^T \gamma_t(j)y_t} {\sum_{t=1}^T \gamma_t(j)}, \]

and

\[ (\sigma_j^2)^{new} =\frac{\sum_{t=1}^T \gamma_t(j)(y_t-\mu_j^{new})^2} {\sum_{t=1}^T \gamma_t(j)}. \]

The EM algorithm increases the likelihood at each iteration, but it may converge to a local maximum. Initialization matters.

13.11 11.10 Gaussian HMM for time series

A Gaussian HMM is often the first regime-switching model to try for real-valued data:

\[ S_t\mid S_{t-1}\sim \text{Categorical}(\mathbf{A}_{S_{t-1},:}), \]

\[ Y_t\mid S_t=j\sim N(\mu_j,\sigma_j^2). \]

This model captures changing mean and variance. A two-state Gaussian HMM might have:

\[ \mu_1\approx 0,\quad \sigma_1 \text{ small}, \]

and

\[ \mu_2\approx 0,\quad \sigma_2 \text{ large}. \]

Then the hidden states represent low-volatility and high-volatility regimes.

13.11.1 What this model assumes

Conditional on the hidden state \(S_t\), observations are independent over time:

\[ Y_t \perp Y_s \mid S_t,S_s \quad \text{for }t\neq s. \]

Dependence in \(Y_t\) comes from persistence in the hidden Markov chain. If the data have strong linear autocorrelation after conditioning on state, we may need a regime-switching autoregressive model.

13.12 11.11 Regime-switching autoregressive models

A more time-series-specific extension is a Markov-switching autoregression:

\[ Y_t=c_{S_t}+\phi_{S_t}Y_{t-1}+\varepsilon_t, \qquad \varepsilon_t\mid S_t=j\sim N(0,\sigma_j^2). \]

Each regime has its own AR coefficient, intercept, and variance. For example:

  • state 1: stable mean-reverting behavior;
  • state 2: weak mean reversion and high volatility;
  • state 3: negative mean or crisis dynamics.

The emission density now depends on \(Y_{t-1}\):

\[ b_j(y_t\mid y_{t-1}) =\frac{1}{\sqrt{2\pi\sigma_j^2}} \exp\left[-\frac{(y_t-c_j-\phi_j y_{t-1})^2}{2\sigma_j^2}\right]. \]

The forward algorithm still applies if we replace \(b_j(y_t)\) by \(b_j(y_t\mid y_{t-1})\).

TipConnection to ARIMA and Kalman filtering

ARIMA uses one linear dynamic law for the entire sample. A Markov-switching AR model uses several possible dynamic laws and lets a hidden Markov chain choose which law is active. Kalman filtering uses continuous latent states; HMMs use discrete latent states.

13.13 11.12 Forecasting with HMMs

Suppose we have filtering probabilities at time \(T\):

\[ \mathbf{p}_{T\mid T} =\left(P(S_T=1\mid y_{1:T}),\ldots,P(S_T=K\mid y_{1:T})\right). \]

The \(h\)-step-ahead state distribution is

\[ \mathbf{p}_{T+h\mid T}=\mathbf{p}_{T\mid T}\mathbf{A}^h. \]

For a Gaussian HMM, the predictive distribution is a mixture:

\[ p(y_{T+h}\mid y_{1:T}) =\sum_{j=1}^K p_{T+h\mid T}(j)\,N(y_{T+h};\mu_j,\sigma_j^2). \]

The forecast mean is

\[ E(Y_{T+h}\mid y_{1:T}) =\sum_{j=1}^K p_{T+h\mid T}(j)\mu_j. \]

The forecast variance follows the law of total variance:

\[ \operatorname{Var}(Y_{T+h}\mid y_{1:T}) =\sum_{j=1}^K p_{T+h\mid T}(j)\sigma_j^2 +\sum_{j=1}^K p_{T+h\mid T}(j) \left(\mu_j-\bar{\mu}_{T+h}\right)^2, \]

where

\[ \bar{\mu}_{T+h}=\sum_{j=1}^K p_{T+h\mid T}(j)\mu_j. \]

The first term is within-regime uncertainty. The second term is between-regime uncertainty.

13.14 11.13 Interactive HTML: hidden regime simulator

The following interactive visualization simulates a two-state Gaussian HMM. Change the transition probabilities, means, and standard deviations. Notice how persistent hidden states create clustered behavior in the observations.

NoteHow to use the simulator

Try three experiments:

  1. Set \(p_{11}=p_{22}=0.50\). Regimes switch frequently.
  2. Set \(p_{11}=p_{22}=0.95\). Regimes become persistent.
  3. Make \(\sigma_2\) much larger than \(\sigma_1\). The high-volatility regime becomes visually obvious.

Two-State Gaussian HMM Simulator

13.15 11.14 Python example: simulate a Gaussian HMM

The next code block simulates a two-state Gaussian HMM without using a specialized HMM package. This is useful pedagogically because the model assumptions are completely visible.

Code
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(7339)

T = 300
pi = np.array([0.6, 0.4])
A = np.array([
    [0.95, 0.05],
    [0.08, 0.92]
])
mu = np.array([0.0, 2.5])
sigma = np.array([0.45, 1.25])

states = np.zeros(T, dtype=int)
y = np.zeros(T)

states[0] = rng.choice(2, p=pi)
y[0] = rng.normal(mu[states[0]], sigma[states[0]])

for t in range(1, T):
    states[t] = rng.choice(2, p=A[states[t - 1]])
    y[t] = rng.normal(mu[states[t]], sigma[states[t]])

plt.figure(figsize=(10, 4))
plt.plot(y, linewidth=1.2, label="observation")
plt.step(np.arange(T), states + 1, where="post", linewidth=1.0, label="hidden state")
plt.title("Simulated two-state Gaussian HMM")
plt.xlabel("time")
plt.legend()
plt.show()

A useful first diagnostic is to compare the sample behavior within the true states.

Code
for j in range(2):
    values = y[states == j]
    print(f"state {j + 1}: count={len(values):3d}, sample mean={values.mean(): .3f}, sample sd={values.std(ddof=1): .3f}")
state 1: count=242, sample mean=-0.023, sample sd= 0.438
state 2: count= 58, sample mean= 2.536, sample sd= 1.305

13.16 11.15 Python example: scaled forward filtering

We now implement the forward algorithm. The function below returns the filtered state probabilities and the log-likelihood.

Code
def normal_pdf(x, mean, sd):
    """Gaussian density evaluated at x."""
    z = (x - mean) / sd
    return np.exp(-0.5 * z * z) / (np.sqrt(2 * np.pi) * sd)


def forward_scaled(y, pi, A, mu, sigma):
    """Scaled forward algorithm for a Gaussian HMM."""
    y = np.asarray(y)
    K = len(pi)
    T = len(y)

    alpha = np.zeros((T, K))
    scales = np.zeros(T)

    emission = np.array([normal_pdf(y[0], mu[j], sigma[j]) for j in range(K)])
    unnormalized = pi * emission
    scales[0] = unnormalized.sum()
    alpha[0] = unnormalized / scales[0]

    for t in range(1, T):
        prediction = alpha[t - 1] @ A
        emission = np.array([normal_pdf(y[t], mu[j], sigma[j]) for j in range(K)])
        unnormalized = prediction * emission
        scales[t] = unnormalized.sum()
        alpha[t] = unnormalized / scales[t]

    loglik = np.log(scales).sum()
    return alpha, loglik

alpha, loglik = forward_scaled(y, pi, A, mu, sigma)
print(f"Log-likelihood under the true parameters: {loglik:.2f}")
print("Last filtered state probability:", alpha[-1])
Log-likelihood under the true parameters: -305.61
Last filtered state probability: [0.99479003 0.00520997]

Plotting the filtered probability of state 2 shows how the model tracks the hidden regime.

Code
plt.figure(figsize=(10, 4))
plt.plot(alpha[:, 1], label="P(S_t = 2 | y_1:t)")
plt.step(np.arange(T), states, where="post", alpha=0.45, label="true state 2 indicator")
plt.ylim(-0.05, 1.05)
plt.title("Filtered probability of state 2")
plt.xlabel("time")
plt.legend()
plt.show()

13.17 11.16 Python example: Viterbi decoding

The following function implements Viterbi decoding in log scale.

Code
def viterbi_gaussian(y, pi, A, mu, sigma):
    """Most likely state path for a Gaussian HMM."""
    y = np.asarray(y)
    K = len(pi)
    T = len(y)

    log_pi = np.log(pi)
    log_A = np.log(A)

    log_delta = np.zeros((T, K))
    psi = np.zeros((T, K), dtype=int)

    log_emission = np.array([
        np.log(normal_pdf(y[0], mu[j], sigma[j])) for j in range(K)
    ])
    log_delta[0] = log_pi + log_emission

    for t in range(1, T):
        for j in range(K):
            candidates = log_delta[t - 1] + log_A[:, j]
            psi[t, j] = np.argmax(candidates)
            log_delta[t, j] = candidates[psi[t, j]] + np.log(normal_pdf(y[t], mu[j], sigma[j]))

    path = np.zeros(T, dtype=int)
    path[-1] = np.argmax(log_delta[-1])
    for t in range(T - 2, -1, -1):
        path[t] = psi[t + 1, path[t + 1]]

    return path, log_delta

path, log_delta = viterbi_gaussian(y, pi, A, mu, sigma)
accuracy = np.mean(path == states)
print(f"Viterbi state decoding accuracy against simulated truth: {accuracy:.3f}")
Viterbi state decoding accuracy against simulated truth: 0.993
Code
plt.figure(figsize=(10, 4))
plt.step(np.arange(T), states + 1, where="post", label="true hidden state")
plt.step(np.arange(T), path + 1, where="post", linestyle="--", label="Viterbi decoded state")
plt.yticks([1, 2])
plt.title("True hidden path versus Viterbi decoded path")
plt.xlabel("time")
plt.legend()
plt.show()

13.18 11.17 Python example: Baum–Welch for a Gaussian HMM

Here is a compact educational implementation of EM for a Gaussian HMM. The goal is clarity, not production-level numerical robustness.

Code
def forward_backward_scaled(y, pi, A, mu, sigma):
    """Forward-backward algorithm for a Gaussian HMM."""
    y = np.asarray(y)
    K = len(pi)
    T = len(y)

    alpha, loglik = forward_scaled(y, pi, A, mu, sigma)

    beta = np.ones((T, K))
    scales = np.zeros(T)

    # Recompute scales from the forward pass.
    emission0 = np.array([normal_pdf(y[0], mu[j], sigma[j]) for j in range(K)])
    scales[0] = (pi * emission0).sum()
    for t in range(1, T):
        pred = alpha[t - 1] @ A
        emission = np.array([normal_pdf(y[t], mu[j], sigma[j]) for j in range(K)])
        scales[t] = (pred * emission).sum()

    for t in range(T - 2, -1, -1):
        emission_next = np.array([normal_pdf(y[t + 1], mu[j], sigma[j]) for j in range(K)])
        beta[t] = A @ (emission_next * beta[t + 1])
        beta[t] = beta[t] / scales[t + 1]

    gamma = alpha * beta
    gamma = gamma / gamma.sum(axis=1, keepdims=True)

    xi = np.zeros((T - 1, K, K))
    for t in range(T - 1):
        emission_next = np.array([normal_pdf(y[t + 1], mu[j], sigma[j]) for j in range(K)])
        numerator = alpha[t, :, None] * A * emission_next[None, :] * beta[t + 1, None, :]
        xi[t] = numerator / numerator.sum()

    return gamma, xi, loglik


def baum_welch_gaussian(y, K=2, n_iter=30, seed=1):
    """Simple EM algorithm for a Gaussian HMM."""
    rng = np.random.default_rng(seed)
    y = np.asarray(y)
    T = len(y)

    pi_est = np.ones(K) / K
    A_est = np.full((K, K), 1 / K)
    A_est = 0.85 * np.eye(K) + 0.15 * A_est
    mu_est = np.quantile(y, np.linspace(0.25, 0.75, K))
    sigma_est = np.full(K, np.std(y))

    history = []
    for _ in range(n_iter):
        gamma, xi, loglik = forward_backward_scaled(y, pi_est, A_est, mu_est, sigma_est)
        history.append(loglik)

        pi_est = gamma[0]
        A_est = xi.sum(axis=0) / gamma[:-1].sum(axis=0, keepdims=True).T

        weights = gamma.sum(axis=0)
        mu_est = (gamma * y[:, None]).sum(axis=0) / weights
        sigma_est = np.sqrt((gamma * (y[:, None] - mu_est[None, :])**2).sum(axis=0) / weights)
        sigma_est = np.maximum(sigma_est, 1e-4)

    return pi_est, A_est, mu_est, sigma_est, np.array(history)

pi_hat, A_hat, mu_hat, sigma_hat, history = baum_welch_gaussian(y, K=2, n_iter=40, seed=5)
print("Estimated pi:", np.round(pi_hat, 3))
print("Estimated A:\n", np.round(A_hat, 3))
print("Estimated means:", np.round(mu_hat, 3))
print("Estimated standard deviations:", np.round(sigma_hat, 3))
Estimated pi: [1. 0.]
Estimated A:
 [[0.95 0.05]
 [0.21 0.79]]
Estimated means: [-0.029  2.591]
Estimated standard deviations: [0.434 1.221]
Code
plt.figure(figsize=(8, 4))
plt.plot(history, marker="o")
plt.title("Baum--Welch log-likelihood trace")
plt.xlabel("EM iteration")
plt.ylabel("log-likelihood")
plt.show()

The likelihood should generally increase. Small numerical decreases can occur if the implementation is unstable, but a clean EM implementation should be monotone up to floating-point error.

13.19 11.18 Model selection and diagnostics

Choosing the number of hidden states \(K\) is a modeling decision. More states increase flexibility but can make interpretation unstable.

Common strategies include:

  1. Domain interpretation: choose states that correspond to meaningful regimes.
  2. Information criteria: compare AIC or BIC across values of \(K\).
  3. Out-of-sample performance: compare forecast or classification performance.
  4. State persistence: check whether transition probabilities are interpretable.
  5. Posterior uncertainty: inspect whether state probabilities are decisive or ambiguous.
  6. Residual checks: after conditioning on decoded or smoothed states, examine residual behavior.

For a fitted Gaussian HMM, state-specific standardized residuals are

\[ r_t^{(j)}=\frac{y_t-\widehat{\mu}_j}{\widehat{\sigma}_j}. \]

A soft residual can use smoothed probabilities:

\[ \widehat{\mu}_t=\sum_{j=1}^K \gamma_t(j)\widehat{\mu}_j, \]

\[ \widehat{\sigma}_t^2 =\sum_{j=1}^K \gamma_t(j)\left[\widehat{\sigma}_j^2+(\widehat{\mu}_j-\widehat{\mu}_t)^2\right], \]

and

\[ r_t=\frac{y_t-\widehat{\mu}_t}{\widehat{\sigma}_t}. \]

Then we can inspect time plots, ACF plots, and distributional diagnostics of \(r_t\).

WarningLabel switching

The labels of hidden states are arbitrary. If state 1 and state 2 are swapped, the likelihood is unchanged. Interpret states by their estimated means, variances, persistence, or application meaning, not by their numeric labels.

13.20 11.19 HMMs and modern AI sequence models

HMMs are classical probabilistic sequence models. They remain important because they expose ideas that also appear in modern AI systems.

HMM idea Modern sequence-learning analogue
Hidden state \(S_t\) latent representation or memory state
Transition matrix \(\mathbf{A}\) learned state update rule
Emission distribution \(b_j(y)\) decoder or output distribution
Forward filtering sequential inference
Viterbi decoding most likely structured prediction
Baum–Welch latent-variable training
Regime probabilities interpretable uncertainty over modes

RNNs, LSTMs, GRUs, and transformers replace discrete hand-specified state dynamics with flexible learned functions. However, HMMs remain useful when interpretability, small data, uncertainty, or regime structure matters.

13.21 11.20 AI-assisted modeling component

AI tools can help with HMMs, but they often make subtle mistakes with scaling, indexing, and interpretation. Use AI as a critic and collaborator, not as the source of truth.

TipPrompt: code audit

I implemented the scaled forward algorithm for a Gaussian HMM. Please check the dimensions, normalization, likelihood calculation, and possible underflow problems. Do not rewrite the code first. Identify mathematical or indexing mistakes and explain how to test each one.

TipPrompt: model critique

I fitted a two-state Gaussian HMM to a financial return series. State 1 has low variance and state 2 has high variance. The transition probabilities are 0.97 and 0.91 on the diagonal. What diagnostics should I check before claiming these are calm and crisis regimes? Include tests for residual autocorrelation, outliers, label switching, and stability across initializations.

TipPrompt: interpretation guardrail

Here is a plot of smoothed state probabilities from an HMM. Help me write a cautious interpretation. Avoid saying the hidden state is directly observed. Use language such as “the model assigns high posterior probability to…” and distinguish model-based regimes from true causal regimes.

13.21.1 Human verification checklist

Whenever an AI tool helps with HMM code or interpretation, manually check:

  1. rows of \(\mathbf{A}\) sum to one;
  2. all probabilities are nonnegative;
  3. forward probabilities are normalized at each time;
  4. log-likelihood is computed from scaling constants or log-sum-exp;
  5. Viterbi output uses backpointers correctly;
  6. EM likelihood increases across iterations;
  7. fitted states are stable across random initializations;
  8. interpretation does not confuse latent states with observed ground truth.

13.22 11.21 Common mistakes

WarningMistake 1: treating hidden states as observed labels

The hidden states are model-based latent variables. Unless the data include true labels, decoded states are estimates, not facts.

WarningMistake 2: ignoring numerical scaling

Unscaled forward probabilities usually underflow for long sequences. Use scaled probabilities or log-sum-exp.

WarningMistake 3: comparing state labels across fits

State 1 in one fit may correspond to state 2 in another. Compare states by their parameters, not their labels.

WarningMistake 4: using too many states

A model with many states may fit the training sequence better but produce unstable, uninterpretable, or non-reproducible regimes.

WarningMistake 5: forgetting time-series dependence inside regimes

A Gaussian HMM may not remove autocorrelation. If residuals remain autocorrelated within states, consider a Markov-switching AR model.

13.23 11.22 Exercises

13.23.1 Conceptual exercises

  1. Explain the difference between a Markov chain and a hidden Markov model.
  2. In a two-state HMM with \(a_{11}=0.98\) and \(a_{22}=0.80\), compute the expected duration of each state.
  3. Explain why direct likelihood computation requires summing over \(K^T\) paths.
  4. Explain the difference between filtering probability, smoothing probability, and Viterbi decoding.
  5. Give an example where the Viterbi path and the pointwise most likely states may differ.

13.23.2 Mathematical exercises

  1. Derive the forward recursion from the joint probability formula.
  2. Derive the backward recursion.
  3. Show that \(\sum_j \gamma_t(j)=1\).
  4. Derive the Baum–Welch update for \(a_{ij}\) using expected transition counts.
  5. Derive the weighted Gaussian updates for \(\mu_j\) and \(\sigma_j^2\).
  6. For a Gaussian HMM, derive the one-step forecast mean and variance using the law of total expectation and law of total variance.

13.23.3 Computational exercises

  1. Simulate a three-state Gaussian HMM and plot the hidden states and observations.
  2. Implement the scaled forward algorithm and verify that filtered probabilities sum to one.
  3. Implement Viterbi decoding and compare the decoded path to the true simulated path.
  4. Fit a two-state Gaussian HMM using the Baum–Welch code and test several random initializations.
  5. Modify the emission distribution to use state-dependent Poisson distributions for count data.
  6. Implement a Markov-switching AR(1) filter by replacing \(b_j(y_t)\) with \(b_j(y_t\mid y_{t-1})\).

13.24 11.23 Mini project: regime detection in a real sequence

Choose one real sequence dataset. Possible choices include:

  • daily financial returns;
  • hourly electricity demand changes;
  • machine vibration or sensor data;
  • weather anomalies;
  • web traffic or user activity counts;
  • biological signal measurements.

Your report should include:

  1. exploratory plots and summary statistics;
  2. justification for the number of states \(K\);
  3. fitted transition matrix and emission parameters;
  4. filtered and smoothed state probability plots;
  5. Viterbi decoded path;
  6. residual diagnostics;
  7. out-of-sample forecasting or anomaly-detection evaluation;
  8. an AI-assisted critique section describing how you used AI and how you verified its suggestions.

13.25 11.24 Chapter checklist

By the end of this chapter, you should be able to:

13.26 11.25 Looking ahead

State-space models and HMMs provide two classical hidden-state frameworks:

\[ \text{continuous hidden state} \quad \leftrightarrow \quad \text{Kalman filter}, \]

\[ \text{discrete hidden state} \quad \leftrightarrow \quad \text{HMM}. \]

The next part of the book moves from hidden-state time-domain models to frequency-domain methods. Instead of asking which hidden regime generated the observation, we ask which oscillatory components explain the variation in the sequence.