# 前向算法

前向算法是一种动态规划算法，用于在隐马尔可夫模型（HMM）下计算观测序列的似然度。它通过对所有隐藏状态路径的概率递归求和来实现，对于 \( N \) 个状态和 \( T \) 个观测，运算量为 \( O(N^{2} \cdot T) \)，而非枚举每条路径的指数级开销。<sup>[1](https://doi.org/10.1109/5.18626)</sup><sup> • </sup><sup>[2](https://web.stanford.edu/~jurafsky/slp3/old_aug24/A.pdf)</sup> 它解决了 HMM 的评估问题：在参数已知的模型下，观测序列出现的概率有多大。<sup>[3](https://www.inf.ed.ac.uk/teaching/courses/asr/2024-25/asr05-hmm-algorithms.pdf)</sup> 同一递归式配合反向传递，可提供用于训练和解码的状态后验概率，广泛应用于语音识别、计算生物学和一般序列建模。<sup>[3](https://www.inf.ed.ac.uk/teaching/courses/asr/2024-25/asr05-hmm-algorithms.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 输出量 | P(O\|λ)，即模型下完整观测序列的概率<sup>[1](https://doi.org/10.1109/5.18626)</sup> |
| 前向变量 | α_t(i) = P(O₁…O_t, q_t = S_i \| λ)，即截至时刻 t 的观测与时刻 \( t \) 状态为 \( S_i \) 的联合概率<sup>[1](https://doi.org/10.1109/5.18626)</sup> |
| 开销 | \( O(N^{2} \cdot T) \) 时间，而直接枚举约为 \( 2T \cdot N^{T} \)<sup>[1](https://doi.org/10.1109/5.18626)</sup> |
| 空间 | \( K \) 个状态、长度为 \( N \) 的序列需 \( O(K \cdot N) \)；若只保留当前列则为 \( O(2 \cdot \|S\|) \)<sup>[4](https://ocw.mit.edu/courses/6-047-computational-biology-fall-2015/aabc42b3391c9bbc2809c0926921c5c9_MIT6_047F15_Lecture04.pdf)</sup><sup> • </sup><sup>[5](https://www.cs.huji.ac.il/course/2008/cbio/Scribes/lect14.pdf)</sup> |
| 与维特比的关系 | 递归式完全相同，只是维特比取前驱状态的最大值而非求和<sup>[2](https://web.stanford.edu/~jurafsky/slp3/old_aug24/A.pdf)</sup> |
| 数值注意事项 | 即使双精度，\( \alpha \) 值在约 100 个时间步后也会下溢；需要缩放或对数空间计算<sup>[1](https://doi.org/10.1109/5.18626)</sup><sup> • </sup><sup>[6](https://courses.physics.illinois.edu/ece417/fa2020/slides/lec14.pdf)</sup> |

## 工作原理

前向变量 \( \alpha_t(i) \) 定义为：给定模型 λ，部分观测序列 O₁…O_t 与时刻 t 状态为 S_i 的联合概率。<sup>[1](https://doi.org/10.1109/5.18626)</sup> 该变量满足递归式

\[ \alpha_{t+1}(j) = \Big[ \sum_{i=1}^{N} \alpha_t(i) \, a_{ij} \Big] \, b_j(O_{t+1}), \]

其中 \( a_{ij} \) 是从状态 \( i \) 到状态 \( j \) 的转移概率，\( b_j(O_{t+1}) \) 是状态 j 下下一观测的发射概率。<sup>[1](https://doi.org/10.1109/5.18626)</sup><sup> • </sup><sup>[7](https://courses.grainger.illinois.edu/ece537/fa2022/slides/lec14.pdf)</sup> 基础情形使用初始状态分布 \( \pi \)，算法以似然度 P(O\|λ) = Σ_i α_T(i) 终止。<sup>[1](https://doi.org/10.1109/5.18626)</sup>

效率来自计算的结构。直接对所有状态序列求和观测概率，意味着对 \( N^{T} \) 条路径求和。[动态规划](https://www.edgepedia.org/zh/dynamic-programming)利用了一个事实：在每个时间步，所有这些路径在格架（trellis）上重新汇合为仅 \( N \) 个节点——量 \( \alpha_t(i) \) 概括了关于过去对将来有影响的一切，因此部分和只计算一次并重复使用。<sup>[1](https://doi.org/10.1109/5.18626)</sup> 朴素评估对于 \( n \) 个观测和 \( s \) 个状态的开销为 \( O(n \cdot s^{n}) \)；递归评估的开销为 \( O(n \cdot s^{2}) \)。<sup>[8](https://faculty.washington.edu/yenchic/24A_stat516/Lec9_HMM.pdf)</sup>

## 计算步骤

对于有 \( N \) 个状态的模型和长度为 \( T \) 的观测序列：<sup>[1](https://doi.org/10.1109/5.18626)</sup>

1. 对每个状态 \( i \)，初始化 \( \alpha_1(i) = \pi_i \cdot b_i(O_1) \)。
2. 对 \( t = 1, \ldots, T-1 \) 及每个状态 \( j \)，计算 \( \alpha_{t+1}(j) = \left[ \sum_i \alpha_t(i) \cdot a_{ij} \right] \cdot b_j(O_{t+1}) \)。
3. 以 P(O\|λ) = Σ_i α_T(i) 终止。

\( T-1 \) 个步骤中每一步计算 \( N \) 个变量，每个变量需要对 N 项求和，总共 \( O(T \cdot N^{2}) \) 次乘法。<sup>[7](https://courses.grainger.illinois.edu/ece537/fa2022/slides/lec14.pdf)</sup> 存储完整表格对于 \( K \) 个状态和序列长度 \( N \) 需要 \( O(K \cdot N) \) 空间，若还需要反向变量或后验概率则必须存储；若只需似然度，则只需保留最后一列，存储降至 \( O(2 \cdot \|S\|) \)。<sup>[4](https://ocw.mit.edu/courses/6-047-computational-biology-fall-2015/aabc42b3391c9bbc2809c0926921c5c9_MIT6_047F15_Lecture04.pdf)</sup><sup> • </sup><sup>[5](https://www.cs.huji.ac.il/course/2008/cbio/Scribes/lect14.pdf)</sup>

在浮点运算中，原始 \( \alpha \) 值随 t 指数式趋近于零；超过约 100 步后动态范围超出双精度，因此需要重新缩放计算（见变体）。<sup>[1](https://doi.org/10.1109/5.18626)</sup>

## 起源

前向递归由 伦纳德·E·鲍姆（Leonard E. Baum）和 J·A·伊根（J. A. Eagon） 在 1967 年发表于《美国数学会公报》的关于马尔可夫过程概率函数统计估计的论文中提出。<sup>[9](https://dm.cs.tu-dortmund.de/en/mlbits/sequential-models-hmm-algorithms/)</sup><sup> • </sup><sup>[10](https://doi.org/10.1090/s0002-9904-1967-11751-8)</sup>

相关机制的记载更为充分。A·维特比（A. Viterbi）1967 年关于卷积码解码的论文为递归式的最大值版本命名。<sup>[11](https://doi.org/10.1109/tit.1967.1054010)</sup> A·P·登普斯特（A. P. Dempster）、N·M·莱尔德（N. M. Laird）和 D·B·鲁宾（D. B. Rubin）（1977）的 EM 算法提供了 鲍姆-韦尔奇训练背后的通用框架。<sup>[12](https://doi.org/10.1111/j.2517-6161.1977.tb01600.x)</sup> L·R·拉比纳（L. R. Rabiner）1989 年发表于《IEEE 会刊》的教程标准化了三问题框架及上述缩放流程。<sup>[1](https://doi.org/10.1109/5.18626)</sup>

## 变体

**缩放前向。** 下溢的标准补救措施是在每个时刻 \( t \) 将 \( \alpha_t(i) \) 乘以与 i 无关的缩放系数，使值保持在机器动态范围内；这些系数在重估公式中精确抵消，且只需在必要时应用缩放。<sup>[1](https://doi.org/10.1109/5.18626)</sup> 一种常见的归一化在每步后将 α̂_t(j) 重新缩放使 Σ_j α̂_t(j) = 1；用每步因子 c_it，对数似然恢复为 log P(Y\|M) = −Σ_t log(c_it)。<sup>[6](https://courses.physics.illinois.edu/ece417/fa2020/slides/lec14.pdf)</sup><sup> • </sup><sup>[13](https://cran.r-project.org/web/packages/seqHMM/vignettes/seqHMM_algorithms.pdf)</sup>

**对数空间前向。** 另一种方法是将所有概率存为对数，用 log-sum-exp 运算求和，例如 LSE(−6.11, −9.47) = −6.08。这在数值上更稳定，但因反复调用 log-sum-exp 而较慢。<sup>[13](https://cran.r-project.org/web/packages/seqHMM/vignettes/seqHMM_algorithms.pdf)</sup><sup> • </sup><sup>[14](https://cs.rice.edu/~ogilvie/comp571/forward-algorithm/)</sup> [维特比算法](https://www.edgepedia.org/zh/viterbi-algorithm)更廉价地避免了这一困难，因为 log(max) = max(log)。<sup>[6](https://courses.physics.illinois.edu/ece417/fa2020/slides/lec14.pdf)</sup>

**前向-后向。** 在前向传递后运行反向递归 \( \beta_j(t) = p(x_{t+1}, \ldots, x_T \mid q_t = j, M) \)，得到状态后验 \( \gamma_j(t) = \alpha_j(t) \cdot \beta_j(t) / \alpha_E \)，即 Baum-Welch 重估和后验解码中使用的量。<sup>[3](https://www.inf.ed.ac.uk/teaching/courses/asr/2024-25/asr05-hmm-algorithms.pdf)</sup> 同样的动态规划方案适用于 HMM 之外，例如条件随机场，其中它产生配分函数 \( Z \) 和似然梯度所需的期望特征计数。<sup>[15](https://www.cs.columbia.edu/~mcollins/fb.pdf)</sup>

**束近似。** 每步只保留最可能的 \( B \) 个状态，将开销从 \( O(\|S\|^{2} \cdot T) \) 降至 \( O(B \cdot \|S\| \cdot T) \)；这一束搜索思想体现在 B·洛厄尔（B. Lowerre）和 R·雷迪（R. Reddy）于 1976 年描述的 Harpy 语音识别系统中。<sup>[9](https://dm.cs.tu-dortmund.de/en/mlbits/sequential-models-hmm-algorithms/)</sup><sup> • </sup><sup>[16](https://doi.org/10.1121/1.2003089)</sup>

## 应用

在语音识别中，三个经典 HMM 问题直接对应算法：前向算法计算已知模型下话语的似然度，维特比执行解码，前向-后向配合 EM（鲍姆-韦尔奇）执行训练。<sup>[3](https://www.inf.ed.ac.uk/teaching/courses/asr/2024-25/asr05-hmm-algorithms.pdf)</sup> 该算法很早就进入语音处理：J·贝克（J. Baker）1975 年关于卡内基梅隆大学（CMU）的 DRAGON 系统的论文是这些方法在识别中的早期应用。<sup>[17](https://doi.org/10.1109/tassp.1975.1162650)</sup> 前向-后向递归在现代语音系统的实际部署中仍用于计算连接时序分类（CTC）和无格 MMI 序列判别目标函数的梯度。<sup>[18](https://doi.org/10.48550/arxiv.2112.00709)</sup>

在计算生物学中，基于前向递归构建的 HMM 用于识别 GC 富集区、保守元件、编码外显子、基因结构和染色质状态。<sup>[4](https://ocw.mit.edu/courses/6-047-computational-biology-fall-2015/aabc42b3391c9bbc2809c0926921c5c9_MIT6_047F15_Lecture04.pdf)</sup> 在这两个领域之外，似然度计算支持一般序列数据的混合 HMM，如 萨图·赫尔斯基（Satu Helske）和约尼·赫尔斯基（Jouni Helske）为 R 实现的 seqHMM 包。<sup>[19](https://doi.org/10.18637/jss.v088.i03)</sup>

## 局限与替代方案

前向算法计算的似然度是否可靠，取决于模型假设是否成立：HMM 依赖一阶马尔可夫假设 \(P(q_i \mid q_1 \ldots q_{i-1}) = P(q_i \mid q_{i-1})\) 和输出独立假设 \(P(o_i \mid q_1 \ldots q_i, o_1 \ldots o_T) = P(o_i \mid q_i)\)。<sup>[2](https://web.stanford.edu/~jurafsky/slp3/old_aug24/A.pdf)</sup> \( O(N^{2} \cdot T) \) 开销随状态数二次增长，对于很长的序列或丰富的状态拓扑会变得昂贵。<sup>[1](https://doi.org/10.1109/5.18626)</sup>

最接近的替代方案维特比算法具有相同的 \( O(K^{2} \cdot N) \) 时间和 \( O(K \cdot N) \) 空间开销，但用最大值代替求和，返回单条最可能路径而非总似然度。<sup>[4](https://ocw.mit.edu/courses/6-047-computational-biology-fall-2015/aabc42b3391c9bbc2809c0926921c5c9_MIT6_047F15_Lecture04.pdf)</sup><sup> • </sup><sup>[2](https://web.stanford.edu/~jurafsky/slp3/old_aug24/A.pdf)</sup> 两者回答不同的问题：前向算法的边缘似然度也是后验解码成为可能的原因，后验解码最大化正确解释状态的期望数量而非选择一条路径。<sup>[20](https://compbio.fmph.uniba.sk/papers/08hmmchap.pdf)</sup> 对于训练，维特比训练是仅考虑最可能路径的近似，而鲍姆-韦尔奇使用所有路径上的前向-后向后验。<sup>[3](https://www.inf.ed.ac.uk/teaching/courses/asr/2024-25/asr05-hmm-algorithms.pdf)</sup>

## References

1. [L.R. Rabiner (1989). A tutorial on hidden Markov models and selected applications in speech recognition. Proceedings of the IEEE.](https://doi.org/10.1109/5.18626)
2. [Speech and Language Processing, 3rd ed., Appendix A: Hidden Markov Models (Jurafsky & Martin)](https://web.stanford.edu/~jurafsky/slp3/old_aug24/A.pdf)
3. [HMM Algorithms, Automatic Speech Recognition Lecture 5 (University of Edinburgh, 27 January 2025)](https://www.inf.ed.ac.uk/teaching/courses/asr/2024-25/asr05-hmm-algorithms.pdf)
4. [6.047 Computational Biology, Lecture 4: HMMs Part 1 (MIT OCW)](https://ocw.mit.edu/courses/6-047-computational-biology-fall-2015/aabc42b3391c9bbc2809c0926921c5c9_MIT6_047F15_Lecture04.pdf)
5. [Introduction to Computational Biology Lecture 14: Forward, Backward algorithms (Hebrew University)](https://www.cs.huji.ac.il/course/2008/cbio/Scribes/lect14.pdf)
6. [Lecture 14: Log Viterbi and Scaled Forward-Backward (ECE 417, UIUC)](https://courses.physics.illinois.edu/ece417/fa2020/slides/lec14.pdf)
7. [Lecture 14: A tutorial on hidden Markov models and selected applications in speech recognition, part 1 (UIUC ECE 537)](https://courses.grainger.illinois.edu/ece537/fa2022/slides/lec14.pdf)
8. [Lecture 9: Hidden Markov Model (UW STAT 516)](https://faculty.washington.edu/yenchic/24A_stat516/Lec9_HMM.pdf)
9. [Algorithms for HMM – Lecture Notes (TU Dortmund)](https://dm.cs.tu-dortmund.de/en/mlbits/sequential-models-hmm-algorithms/)
10. [Leonard E. Baum, J. A. Eagon (1967). An inequality with applications to statistical estimation for probabilistic functions of Markov processes and to a model for ecology. Bulletin of the American Mathematical Society.](https://doi.org/10.1090/s0002-9904-1967-11751-8)
11. [A. Viterbi (1967). Error bounds for convolutional codes and an asymptotically optimum decoding algorithm. IEEE Transactions on Information Theory.](https://doi.org/10.1109/tit.1967.1054010)
12. [A. P. Dempster, N. M. Laird, D. B. Rubin (1977). Maximum Likelihood from Incomplete Data Via the EM Algorithm. Journal of the Royal Statistical Society Series B (Statistical Methodology).](https://doi.org/10.1111/j.2517-6161.1977.tb01600.x)
13. [The main algorithms used in the seqHMM package (Helske, CRAN vignette)](https://cran.r-project.org/web/packages/seqHMM/vignettes/seqHMM_algorithms.pdf)
14. [Forward algorithm (Rice COMP 571 course notes)](https://cs.rice.edu/~ogilvie/comp571/forward-algorithm/)
15. [The Forward-Backward Algorithm (Michael Collins, Columbia University lecture notes)](https://www.cs.columbia.edu/~mcollins/fb.pdf)
16. [B. Lowerre, R. Reddy (1976). The Harpy Speech Recognition System: performance with large vocabularies. The Journal of the Acoustical Society of America.](https://doi.org/10.1121/1.2003089)
17. [J. Baker (1975). The DRAGON system--An overview. IEEE Transactions on Acoustics Speech and Signal Processing.](https://doi.org/10.1109/tassp.1975.1162650)
18. [Ondel, Lucas and colleagues (2021). GPU-Accelerated Forward-Backward algorithm with Application to Lattice-Free MMI. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2112.00709)
19. [Satu Helske, Jouni Helske (2019). Mixture Hidden Markov Models for Sequence Data: The seqHMM Package in R. Journal of Statistical Software.](https://doi.org/10.18637/jss.v088.i03)
20. [Advances in Hidden Markov Models for Sequence Annotation](https://compbio.fmph.uniba.sk/papers/08hmmchap.pdf)

---
*Initially written Sep 29, 2026 · Reviewed: Sep 30, 2026 · Edited: Oct 15, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Optimization and dynamic programming › Dynamic programming and sequential optimization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/forward-algorithm

English original: https://www.edgepedia.org/forward-algorithm
