HMM — Hidden Markov Models
এই পাঠে যা শিখবেন
- HMM-এর তিন core component — initial, transition, emission
- Markov assumption ও সীমাবদ্ধতা
- HMM-এর তিন core problem — likelihood, decoding, learning
- hmmlearn দিয়ে hands-on — Bangla weather/sentiment sequence
- NLP-তে HMM ও তার আধুনিক বিকল্প
১ · Sequential data — কেন HMM?
বাস্তবে অনেক ডেটাই sequential — শব্দের sequence (sentence), সময়ের সাথে stock price, ECG signal, DNA sequence। প্রতিটি element-কে independently treat করলে — temporal pattern হারাই। HMM (Baum-Petrie, ১৯৬৬; Rabiner ১৯৮৯ tutorial) — sequence-এর underlying state Markov চেইন assume করে।
Hidden: state সরাসরি দেখা যায় না। যেমন — speech-এ phoneme observable নয়, audio signal observable। POS tagging-এ — শব্দ visible, tag (noun/verb) hidden।
Markov: next state শুধু current-এর উপর depends — past-এর full history-র উপর নয়।
১) States $S = \{s_1, \ldots, s_N\}$ — hidden states।
২) Observations $O = \{o_1, \ldots, o_M\}$ — possible visible outputs।
৩) Transition matrix $\mathbf{A}$ — $a_{ij} = P(s_j \mid s_i)$।
৪) Emission matrix $\mathbf{B}$ — $b_i(o) = P(o \mid s_i)$।
৫) Initial distribution $\boldsymbol{\pi}$ — $\pi_i = P(s_i \text{ at } t=1)$।
২ · Joint probability
State sequence $\mathbf{s} = (s_1, \ldots, s_T)$, observation sequence $\mathbf{o} = (o_1, \ldots, o_T)$। Joint:
$$P(\mathbf{s}, \mathbf{o}) = \pi_{s_1} \cdot b_{s_1}(o_1) \cdot \prod_{t=2}^T a_{s_{t-1} s_t} \cdot b_{s_t}(o_t)$$
প্রতিটি step — previous state থেকে transition + current state-এ emission। Bayesian Network notation-এ — state ও observation node-এর DAG (L36)।
৩ · তিন core problem
Rabiner-এর classic ১৯৮৯ paper — তিন প্রশ্নই HMM-এর সবকিছু:
-
Problem 1 — Likelihood (Evaluation):
Model $\lambda = (\mathbf{A}, \mathbf{B}, \boldsymbol{\pi})$ ও observation $\mathbf{o}$ given — $P(\mathbf{o} \mid \lambda)$ কত?
Solution: Forward algorithm (dynamic programming) — exponential থেকে polynomial। -
Problem 2 — Decoding:
$\mathbf{o}$ ও $\lambda$ given — সবচেয়ে likely state sequence $\mathbf{s}^*$?
Solution: Viterbi algorithm (L38)। Most-likely path find। -
Problem 3 — Learning:
$\mathbf{o}$ given — best $\lambda$ কী?
Solution: Baum-Welch algorithm (L38) — EM-এর বিশেষ রূপ।
৪ · Forward algorithm — likelihood
Naive: সব $N^T$ state sequence enumerate — exponential, infeasible।
Forward DP — $\alpha_t(i) = P(o_1, \ldots, o_t, s_t = i \mid \lambda)$:
$$\alpha_1(i) = \pi_i b_i(o_1)$$
$$\alpha_{t+1}(j) = \left[ \sum_i \alpha_t(i) \cdot a_{ij} \right] b_j(o_{t+1})$$
$$P(\mathbf{o} \mid \lambda) = \sum_i \alpha_T(i)$$
Complexity $O(N^2 T)$ — feasible।
৫ · উদাহরণ — Dhaka weather → wear
Hidden states: Weather $\in$ {Sunny, Rainy}।
Observations: যা মানুষ পরছে $\in$ {Umbrella, Sunglasses, Raincoat}।
Transition $\mathbf{A}$: Sunny→Sunny ০.৭, Sunny→Rainy ০.৩, Rainy→Sunny ০.৪, Rainy→Rainy ০.৬।
Emission $\mathbf{B}$: Sunny → Umbrella ০.১, Sunglasses ০.৭, Raincoat ০.২। Rainy → Umbrella ০.৬, Sunglasses ০.১, Raincoat ০.৩।
মানুষ পরপর Umbrella, Raincoat, Umbrella পরেছে — weather sequence কী ছিল? এটাই decoding problem।
৬ · hmmlearn — Gaussian HMM
Discrete observation নয়, continuous (e.g., audio MFCC, stock price)? — Gaussian emission।
# pip install hmmlearn
import numpy as np
from hmmlearn import hmm
# 2 hidden regime: bull / bear stock market
np.random.seed(0)
n_samples = 500
# bull regime: positive return mean 0.5%
# bear regime: negative return mean -0.5%
# alternating 50-step blocks
returns = []
for i in range(10):
if i % 2 == 0:
returns.append(np.random.normal(0.005, 0.01, 50))
else:
returns.append(np.random.normal(-0.005, 0.02, 50))
X = np.concatenate(returns).reshape(-1, 1)
model = hmm.GaussianHMM(n_components=2, covariance_type="full",
n_iter=100, random_state=0)
model.fit(X)
states = model.predict(X)
print(f"Inferred regime sequence (first 60): {states[:60]}")
print(f"\nRegime means: {model.means_.ravel().round(4)}")
print(f"Transition matrix:\n{model.transmat_.round(3)}")
print(f"Score (log-likelihood): {model.score(X):.2f}")
৭ · NLP-তে HMM — POS tagging
Bangla sentence — শব্দ visible, POS tag (বিশেষ্য, ক্রিয়া) hidden। HMM-এর textbook application।
from hmmlearn import hmm
# 3 POS tag, 5 word vocabulary (toy)
# states: 0=Noun, 1=Verb, 2=Adj
# words : 0=cat, 1=run, 2=red, 3=fast, 4=dog
# transition: noun→verb common, verb→noun common
A = np.array([[0.1, 0.6, 0.3], # Noun → ?
[0.7, 0.1, 0.2], # Verb → ?
[0.6, 0.2, 0.2]]) # Adj → ?
# emission: noun emits cat/dog, verb emits run, adj emits red/fast
B = np.array([[0.4, 0.05, 0.05, 0.1, 0.4], # Noun
[0.1, 0.7, 0.1, 0.05, 0.05], # Verb
[0.05, 0.05, 0.45, 0.4, 0.05]])# Adj
pi = np.array([0.6, 0.2, 0.2])
model = hmm.CategoricalHMM(n_components=3)
model.startprob_ = pi
model.transmat_ = A
model.emissionprob_ = B
# observe word sequence: cat run fast dog
obs = np.array([[0], [1], [3], [4]])
logprob, states = model.decode(obs, algorithm="viterbi")
tag_names = ["Noun", "Verb", "Adj"]
print("Sequence: cat run fast dog")
print("Tags: ", " ".join(tag_names[s] for s in states))
print(f"Log-probability: {logprob:.3f}")
৮ · HMM-এর সীমাবদ্ধতা
- Markov: long-distance dependency miss। "He, who studied hard last year, now ___" — gap বড় হলে।
- Independence: emission শুধু current state-এ depend — context-aware emission impossible।
- Discrete state: continuous latent (mixed)।
- Local optima: Baum-Welch local maximum।
- Modern alternatives: RNN, LSTM, Transformer — context-aware, better।
৯ · কখন HMM আজও relevant
- Small data: deep learning data-hungry — HMM ১০০ sample-এও কাজ।
- Interpretable: state, transition explicit।
- Online/streaming: real-time decode।
- Domain knowledge: known structure encode।
- Bioinformatics: CpG island, gene structure — HMM gold standard।
- Speech: classical recognizer; modern hybrid HMM-DNN।
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ Markov assumption কেন এত strong? Higher-order Markov কাজ করে কি?
Markov assumption — HMM-এর হৃদয়। কিন্তু এটা সরলীকরণ — বাস্তবে প্রায়ই violated।
Markov assumption:
- $P(s_t | s_{t-1}, s_{t-2}, \ldots) = P(s_t | s_{t-1})$।
- Future শুধু present-এ depend।
- "Memoryless"।
কেন strong:
- Real sequence-এ long-range dependency: "If yesterday আমি sick, then today still recovering"।
- Language: "The cats that I saw — verb plural"।
- Music: motif return after many bars।
- Stock: trend persistence beyond single-day।
Higher-order Markov:
- $P(s_t | s_{t-1}, s_{t-2})$ — 2nd order।
- $P(s_t | s_{t-1}, \ldots, s_{t-k})$ — $k$-th order।
- State space exponentially blows up — $N^k$।
- Practically rare beyond 2nd-order।
Trick — augmented state:
- $k$-th order HMM equivalent to 1st order with state = $(s_{t-k+1}, \ldots, s_t)$।
- Theory same; computation cost-এ shift।
Beyond Markov:
- RNN: hidden state encode arbitrary past — theoretically infinite-order।
- LSTM: selective memory — long-range dependency।
- Transformer: attention-এ যেকোনো past position-এ direct access।
- State Space Model (Mamba, ২০২৩): efficient long-context।
Practical considerations:
- Markov enough for short sequences।
- 2nd-order helps marginally।
- Long sequence + complex dependency → neural model।
Hidden semi-Markov:
- State duration explicit modeling।
- "Speech phoneme average ৫০ms" — classical HMM exponential, semi-Markov gamma।
Auto-regressive HMM:
- Emission depends on previous emission too।
- $P(o_t | s_t, o_{t-1})$।
- Time series modeling enhanced।
Bangladesh examples:
- Bangla sentence — long-range agreement (subject-verb)। HMM struggle। BERT excels।
- Climate modeling — monthly weather Markov OK; weekly fine-grain insufficient।
- Healthcare — symptom progression weeks-long, HMM 1st-order limited।
মূল উপলব্ধি: Markov simplifying — practical, computational। Long-range dependency need → modern neural। HMM understand করেই পরবর্তী step appreciate।
প্র ০২ HMM বনাম RNN/Transformer — performance gap কত? HMM-এর জায়গা আছে কি আজকের দিনে?
চমৎকার ranking question। এক যুগে state-of-art ছিল HMM, এখন neural network।
HMM advantages:
- Parameter efficient — small।
- Fast training — Baum-Welch in seconds for 1000-sample sequence।
- Inference dynamic programming — exact, polynomial।
- Probability output principled।
- State interpretable।
- Domain knowledge integrate (transition prior)।
- No GPU needed।
RNN/LSTM advantages:
- Long-range dependency।
- Continuous high-D embedding।
- Better with big data।
- Feature learn automatic।
- End-to-end task-specific।
Transformer advantages:
- Parallel training (vs RNN sequential)।
- Attention captures any-distance dependency।
- Pretrained model transfer learning।
- State-of-art everywhere।
Performance benchmark:
POS tagging (English):
- HMM — ~95% accuracy।
- BiLSTM-CRF — ~97%।
- BERT — ~98%।
- Gap small but consistent।
Speech recognition:
- HMM-GMM — ~80% WER (১৯৯০s)।
- HMM-DNN hybrid — ~70% (২০১২)।
- End-to-end neural (Transformer/Conformer) — ~5% (Whisper, ২০২২)।
- Massive jump।
Bioinformatics gene finding:
- HMM — still dominant (HMMER, Glimmer)।
- Neural approach challenging — interpretability + small data।
- HMM আজও gold standard।
Time series regime:
- HMM — financial classical।
- Neural — better predictive power।
- HMM interpret-able state, neural correlation।
HMM আজও জায়গা:
- Embedded systems: low compute, real-time।
- Bioinformatics: domain-specific tools।
- Education: sequence model intuition।
- Speech (legacy): Kaldi, telephony।
- Anomaly detection: regime detection — interpretable state।
- Small/specialized data: fine-tuned-model বানাতে আগে।
Hybrid HMM:
- HMM-DNN — emission via neural, transition Markov।
- Best of both for some tasks।
- Speech recognition transition era।
Bangladesh-specific:
- Bangla speech — limited data, HMM-DNN useful।
- Bangla POS — HMM baseline; BERT production।
- Stock market regime — HMM interpretable।
মূল উপলব্ধি: HMM "deprecated" সম্পূর্ণ ভুল — niche-এ champion। Big data + complex dependency = neural। Small/structured/interpretability = HMM। Tool selection — task-specific, not trend।
প্র ০৩ Number of states কীভাবে চয়ন? Underfitting vs overfitting trade-off।
HMM-এর K-Means-similar challenge — $N$ (state count) আগে দিতে হয়।
State count effect:
(১) Too few states:
- Underfitting — distinct regime merge।
- "All weather is sunny or rainy" — অথচ overcast separate।
- Likelihood low।
- Decoding poor।
(২) Too many states:
- Overfitting — random fluctuation state হিসেবে।
- Parameter count blows — $N^2 + NM$।
- Likelihood high but generalization poor।
- Local optima — Baum-Welch confuse।
- State interpretation muddled।
Selection methods:
(১) Cross-validation:
- Held-out log-likelihood।
- Best $N$ on validation set।
- Standard but slow।
(২) Information criteria:
- BIC (Bayesian Information Criterion):
- $\text{BIC} = -2 \log L + p \log n$
- Penalize parameters — automatically select।
- AIC — less penalty।
(৩) Domain knowledge:
- Speech: phoneme count fixed (~৪০ for English, ~৫০ for Bangla)।
- Finance: bull/bear/sideways = 3 regime।
- POS: tag set predefined।
(৪) Hierarchical:
- HDP-HMM (Hierarchical Dirichlet Process) — non-parametric।
- State count auto-determined।
- Bayesian — prior penalize complexity।
(৫) Sticky HMM:
- State persistence prior।
- Avoid spurious quick switches।
(৬) Initialization sensitivity:
- Multiple random init।
- K-Means init — emission similar to centroid।
- Best likelihood selection।
Practical workflow:
- Domain prior — start range।
- BIC across $N \in [\text{min}, \text{max}]$।
- Top-3 candidate validate manually।
- Final selection — interpretability priority।
Caveats:
- BIC strict — may underfit।
- Domain expert often disagree with BIC।
- State identifiability — different state-ordering same likelihood।
Bangladesh case:
- DSE regime: bull, bear, sideways = 3। BIC may suggest 2; domain says 3।
- Bangla speech phoneme: ~৫০। Linguistic-driven।
- Health symptom progression: domain expert input critical।
মূল উপলব্ধি: $N$ selection mix of statistical criterion + domain knowledge + interpretability। No single right answer। Robustness check via multiple $N$ + qualitative review।
প্র ০৪ DSE (Dhaka Stock Exchange)-এ HMM দিয়ে regime detection — production system কেমন হবে?
Real fintech application — Bangladesh equity research, asset management।
(১) Use cases:
- Bull/bear regime — portfolio adjustment।
- Volatility regime — risk model।
- Liquidity regime — execution strategy।
- Market sentiment — research insight।
(২) Data:
- DSE Broad Index (DSEX) daily return।
- Sector indices।
- Volume, volatility।
- Macro indicators (BB rate, inflation)।
- Time horizon: ১০ year+ history।
(৩) Model design:
- Hidden states: 2-3 regime (bull/bear, optional sideways)।
- Emission: Gaussian return + volatility।
- Multivariate — multiple sector simultaneously।
(৪) Training:
- Baum-Welch — ১০০ iteration।
- Multiple initialization — best likelihood।
- Train on ৭০% history, test on ৩০%।
(৫) Real-time inference:
- Daily update — new return arrive।
- Online forward algorithm।
- Posterior probability each regime।
- Threshold-based regime label।
(৬) Output dashboard:
- Current regime probability।
- Historical regime timeline।
- Transition probability (next regime)।
- Sector-wise regime ভিন্নতা।
(৭) Trading strategy integration:
- Bull → equity overweight।
- Bear → cash/bond overweight।
- Sideways → mean-reversion।
- Backtesting — risk-adjusted return।
(৮) Validation:
- Out-of-sample regime accuracy।
- Sharpe ratio — regime-aware vs naive।
- Drawdown reduction।
- Comparison: HMM vs threshold-based, vs ML classifier।
(৯) Challenges:
- Bangladesh-specific: low liquidity, limited data depth।
- Regime persistence: Bangladesh political event disrupt।
- Black swan: COVID, war — out-of-distribution।
- Data quality: missing day, holiday। DSE half-day session।
- Survivorship bias: delisted stock dropped।
(১০) Risk management:
- HMM probability — uncertainty quantify।
- Confidence interval position size।
- Regime change probability — early warning।
- VaR adjusted for regime।
(১১) Ensemble:
- HMM + threshold + ML classifier।
- Voting/averaging regime probability।
- Robust to single-model failure।
(১২) Compliance:
- BSEC regulation।
- Backtest documented।
- Model risk management framework।
- Audit trail।
(১৩) Continuous monitoring:
- Performance drift detection।
- Annual model retrain।
- State count revisit (regime structure change?)।
- Market microstructure shift adaptation।
(১৪) Beyond HMM:
- Transformer time-series forecasting (Informer, PatchTST)।
- Mixture of HMM-NN।
- Graph neural network for sector dependencies।
- HMM-as-baseline, neural-as-champion।
মূল উপলব্ধি: HMM-based regime detection Bangladesh asset management-এ practical, interpretable। Risk-adjusted return improvement via regime-aware allocation — proven globally, Bangladesh-এ underexplored opportunity। Engineering 80%, model 20%।
অনুশীলন
-
হিসাব করুন: 2-state HMM, π=(0.6, 0.4), A=[[0.7, 0.3], [0.4, 0.6]], emission B[s][o]=[[0.5, 0.5], [0.1, 0.9]] (state 0: balance, state 1: o=1 likely)। Observe o=(0, 1)। P(o|λ)?
- α₁(0) = 0.6 × 0.5 = 0.30।
- α₁(1) = 0.4 × 0.1 = 0.04।
- α₂(0) = (0.30×0.7 + 0.04×0.4) × 0.5 = 0.226 × 0.5 = 0.113।
- α₂(1) = (0.30×0.3 + 0.04×0.6) × 0.9 = 0.114 × 0.9 = 0.1026।
- P(o|λ) = 0.113 + 0.1026 = 0.2156।
-
NumPy-তে চেষ্টা: Forward algorithm scratch।
import numpy as np def forward(obs, A, B, pi): T, N = len(obs), len(pi) alpha = np.zeros((T, N)) alpha[0] = pi * B[:, obs[0]] for t in range(1, T): alpha[t] = (alpha[t-1] @ A) * B[:, obs[t]] return alpha, alpha[-1].sum() A = np.array([[0.7, 0.3], [0.4, 0.6]]) B = np.array([[0.5, 0.5], [0.1, 0.9]]) pi = np.array([0.6, 0.4]) obs = [0, 1] alpha, p = forward(obs, A, B, pi) print("alpha:\n", alpha.round(4)) print("P(o|λ):", round(p, 4)) -
ভাবুন: Bangla typing autocorrect-এ HMM কীভাবে কাজে দিতে পারে? Limitations?
- Hidden state: intended word।
- Observation: typed word (with errors)।
- Transition: language model — common bigram।
- Emission: typo probability — keyboard adjacency।
- Decoding: Viterbi most-likely intended sequence।
- Limit: word sequence Markov sufficient nয় — semantic context miss।
- Modern alt: Transformer (BERT-based corrector) — context-aware।
- Hybrid: HMM fast, neural fallback for complex case।
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ৩৮ · Viterbi ও Baum-Welch পরবর্তী পাঠ HMM-এর decode ও learning algorithm।
- পাঠ ৩৬ · Bayesian Networks আগের পাঠ HMM Bayesian network-এর temporal special case।
- পাঠ ৩৯ · EM Algorithm এই পাঠের সাথে সম্পর্কিত Baum-Welch = EM-এর HMM-specific instance।
- সব AI Courses ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps।