Baum-Welch algorithm

On this page 7

The Baum-Welch algorithm is an expectation-maximization (EM) procedure that estimates the parameters of a hidden Markov model (HMM) from observation sequences whose hidden states are never seen. Within probabilistic models, it is the standard way to train an HMM without labeled state sequences, and it was developed by Leonard Baum and colleagues in the late 1960s and early 1970s.

The algorithm alternates between computing expected state occupancies and transitions with the forward-backward algorithm and re-estimating the transition, emission and initial probabilities from those expectations. Each iteration is guaranteed not to decrease the likelihood of the training data, but the procedure converges only to a local maximum, so initialization matters a great deal.

The hidden Markov model parameters Baum-Welch estimates

Baum-Welch estimates the full parameter set of an HMM, usually written as lambda = (A, B, pi). The model assumes a hidden state sequence that follows a first-order Markov chain and an observation at each time step that depends only on the current hidden state.

SymbolMeaningShape or formConstraint
AState transition probabilitiesN x N matrixEach row sums to 1
BEmission probabilities or densitiesN x M matrix, or Gaussian parametersEach distribution normalized
piInitial state distributionVector of length NSums to 1
NNumber of hidden statesInteger chosen in advanceNot learned by the algorithm
HMM parameters learned by the Baum-Welch algorithm

The number of hidden states is a modeling choice, not something Baum-Welch discovers. Practitioners select it with domain knowledge or with criteria such as the Bayesian information criterion, comparing models trained with different values of N.

How the expectation and maximization steps work

Each Baum-Welch iteration has an expectation step that computes posterior quantities under the current parameters and a maximization step that turns them into new parameters. The E-step relies on the forward-backward algorithm, which runs in O(N squared times T) time for a sequence of length T.

  1. Forward pass: compute alpha(t, i), the joint probability of the observations up to time t and being in state i at t.
  2. Backward pass: compute beta(t, i), the probability of the remaining observations given state i at time t.
  3. State posteriors: gamma(t, i) is proportional to alpha(t, i) times beta(t, i) and gives the probability of state i at time t.
  4. Transition posteriors: xi(t, i, j) gives the probability of moving from state i to state j between t and t+1.
  5. Re-estimation: new A, B and pi are ratios of expected counts derived from gamma and xi.

The re-estimation formulas are intuitive. A new transition probability equals the expected number of i-to-j transitions divided by the expected number of visits to state i. For discrete emissions, a new emission probability is the expected time spent in a state while emitting a given symbol, divided by the total expected time in that state.

QuantityComputed inUsed for
Forward variable alphaE-step, left to rightSequence likelihood and posteriors
Backward variable betaE-step, right to leftPosteriors
GammaE-stepUpdating pi and B
XiE-stepUpdating A
Log-likelihoodAfter E-stepMonitoring convergence
Quantities computed in one Baum-Welch iteration

Baum-Welch is often confused with other HMM algorithms because they share the same dynamic programming machinery. The key difference is the question each algorithm answers: learning parameters, scoring a sequence or decoding the best state path.

MethodTaskOutputTraining data needed
Baum-WelchLearningParameters A, B, piUnlabeled observations
Forward-backwardInferenceState posteriorsTrained model
Viterbi algorithmDecodingSingle most likely state pathTrained model
Viterbi trainingLearning, hard assignmentsParametersUnlabeled observations
Supervised countingLearningParametersLabeled state sequences
Baum-Welch and neighboring HMM methods

Viterbi training is faster but uses hard state assignments, which tends to give biased estimates. Bayesian alternatives, such as variational inference or Gibbs sampling over HMM parameters, replace point estimates with posterior distributions and reduce overfitting when data are scarce.

Practical implementation details

A correct implementation must handle numerical underflow, because products of many probabilities shrink below floating-point range within a few hundred steps. Two standard remedies are per-step scaling of alpha and beta and computing everything in log space with the log-sum-exp operation.

  • Multiple sequences: sum expected counts over all training sequences before the M-step.
  • Continuous observations: use Gaussian or Gaussian mixture emissions, updated with posterior-weighted means and covariances.
  • Convergence test: stop when the log-likelihood gain falls below a small threshold or after a fixed iteration limit.
  • Smoothing: add pseudocounts or Dirichlet priors so unseen events do not receive zero probability.

The algorithm is sensitive to initialization. Common strategies include random restarts with selection by likelihood, initializing emissions with k-means clustering, and imposing structure such as left-to-right topologies in speech recognition, where zeros in A stay zero throughout training.

Limitations and common mistakes

Baum-Welch is reliable within its assumptions, but those assumptions are strong. The table below lists the problems practitioners meet most often and their standard remedies.

SymptomLikely causeRemedy
Likelihood becomes NaNUnderflow or zero probabilitiesScaling, log space, pseudocounts
Poor final modelBad local maximumMultiple restarts, informed initialization
Collapsing Gaussian varianceState fits a single pointVariance floor or prior
Excellent training fit, poor test fitToo many statesFewer states, model selection criteria
Uninterpretable statesLabel switching across runsAlign states after training
Typical problems when training with Baum-Welch

Because HMMs assume conditionally independent emissions and Markovian dynamics, Baum-Welch cannot capture long-range dependencies. Many sequence tasks once handled with HMMs now use recurrent or transformer networks, yet Baum-Welch remains valuable where data are limited, interpretability is required or the generative structure is known.

A non-technical view

Imagine watching a friend’s moods only through the clothes they wear each day. You never see the mood directly. You only see the outfits. The Baum-Welch algorithm is a way to guess the hidden pattern behind what you can see.

It starts with a rough guess about how moods change and which outfits go with which mood. Then it checks that guess against the outfits it actually saw. It adjusts the guess and checks again. Each round makes the explanation fit the observations a little better, until further changes stop helping.

A concrete example

A bioinformatics team wants to find CpG islands, regions of DNA rich in cytosine-guanine pairs, in a newly sequenced genome with no annotations. They define an HMM with two hidden states, island and background, and four emission symbols for the nucleotides A, C, G and T.

They initialize the model with mildly different emission probabilities for the two states and run Baum-Welch on about 10 million bases, using scaled forward and backward variables to avoid underflow. The log-likelihood rises quickly over the first 15 iterations and changes by less than 0.01 after about 40, so training stops there.

The learned island state emits C and G noticeably more often than the background state and has a high self-transition probability, which produces long contiguous regions. Viterbi decoding with the trained model then marks candidate islands, and most of them overlap known gene promoters when compared against a related species, confirming that the unsupervised model captured a real biological signal.