1. Introduction
A Hidden Markov Model describes the joint probability of a collection of 'hidden' and 'observed' random variables. It relies on the assumption that the distribution of hidden variables depends only on the immediate predecessors, that the realization of each variable is independent of previous hidden variables if the value of the hidden variable in the previous timestep is known (this is often called firstorder Markov property). Furthermore the current observation variables is assumed to depend only on the current hidden state (this assumption is actually a little optimistic, see the pages Acoustic Model and Discrimiative Training for further information).
The Baum–Welch algorithm uses the EM algorithm to find the maximum likelihood estimate of the parameters of a hidden Markov model given a set of observations.
More mathematically, training a Hidden Markov Model is basically a parameter estimation task, we want to find the parameters characterizing the joint distribution (, initial state distibution , state transition matrix and observation parameters ) based on observations .
In a maximum likelihood approach we set the values to the value maximizing the probability of the evidence (i.e. the observed data).
If labeled training data is available the parameters could (theoretically) be determined from the data likelihood obtained through marginalization over the hidden variables. In practice this is infeasible for non trivial cases because the joint distribution does not factorize over time indices and the computational effort therefore grows exponentially with the length of the observation sequence. (see (Bishop 2006, sec. 13.2.1) for an extensive discussion of that matter).
In this article we present the Baum Welch Algorithm, a special case of the general expectation maximization (EM) algorithm, which gives us an iterative procedure to infer the parameters in realistic scenarios.
Our presentation is to great extent based on (Bishop 2006), additional references can be found in section Further Reading.
2. Baum Welch Algorithm
2.1 Preliminiaries
We assume a discrete state Hidden Markov Model with a known number of states . The hidden states of the latent variables can be represented using a of coding scheme such that for each realization we are given a vector with exactly one element equal to one and the remaining equal to zero.
We represent the transition probabilities in matrix form as
and obtain
Similarily the emission probalities can be written as
where bundles the parameters for the th component (For dimensional observations and Gaussian emissions that would be , for a multinoulli model (see also (Bishop 2006, Appendix B)) simply a vector ). Intuitively we can interpret as a gating variable that selects the distribution to use (see also the article on Gaussian Mixture Models).
The joint probability distribution over both latent and observed variables is then
(We bundle observation and hidden variables in matrices and let denote the collection of parameters , where gives the probability distribution over the intial state)
2.2 Expectation Maximization
Recall that the EM Algorithm iterates maximization of a lower bound on the complete data log likelihood and maximum likelihood estimation of the parameters. More formally the corresponding objective function is
Introducing additional notation
we have
and
(The expectation of a binary variable is just the probability that it takes the value )
such that
2.2.1 EStep
Now the EStep is actually almost completely given by the ForwardBackward Algorithm, we have
where
and
2.2.2 MStep
In the M step we maximize with respect to the parameters , treating and as constant
If the parameters of the emission densities are independent the problem decouples and we can find the ML estimates as follows
2.2.2.1 Discrete Emissions
For a multinoulli emission model each observation corresponds to a binary vector of length and we have the conditional observation distribution
and the corresponding estimate is
2.2.2.2 Continuous Emissions
A simple but practical relevant case of a continuous observation density is a Gaussian distribution
for which we obtain
and
2.3 Example
For illustration let's consider a two state () HMM with Gaussian emissions .
We take true parameters
A Matlab implementation based on code from Sebastien Paris (available on the Mathworks FileExchange) converges to estimated parameter values
(Parameters initialized at random, 100 training samples, 30 Iterations of the algorithm)
The resulting clustering can be visualized as follows
3. Discussion
As EM methods are notorious for getting stuck in local optima (the underlying problem is nonconvex) we want to reference alternative approaches and mention some caveats
 Extensions Staying with the EM approach deterministic annealing (see i.e Rao 2001) can mitigate the danger of local minima. Also it is advisable to spend some effort on initialisation
 Use fully labeled data for the initial settings of the parameters
 Initially ignore the time dependencies but treat the observations as IID random variables and apply standard methods (e.g. KMeans)
 Initialize at random but run the algorithm multiple times and pick the best solution.
 A comparative analysis of appropriate stopping (convergence criteria) can be found in (Abbi 2008)

Bayesian Approaches: EM returns a maximum a posteriori (likelihood) estimate. Alternatives (more Bayesian approaches) are

Markov Chain Monte Carlo (MCMC): Generate hidden paths using forwardsfiltering and backwards sampling and obtain the parameters from their posteriors conditioned on the simulated data (see (FruhwirtSchnatter 2007) for details)

Variational Bayes EM (Beal 2003): Use the posterior mean parameters instead of the MAP estimates in the E step and update the parameters of the conjugate posteriors in the M step

 Alternative gradient based methods allowing online learning are described in (Baldi 1994)
4. Further Reading
(Abbi 2008) Revlin Abbi, Analysis of stopping criteria for the EM algorithm in the context of patient grouping according to length of stay, 4th International IEEE Conference on Intelligent Systems, 2008.
(Baldi 1994) Patrick Balid, Smooth online learning algorithms for hidden Markov models, Journal on Neural Computation, Issue 6, 1994
(Barber 2014): David Barber, Bayesian reasoning and machine learning, Cambridge University Press, 2014.
(Beal 2003) Matthew Beal, Variational Algorithms for Approximate Bayesian Inference, Ph.D. Thesis, Gatsby University, 2003
(Bishop 2006): Christopher Bishop, Pattern recognition and machine learning, Springer, 2006.
(Fruhwirt 2007): Sabine FruhwirtSchnatter, Finite Mixture and Markov Switching Models, Springer 2007.
(Murphy 2012): Kevin Murphy, Machine Learning  A probabilistic perspective, MIT Press, 2012.