# 精确推断

精确推断是一类用于概率图模型的算法，它通过对因子化联合分布进行符号或数值操作，而非近似，来精确计算后验概率与边缘概率。主要的精确方法包括变量消元、树上的和-积消息传递，以及适用于一般图的[联结树算法](https://www.edgepedia.org/zh/junction-tree-algorithm)。其代价不由变量数目决定，而由图的树宽决定：低树宽模型可以高效处理，而仅凭变量数目并不构成限制因素。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1111/anzs.12257)</sup> 在树上，精确边缘概率可通过递归消息传递在节点数的线性时间内求得；<sup>[2](https://pachecoj.com/courses/csc665-1/papers/WainwrightJordan_TR2008.pdf)</sup> 在一般图上，代价随树宽呈指数增长。<sup>[3](https://arxiv.org/pdf/1206.3240)</sup>

| 关键事实 | 细节 |
|---|---|
| 计算内容 | 从因子化联合分布中精确计算后验边缘概率与条件分布<sup>[4](https://arxiv.org/html/1201.4724)</sup> |
| 树的情形 | 通过两轮消息传递计算所有节点的精确边缘概率，对于 n 个节点、每个节点 k 个状态，复杂度为 \( O(n \cdot k^{2}) \)<sup>[5](https://www.cs.toronto.edu/~duvenaud/courses/csc412/lectures/05-exactInference.pdf)</sup> |
| 变量消元 | 对于 p 个被消元变量，时间复杂度为 \( O(p \cdot K^{w+1}) \)，加上输入因子的代价，其中 w 是消元顺序下的诱导宽度<sup>[6](https://staff.fnwi.uva.nl/j.m.mooij/edu/ML2/Murphy_ch20.pdf)</sup> |
| 联结树算法 | 对于离散团，时间与空间复杂度为 \( O(|C| \cdot K^{w+1}) \)；对于高斯模型，时间复杂度为 \( O(|C| \cdot w^{3}) \)<sup>[6](https://staff.fnwi.uva.nl/j.m.mooij/edu/ML2/Murphy_ch20.pdf)</sup> |
| 难度 | 在离散模型中计算边缘概率是 #P完全的，即使是乘性近似也是 NP难的<sup>[7](https://www.cs.cmu.edu/~pradeepr/courses/708/2020-fall/resources/Exact-Inference-Variable-Elimination.pdf)</sup><sup> • </sup><sup>[3](https://arxiv.org/pdf/1206.3240)</sup> |
| 爆炸示例 | 一个 \( m \times n \) 格子的树宽为 \( O(\min\{m, n\}) \)；在 100 × 100 的伊辛模型上进行变量消元需要 \( O(2^{100}) \) 时间<sup>[6](https://staff.fnwi.uva.nl/j.m.mooij/edu/ML2/Murphy_ch20.pdf)</sup> |

## 工作原理

图模型将联合分布表示为若干因子的乘积，每个因子仅依赖于一小组变量。计算边缘概率需要对联合分布在所有其他变量上求和；朴素地做，代价随变量数目呈指数增长。分配律是出路：当一个变量不出现在某个因子中时，对该变量的求和可以移到乘积内部，于是公共子表达式只需计算一次并可复用。<sup>[8](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch9.S5.html)</sup> 在链式网络上，将求和推入乘积内部可将边缘推断从 \( O(k^{n}) \) 降至 \( O(n \cdot k^{2}) \)。<sup>[9](https://ermongroup.github.io/cs228-notes/inference/ve/)</sup> 消息传递算法所交换的消息正是这些共享的中间项，因此和-积方法与联结树方法是建立在这一运算之上的动态规划算法。<sup>[2](https://pachecoj.com/courses/csc665-1/papers/WainwrightJordan_TR2008.pdf)</sup>

树宽是控制量。对于三角化图，树宽等于最大团的大小减一；对于一般图，它是所有三角化中最小的树宽。<sup>[3](https://arxiv.org/pdf/1206.3240)</sup> 等价地，对于某个消元顺序，它是在消元过程中创建的任一因子中变量数目的最大值，而一个网络的树宽是所有消元顺序下的最小值。<sup>[8](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch9.S5.html)</sup> 树的树宽为 1，允许线性时间推断；一个 \( n \times n \) 网格图的树宽为 \( n \)。<sup>[7](https://www.cs.cmu.edu/~pradeepr/courses/708/2020-fall/resources/Exact-Inference-Variable-Elimination.pdf)</sup><sup> • </sup><sup>[3](https://arxiv.org/pdf/1206.3240)</sup> 没有结构假设时，精确推断是 NP难的，甚至近似也是 NP难的。<sup>[3](https://arxiv.org/pdf/1206.3240)</sup>

## 实现方法

**变量消元**每次消去一个隐变量：将所有涉及该变量的因子相乘，然后对其求和消去；因子是未归一化的概率。<sup>[10](https://inst.eecs.berkeley.edu/~cs188/textbook/bayes-nets/elimination.html)</sup> 从 m 个初始因子中消去 p 个变量的总工作量为 \( O((m + p) \cdot N_{\max}) \)，其中 \( N_{\max} \) 是中间因子中条目的最大数目。<sup>[7](https://www.cs.cmu.edu/~pradeepr/courses/708/2020-fall/resources/Exact-Inference-Variable-Elimination.pdf)</sup> 消元顺序影响巨大，而寻找最佳顺序是 NP难的；实用的启发式方法包括最小邻居、最小权重和最小填充。<sup>[9](https://ermongroup.github.io/cs228-notes/inference/ve/)</sup> 条件概率 P(Y \| E = e) 可通过在 P(Y, E = e) 和 P(E = e) 上分别运行消元，然后相除得到。<sup>[9](https://ermongroup.github.io/cs228-notes/inference/ve/)</sup>

**和-积消息传递**是将变量消元应用于因子树，也称为信念传播。<sup>[11](https://opencourse.inf.ed.ac.uk/sites/default/files/https/opencourse.inf.ed.ac.uk/pmr/2025/slides06.pdf)</sup> 一个节点只有在收到来自所有其他邻居的消息后，才向某个邻居发送消息；两轮传递，一轮朝向根，一轮返回，即可计算所有边缘概率，每个节点两条消息。<sup>[12](http://www.cs.columbia.edu/~blei/fogm/2014F/lectures/inference-on-trees.pdf)</sup><sup> • </sup><sup>[5](https://www.cs.toronto.edu/~duvenaud/courses/csc412/lectures/05-exactInference.pdf)</sup> 在具有 d 个变量、每个变量至多 K 个取值、每个因子至多 M 个变量的因子树上，代价为 \( O(d \cdot K^{M}) \)，与变量数目呈线性关系。<sup>[11](https://opencourse.inf.ed.ac.uk/sites/default/files/https/opencourse.inf.ed.ac.uk/pmr/2025/slides06.pdf)</sup> 该算法仅在树上精确；在有环图上不精确，且可能发散和振荡。<sup>[13](https://www.cs.ubc.ca/~murphyk/Teaching/CS532c_Fall04/Papers/freyJojicTutorial.pdf)</sup>

**联结树算法**将精确消息传递推广到任意图。对于有向模型，图被道德化并在需要时三角化；联结树是变量子集的树，其中相交的团在它们之间的唯一路径上共享整个交集，即运行交集性质。<sup>[14](https://www.stats.ox.ac.uk/~evans/gms/_book/jt.html)</sup> 消息从叶子传向选定的根（收集阶段），再返回叶子（分发阶段），之后所有势函数一致并给出正确的边缘概率。<sup>[14](https://www.stats.ox.ac.uk/~evans/gms/_book/jt.html)</sup> 寻找具有最小团的最优三角化，或最小化最大簇的联结树，是 NP难的。<sup>[14](https://www.stats.ox.ac.uk/~evans/gms/_book/jt.html)</sup><sup> • </sup><sup>[4](https://arxiv.org/html/1201.4724)</sup>

## 起源

针对多连通图的精确推断的团树形式由 S. L. 劳里岑（S. L. Lauritzen）和 D. J. 施皮格哈尔特（D. J. Spiegelhalter）在《皇家统计学会杂志B辑》1988 年的论文“图结构上概率的局部计算及其在专家系统中的应用”中报道。<sup>[15](https://doi.org/10.1111/j.2517-6161.1988.tb01721.x)</sup> 团树消息传递的和-积形式由普拉卡什·P. 谢诺伊（Prakash P. Shenoy）和格伦·谢弗（Glenn Shafer）在 1990 年的“概率与信度函数传播的公理”中报道。<sup>[16](https://doi.org/10.1016/b978-0-444-88650-7.50019-6)</sup> R. 德克特（R. Dechter）于 1996 年在《ACM计算综述》上报道了桶消元。<sup>[17](https://doi.org/10.1145/242224.242302)</sup> [因子图](https://www.edgepedia.org/zh/factor-graphs)与作为单一消息传递规则的和-积算法由 F. R. 克希尚（F. R. Kschischang）、B. J. 弗雷（B. J. Frey）和 H.-A. 勒利格（H.-A. Loeliger）于 2001 年在《IEEE信息论汇刊》上提出。<sup>[18](https://doi.org/10.1109/18.910572)</sup> 通用精确推断算法的发现被认为推动了概率人工智能的迅速发展。<sup>[19](https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume10/jaakkola99a-html/node1.html)</sup>

## 变体

将求和替换为取最大值、将乘积替换为对数因子之和，即得到最大-积，或对数域中的最大-和消息传递，利用恒等式 \( \max(\log a + \log b, \log a + \log c) = \log a + \max(\log b, \log c) \)；一次反向传递可恢复取得最大值的变量取值，从而给出 MAP 估计。<sup>[11](https://opencourse.inf.ed.ac.uk/sites/default/files/https/opencourse.inf.ed.ac.uk/pmr/2025/slides06.pdf)</sup> 在隐[马尔可夫模型](https://www.edgepedia.org/zh/markov-model)文献中，相应的算法是前向-后向算法和维特比算法：前向-后向是和-积的特例，而维特比是最大-积（或对数域中的最大-和）的特例。<sup>[12](http://www.cs.columbia.edu/~blei/fogm/2014F/lectures/inference-on-trees.pdf)</sup> 惰性传播是一种基于惰性求值的联结树推断算法，由安德斯·L. 马德森（Anders L. Madsen）和芬恩·V. 延森（Finn V. Jensen）于 1999 年在《人工智能》上报道。<sup>[20](https://doi.org/10.1016/s0004-3702%2899%2900062-4)</sup> 许多现代精确算法则改为将网络符号化地编译为概率电路，从中可直接得到证据的概率，且所有后验边缘概率可通过两轮传递得到。<sup>[8](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch9.S5.html)</sup>

## 应用

联结树算法涵盖了经典的递归算法，包括计算遗传学中的剪枝与剥离算法、隐马尔可夫模型的前向-后向算法，以及状态空间模型的卡尔曼滤波-平滑算法。<sup>[2](https://pachecoj.com/courses/csc665-1/papers/WainwrightJordan_TR2008.pdf)</sup> 在编码理论中，Turbo 码的迭代和-积译码在误码率 \( 10^{-5} \) 时，其 \( E_{b}/N_{0} \) 与香农极限的差距可逼近至一 dB 以内，<sup>[21](https://people.ee.ethz.ch/~loeliger/localpapers/FG_Allerton1997.pdf)</sup> 而对于极长码，即使底层因子图有环，这种译码在高斯信道上的性能与香农极限的差距也能缩小到一分贝的很小一部分。<sup>[18](https://doi.org/10.1109/18.910572)</sup>

## 局限与替代方案

主要的失效模式是树宽上的指数爆炸。对于 \( m \times n \) 的二维格子，树宽为 \( O(\min\{m, n\}) \)，因此在 100 × 100 的[伊辛模型](https://www.edgepedia.org/zh/ising-model)上进行变量消元需要 \( O(2^{100}) \) 时间。<sup>[6](https://staff.fnwi.uva.nl/j.m.mooij/edu/ML2/Murphy_ch20.pdf)</sup> 在数值上，许多小于 1 的概率相乘会导致下溢，因此最大值计算在对数空间中进行（最大-和），消息被重新归一化使其和为 1，对数空间计算可防止下溢和上溢。<sup>[12](http://www.cs.columbia.edu/~blei/fogm/2014F/lectures/inference-on-trees.pdf)</sup><sup> • </sup><sup>[5](https://www.cs.toronto.edu/~duvenaud/courses/csc412/lectures/05-exactInference.pdf)</sup>

当树宽过大时，替代方案是与消元原理相关的近似方法，如环形信念传播和变分方法，它们可以很准确，同时比蒙特卡洛方法耗时少得多。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1111/anzs.12257)</sup> 在非树的联结图上，消息传递仍可行，但收敛性无法保证；这就是环形信念传播。<sup>[14](https://www.stats.ox.ac.uk/~evans/gms/_book/jt.html)</sup> 对于非树因子图，精确选项是变量消元或联结树分组，环形传播则是不精确的替代方案。<sup>[11](https://opencourse.inf.ed.ac.uk/sites/default/files/https/opencourse.inf.ed.ac.uk/pmr/2025/slides06.pdf)</sup>

近期工作直接针对代价问题。快速弧反转（Fast arc-reversal）融合了弧反转与变量消元的特性，用于离散贝叶斯网络，在真实基准网络上显示出相比弧反转在平均运行时间和方差上的显著改进。<sup>[22](https://proceedings.mlr.press/v246/butz24a.html)</sup> Faster-BNI 由蒋建通（Jiantong Jiang）及其同事于 2024 年在《IEEE并行与分布式系统汇刊》上报道，利用多核 CPU 上的粗粒度和细粒度混合并行来加速联结树算法。<sup>[23](https://doi.org/10.1109/tpds.2024.3414177)</sup> WeightME 由亚龙·马内（Jaron Maene）、文森特·德金德伦（Vincent Derkinderen）和卢克·德·拉特（Luc De Raedt）于 2024 年提出，是一种基于加权模型采样的无偏的、概率近似正确的梯度估计器，它通过对 SAT 求解器的对数次调用来以概率保证近似梯度；同一论文还证明了计算加权模型计数的精确梯度是 #P完全的。<sup>[24](https://doi.org/10.48550/arxiv.2406.04472)</sup>

## References

1. [Exact or approximate inference in graphical models: why the choice is dictated by the treewidth, and how variable elimination can be exploited (ANZJS)](https://onlinelibrary.wiley.com/doi/10.1111/anzs.12257)
2. [Graphical Models, Exponential Families, and Variational Inference (Wainwright & Jordan, 2008)](https://pachecoj.com/courses/csc665-1/papers/WainwrightJordan_TR2008.pdf)
3. [Complexity of Inference in Graphical Models](https://arxiv.org/pdf/1206.3240)
4. [Tutorial on Exact Belief Propagation in Bayesian Networks: from Messages to Algorithms](https://arxiv.org/html/1201.4724)
5. [Exact inference lecture (University of Toronto, CSC412)](https://www.cs.toronto.edu/~duvenaud/courses/csc412/lectures/05-exactInference.pdf)
6. [Machine Learning: a Probabilistic Perspective, Chapter 20 (Exact inference), Murphy](https://staff.fnwi.uva.nl/j.m.mooij/edu/ML2/Murphy_ch20.pdf)
7. [Exact Inference: Variable Elimination (CMU 10-708 lecture notes)](https://www.cs.cmu.edu/~pradeepr/courses/708/2020-fall/resources/Exact-Inference-Variable-Elimination.pdf)
8. [Artificial Intelligence: Foundations of Computational Agents, 3rd ed., §9.5 Exact Probabilistic Inference (Poole & Mackworth)](https://www.cs.ubc.ca/~poole/aibook/3e/html/ArtInt3e.Ch9.S5.html)
9. [Variable Elimination (CS228 notes, Stanford Ermon group)](https://ermongroup.github.io/cs228-notes/inference/ve/)
10. [6.6 Exact Inference in Bayes Nets (UC Berkeley CS188 textbook)](https://inst.eecs.berkeley.edu/~cs188/textbook/bayes-nets/elimination.html)
11. [Probabilistic Modelling and Reasoning, slides 06: Exact inference (Gutmann, University of Edinburgh, 2025)](https://opencourse.inf.ed.ac.uk/sites/default/files/https/opencourse.inf.ed.ac.uk/pmr/2025/slides06.pdf)
12. [Exact Inference: Elimination and Sum Product (Blei, Columbia lecture notes)](http://www.cs.columbia.edu/~blei/fogm/2014F/lectures/inference-on-trees.pdf)
13. [A Comparison of Algorithms for Inference and Learning in Probabilistic Graphical Models (Frey & Jojic)](https://www.cs.ubc.ca/~murphyk/Teaching/CS532c_Fall04/Papers/freyJojicTutorial.pdf)
14. [Chapter 7: Junction Trees and Message Passing (Graphical Models, Oxford)](https://www.stats.ox.ac.uk/~evans/gms/_book/jt.html)
15. [S. L. Lauritzen, D. J. Spiegelhalter (1988). Local Computations with Probabilities on Graphical Structures and Their Application to Expert Systems. Journal of the Royal Statistical Society Series B (Statistical Methodology).](https://doi.org/10.1111/j.2517-6161.1988.tb01721.x)
16. [Prakash P. SHENOY, Glenn SHAFER (1990). Axioms for Probability and Belief-Function Propagation. Machine intelligence and pattern recognition.](https://doi.org/10.1016/b978-0-444-88650-7.50019-6)
17. [R. Dechter (1996). Bucket elimination. ACM Computing Surveys.](https://doi.org/10.1145/242224.242302)
18. [F.R. Kschischang, B.J. Frey, H.-A. Loeliger (2001). Factor graphs and the sum-product algorithm. IEEE Transactions on Information Theory.](https://doi.org/10.1109/18.910572)
19. [Probabilistic Inference and Learning (Jaakkola, JAIR 1999 tutorial)](https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume10/jaakkola99a-html/node1.html)
20. [Lazy propagation: A junction tree inference algorithm based on lazy evaluation (Artificial Intelligence, 1999)](https://doi.org/10.1016/s0004-3702%2899%2900062-4)
21. [Factor Graphs and Algorithms (Loeliger, Allerton 1997)](https://people.ee.ethz.ch/~loeliger/localpapers/FG_Allerton1997.pdf)
22. [Fast Arc-Reversal (PGM 2024, PMLR v246)](https://proceedings.mlr.press/v246/butz24a.html)
23. [Jiantong Jiang and colleagues (2024). Faster-BNI: Fast Parallel Exact Inference on Bayesian Networks. IEEE Transactions on Parallel and Distributed Systems.](https://doi.org/10.1109/tpds.2024.3414177)
24. [Maene, Jaron, Derkinderen, Vincent, De Raedt, Luc (2024). On the Hardness of Probabilistic Neurosymbolic Learning. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2406.04472)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/exact-inference

English original: https://www.edgepedia.org/exact-inference
