To learn the parameters ( A ) (state transition probabilities) and ( B ) (observation likelihoods, or emission probabilities) of a Hidden Markov Model (HMM) given an observation sequence ( O ) and the set of hidden states, we typically use the Baum-Welch algorithm, a special case of the Expectation-Maximization (EM) algorithm for HMMs. Here's an outline of the process:
Inputs
- An observation sequence ( O = (o_1, o_2, \ldots, o_T) ).
- A set of hidden states ( S = {s_1, s_2, \ldots, s_N} ).
Outputs
- Transition probability matrix ( A = [a_{ij}] ), where ( a_{ij} ) is the probability of transitioning from state ( s_i ) to state ( s_j ).
- Emission probability matrix ( B = [b_{jk}] ), where ( b_{jk} ) is the probability of observing ( o_k ) from state ( s_j ).
- Initial state distribution ( \pi ).
Here are the steps:
Initialization
- Initialize ( A ), ( B ), and ( \pi ) with random values or based on prior knowledge.
- Ensure that ( A ), ( B ), and ( \pi ) are valid probability distributions (rows sum to 1).
Expectation-Maximization Steps
E-Step
-
Forward Algorithm:
- Compute forward probabilities (\alpha_t(i)) for each state ( s_i ) and time ( t ) using:
[
\alpha_1(i) = \pi(i) \cdot b_i(o_1)
]
[
\alpha_t(i) = \sum_{j=1}^{N} \alpha_{t-1}(j) \cdot a_{ji} \cdot b_i(o_t)
]
-
Backward Algorithm:
- Compute backward probabilities (\beta_t(i)) for each state ( s_i ) and time ( t ) using:
[
\beta_T(i) = 1
]
[
\beta_t(i) = \sum_{j=1}^{N} a_{ij} \cdot b_j(o_{t+1}) \cdot \beta_{t+1}(j)
]
-
Compute (\xi_t(i, j)) and (\gamma_t(i)):
- (\xi_t(i, j)) is the probability of being in state ( s_i ) at time ( t ) and in state ( s_j ) at time ( t+1 ).
[
\xi_t(i, j) = \frac{\alpha_t(i) \cdot a_{ij} \cdot b_j(o_{t+1}) \cdot \beta_{t+1}(j)}{\sum_{i=1}^{N} \sum_{j=1}^{N} \alpha_t(i) \cdot a_{ij} \cdot b_j(o_{t+1}) \cdot \beta_{t+1}(j)}
]
- (\gamma_t(i)) is the probability of being in state ( s_i ) at time ( t ).
[
\gamma_t(i) = \frac{\alpha_t(i) \cdot \beta_t(i)}{\sum_{j=1}^{N} \alpha_t(j) \cdot \beta_t(j)}
]
M-Step
-
Re-estimate ( A ), ( B ), and ( \pi ):
-
Transition probabilities:
[
a_{ij} = \frac{\sum_{t=1}^{T-1} \xi_t(i, j)}{\sum_{t=1}^{T-1} \gamma_t(i)}
]
-
Emission probabilities:
[
b_i(k) = \frac{\sum_{t=1, o_t=k}^{T} \gamma_t(i)}{\sum_{t=1}^{T} \gamma_t(i)}
]
-
Initial state probabilities:
[
\pi(i) = \gamma_1(i)
]
Iterate
Repeat the E-step and M-step until convergence, which can be defined as a small change in the likelihood of the observation sequence given the model parameters.
The Baum-Welch algorithm will give you maximum likelihood estimates of the HMM parameters. Note that due to the nature of EM, the algorithm may converge to a local maximum, so it may be beneficial to try multiple initializations.