---
id: wiki-2026-0508-hmm
title: Hidden Markov Model (HMM)
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [HMM, hidden markov model, Viterbi, forward-backward, Baum-Welch]
duplicate_of: none
source_trust_level: A
confidence_score: 0.95
verification_status: applied
tags: [machine-learning, hmm, sequence, viterbi, baum-welch, speech]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: Python
  framework: hmmlearn / pomegranate
---

# Hidden Markov Model (HMM)

## 매 한 줄
> **"매 hidden state + observable emission 의 의 sequence model"**. 매 transition + emission probability. 매 Viterbi (MAP), forward-backward (filter), Baum-Welch (EM training). 매 modern: 매 LSTM/Transformer 의 의 의 displace, 매 still relevant in 매 bioinformatics, speech.

## 매 핵심

### 매 component
- **States**: hidden.
- **Observations**: emitted from state.
- **Transition matrix** A: state → state.
- **Emission matrix** B: state → obs.
- **Initial distribution** π.

### 매 task
- **Evaluation**: P(O|λ) — forward.
- **Decoding**: best state sequence — Viterbi.
- **Learning**: λ from O — Baum-Welch (EM).

### 매 응용
1. **Speech recognition** (legacy, pre-DL).
2. **POS tagging**.
3. **Bioinformatics** (gene, protein domains).
4. **Finance** (regime detection).
5. **Activity recognition**.

## 💻 패턴

### hmmlearn
```python
from hmmlearn import hmm
import numpy as np

# 매 Gaussian emissions
model = hmm.GaussianHMM(n_components=3, covariance_type='full', n_iter=100)
model.fit(X_observations)

states = model.predict(X_test)  # 매 Viterbi
log_prob = model.score(X_test)  # 매 forward
```

### Viterbi (manual)
```python
def viterbi(obs, A, B, pi):
    """매 most likely state sequence."""
    n_states = len(pi)
    T = len(obs)
    delta = np.zeros((T, n_states))
    psi = np.zeros((T, n_states), dtype=int)
    
    delta[0] = pi * B[:, obs[0]]
    for t in range(1, T):
        for j in range(n_states):
            trans = delta[t-1] * A[:, j]
            psi[t, j] = trans.argmax()
            delta[t, j] = trans.max() * B[j, obs[t]]
    
    states = [delta[-1].argmax()]
    for t in range(T-1, 0, -1):
        states.insert(0, psi[t, states[0]])
    return states
```

### Forward (P(O|λ))
```python
def forward(obs, A, B, pi):
    n_states = len(pi)
    T = len(obs)
    alpha = np.zeros((T, n_states))
    alpha[0] = pi * B[:, obs[0]]
    for t in range(1, T):
        for j in range(n_states):
            alpha[t, j] = sum(alpha[t-1, i] * A[i, j] for i in range(n_states)) * B[j, obs[t]]
    return alpha[-1].sum()
```

### Backward
```python
def backward(obs, A, B):
    n_states = A.shape[0]
    T = len(obs)
    beta = np.ones((T, n_states))
    for t in range(T-2, -1, -1):
        for i in range(n_states):
            beta[t, i] = sum(A[i, j] * B[j, obs[t+1]] * beta[t+1, j] for j in range(n_states))
    return beta
```

### Baum-Welch (EM)
```python
def baum_welch(obs, n_states, max_iter=100):
    n_obs = len(obs)
    pi = np.random.dirichlet(np.ones(n_states))
    A = np.random.dirichlet(np.ones(n_states), size=n_states)
    n_symbols = max(obs) + 1
    B = np.random.dirichlet(np.ones(n_symbols), size=n_states)
    
    for _ in range(max_iter):
        alpha = compute_alpha(obs, A, B, pi)
        beta = compute_beta(obs, A, B)
        gamma = (alpha * beta) / (alpha * beta).sum(axis=1, keepdims=True)
        xi = compute_xi(obs, A, B, alpha, beta)
        
        # 매 M-step
        pi = gamma[0]
        A = xi.sum(axis=0) / gamma[:-1].sum(axis=0, keepdims=True).T
        for k in range(n_symbols):
            B[:, k] = gamma[obs == k].sum(axis=0) / gamma.sum(axis=0)
    
    return pi, A, B
```

### POS tagging (toy)
```python
states = ['noun', 'verb', 'adj']
words = ['cat', 'eats', 'red', 'apple']
# 매 P(state | word) via HMM
```

### Gaussian Mixture HMM (continuous)
```python
model = hmm.GMMHMM(n_components=4, n_mix=3, covariance_type='full')
model.fit(X)
```

### Regime detection (finance)
```python
returns = stock.pct_change().dropna()
model = hmm.GaussianHMM(n_components=2).fit(returns.values.reshape(-1, 1))
regimes = model.predict(returns.values.reshape(-1, 1))
# 매 0 = bull, 1 = bear (or vice versa — interpret)
```

## 매 결정 기준
| 상황 | Use |
|---|---|
| Sequence + small data | HMM |
| Speech (modern) | DL |
| Bioinformatics | Profile HMM |
| Regime detection | Gaussian HMM |
| Long sequence | RNN / Transformer |

**기본값**: 매 small data sequence = HMM. 매 large data = DL. 매 bioinformatics 의 still HMM 의 standard.

## 🔗 Graph
- 부모: [[Probabilistic-Graphical-Models]]
- 응용: [[Bioinformatics]]
- Adjacent: [[Markov-Chain]] · [[Kalman-Filter-and-State-Tracking|Kalman-Filter]]

## 🤖 LLM 활용
**언제**: 매 small-data sequence. 매 explainable.
**언제 X**: 매 modern ML 매 DL win.

## ❌ 안티패턴
- **HMM for image**: 매 wrong domain.
- **No prior**: 매 EM stuck.
- **Too many states**: 매 overfit.

## 🧪 검증 / 중복
- Verified (Rabiner 1989, hmmlearn docs).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Viterbi / forward-backward / Baum-Welch / hmmlearn code |
