Mathematics of Hidden Markov Models: filtering, smoothing, and inference via Expectation-Maximization.
Model Definition
A Hidden Markov Model (HMM) consists of a sequence of hidden states with evolving over time, each generating an observation .
The factorization encodes two key conditional independence assumptions: (1) the evolution of latent states is 1-Markov, and (2) each observation depends only on the contemporaneous latent state.

Inference
Since only is observed, there are two major inference tasks: (1) filtering and (2) smoothing . With smoothing, observations beyond state are used to compute the posterior. From these results, other tasks like future latent states or observations become straightforward. We will first look at filtering.
Filtering: Inferring Current Latent State
Filtering is the task of computing the posterior over latent states given observations up to time . Writing
it suffices to track the numerator . Using the conditional independence implied by (1), and , we have
Definition (Forward variable). Define for .
Proposition (Forward recursion). The forward variables satisfy, for :
for :
The marginal likelihood (probability of the data) can be recovered by summing over the latent states :
Which gives us

Smoothing: Inferring an Intermediate Latent State
In the context of HMMs, smoothing refers to finding , making use of the full observation sequence to infer an intermediate latent state. Intuitively, even observations beyond can still carry information about the state of through the Markov chain. Starting with the object of interest
Focusing on the numerator:
which is using the conditional independence due to the HMM graph, :
Definition (Backward variable). Define .
Proposition (Backward recursion). For :
with terminal condition .
Proof.
and now because and :
and for , for convenience we define for .
Definition (Smoother). Define .
Proposition (Smoothing distribution). Putting together (5), for any ,
Parameter Fitting
The model parameters consist of the initial state distribution, the transition distribution between latent states, and the emission distribution associated with each latent state.
That is, the model parameters are:
Following the method of maximum likelihood estimation, we'd like to maximize the probability of the data given the parameters.
The sum over latent sequences appears inside the logarithm, which couples the parameters across all possible . This prevents the objective from decomposing into separable terms, and hence no closed-form maximizer exists.
Expectation-Maximization - E-step
Under EM we want to maximize the expected complete-data log-likelihood under the current posterior:
Moving expectations inside:
Definition (Pairwise Smoother). Define .
Writing in terms of objects we already have:
To summarize:
We have now computed the expectation of the log-likelihood under the posterior of the latent variables.
Expectation Maximization - M-step
Now, we need to update the parameters of our model . Since is the sum of three terms of distinct parameters, we can maximize each term independently. Starting with maximizing the first term :
Initial distribution.
Setting up the Lagrangian:
Now using the constraint:
and because the sum across all latent states and hence
Now we take a brief detour to prove a classic result which can be reused.
Lemma. The solution to subject to , , is
Proof. From the Lagrangian , take the derivative w.r.t. : . Solving: . The constraint then forces .
Transition matrix. The result applies to :
Considering a particular we'd like to maximize
Making use of the Lemma, we see this is maximized by
and because
Therefore, substituting this result into the denominator:
Remark. The updated transition parameters become the normalized estimated/"soft" (from the E-step) counts of transitions from .
The final term to deal with is the emission parameters. The optimization depends on whether we choose emission to be categorical, Gaussian, or some other distribution. For a categorical distribution, we can follow a similar approach using the general result as we did with and .
Let us also show the derivation for Gaussian emissions.
Emission parameters (Gaussian).
Let's start by finding :
Now, solving for the variance term:
The updates (7) and (8) are the standard Gaussian MLE estimates, except each observation is weighted by the posterior probability that it was generated by state .
EM Algorithm Summary (Vectorized)
Given current parameters , define the emission likelihood vector
Let denote elementwise multiplication.
E-step. Compute the forward vectors and backward vectors using
and
Then compute the smoothing probabilities
For , compute the pairwise smoothing probabilities in matrix form
where .
M-step. Update the parameters:
Then define the expected transition count matrix , in which each row encodes the number of expected transitions out of . Then the transition is the row update
Then for the Gaussian parameters, defining as the vector of observations and as stacked row-wise:
Define a residual matrix by
Then the variance update is
where denotes elementwise squaring and division is elementwise.