# 贝叶斯采样

贝叶斯采样是一类统计方法，它从后验概率分布中抽取随机样本，使得参数估计和不确定性可以由样本本身计算得出，而不必依赖解析公式。后验分布通常只能确定到一个难以计算的归一化常数，而基于模拟的方法绕开了这个积分：后验期望值用样本均值近似，后验分位数用样本分位数近似，后验边缘密度用基于样本的密度估计近似，样本越多，精度越高。<sup>[1](https://www.intechopen.com/chapters/71603)</sup> 后验采样之所以适合这些方法，是因为贝叶斯公式的分子往往容易计算，而标量归一化因子却不然，且主力算法可以在未归一化的密度上运行。<sup>[2](https://www.cs.helsinki.fi/u/ahonkela/teaching/compstats1/book/bayesian-inference-with-mcmc.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 输出 | 联合后验分布的样本，可转换为均值、方差、分位数和边缘密度<sup>[1](https://www.intechopen.com/chapters/71603)</sup> |
| 核心要求 | 算法只需知道未归一化的密度<sup>[2](https://www.cs.helsinki.fi/u/ahonkela/teaching/compstats1/book/bayesian-inference-with-mcmc.html)</sup> |
| 有效性条件 | 转移核必须以该后验分布为平稳分布；细致平衡是充分条件<sup>[3](https://stat210a.berkeley.edu/fall-2025/reader/bayes-computation.html)</sup> |
| 精度指标 | 蒙特卡洛标准误随 \( 1/\sqrt{N_{\mathrm{eff}}} \) 缩放，而非 \( 1/\sqrt{N} \)<sup>[4](https://mc-stan.org/docs/reference-manual/analysis.html)</sup> |
| 最优接受率 | 随机游走 Metropolis 为 0.234；高维哈密顿蒙特卡洛为 0.651<sup>[5](https://ora.ox.ac.uk/objects/uuid:f2472c8e-dc3c-4a45-87d1-d7d60a63b3c8/files/svd66w033r)</sup><sup> • </sup><sup>[6](https://beta.sanzserna.org/wp-content/uploads/2013/01/906_revised_bernoulli.pdf)</sup> |
| 实测效率 | 随机游走 Metropolis 为 0.1% 至 1%；切片采样和基于 HMC 的方法约为 2% 至 40%<sup>[7](https://arxiv.org/html/1712.06006)</sup> |

## 工作原理

基本原理是用抽取样本上的平均代替难以计算的积分。分析者构造一列随机变量 \( \theta^{(0)}, \theta^{(1)}, \ldots \)，使其分布收敛到后验分布，然后取后期抽取中精心挑选的一部分作为后验样本的替代。<sup>[3](https://stat210a.berkeley.edu/fall-2025/reader/bayes-computation.html)</sup> 在马尔可夫链蒙特卡洛（MCMC）中，链的转移核 \( Q \) 被构造为其平稳分布 \( \pi \) 恰为后验分布：\( \pi(y) = \int Q(y \mid x) \pi(x) \, d\mu(x) \)。一个充分条件是细致平衡，即对所有 \( x, y \) 有 \( \pi(x) Q(y \mid x) = \pi(y) Q(x \mid y) \)；在温和条件下，链收敛到其平稳分布。<sup>[3](https://stat210a.berkeley.edu/fall-2025/reader/bayes-computation.html)</sup>

Metropolis-Hastings 是这些方法中心的一个通用框架：给定一个除归一化常数外可计算的密度 \( \pi \) 和一个提议核 \( q(\cdot \mid \cdot) \)，它返回一条具有正确平稳分布的马尔可夫链，通过一个永远不需要归一化因子的比值规则来接受或拒绝提议。<sup>[8](https://link.springer.com/article/10.1007/s11222-015-9574-5)</sup> 问题在于，马尔可夫链只有在收敛到平衡之后才能从目标分布中采样，而收敛只在渐近意义下得到保证，因此实践中需要有限样本的诊断工具。<sup>[4](https://mc-stan.org/docs/reference-manual/analysis.html)</sup>

## 操作流程

实践者的工作流分为五个阶段。第一，设定模型和先验，以便能够逐点计算未归一化的后验。第二，选择采样器。哈密顿蒙特卡洛（HMC）利用目标密度的导数来提出高效的转移，在蛙跳积分之后进行 Metropolis 接受修正；其效率对三个调节参数高度敏感：步长 \( \epsilon \)、度量 \( M \) 和蛙跳步数 \( L \)。<sup>[9](https://mc-stan.org/docs/reference-manual/mcmc.html)</sup> 诸如 Stan 之类的软件会自动将 \( \epsilon \) 优化到目标接受率，从预热迭代中估计 \( M \)，并使用无掉头采样器（NUTS）动态调整 \( L \)。<sup>[9](https://mc-stan.org/docs/reference-manual/mcmc.html)</sup>

第三，运行多条链。标准建议是从扰动后的起点并行模拟三条或更多链，丢弃前半部分模拟作为预热，并监测链内的平稳性和链间的混合情况。<sup>[10](https://stat.columbia.edu/~gelman/research/published/GelmanMCMCchapter4.pdf)</sup> 第四，检查收敛性：R-hat 统计量比较链内和链间方差，其值接近 1.0（通常低于 1.05，或低于 1.01）表明各链已收敛到同一分布；相关库也会直接计算有效样本量（ESS）。<sup>[11](https://blackjax-devs.github.io/blackjax/examples/diagnostics.html)</sup> 第五，计算汇总统计量。Stan 会为每个参数估计一个有效样本量，它在 MCMC 中心极限定理中扮演独立抽取次数的角色，而蒙特卡洛标准误与 \( 1/\sqrt{N_{\mathrm{eff}}} \) 成正比。<sup>[4](https://mc-stan.org/docs/reference-manual/analysis.html)</sup>

## 起源

W. K. Hastings 在其 1970 年发表于《生物计量学》的论文中推广了 Metropolis 采样，证明了计算可以仅通过样本点上形如 \( p(x')/p(x) \) 的比值来依赖目标密度。<sup>[12](https://doi.org/10.1093/biomet/57.1.97)</sup> Stuart Geman 和 Donald Geman 在其 1984 年关于贝叶斯图像复原的论文中引入了吉布斯采样，该文还确立了 MRF-吉布斯等价性，用能量函数给出了联合分布。<sup>[13](https://doi.org/10.1109/tpami.1984.4767596)</sup> 同一时期的相关脉络还包括 S. Kirkpatrick、C. D. Gelatt 和 M. P. Vecchi（1983）在《科学》上介绍并引入应用数学领域的模拟退火<sup>[14](https://doi.org/10.1126/science.220.4598.671)</sup>，以及 Martin A. Tanner 和 Wing Hung Wong（1987）在《美国统计协会杂志》上提出的数据增广。<sup>[15](https://doi.org/10.1080/01621459.1987.10478458)</sup> Alan E. Gelfand 和 Adrian F. M. Smith 1990 年发表于《美国统计协会杂志》的论文表明，全条件分布唯一确定联合分布，从而确定边缘分布，并预示着 MCMC 方法进入主流统计学。<sup>[16](https://doi.org/10.1080/01621459.1990.10476213)</sup>

## 变体

最流行的 Metropolis-Hastings 变体是随机游走 Metropolis（RWM），它提议当前状态加上一个来自球对称分布（如高斯分布）的抽取；独立采样器则使用不依赖于当前状态的提议。<sup>[17](https://utstat.toronto.edu/craiu/Papers/final_ARS.pdf)</sup> 有名称的 MH 改进包括延迟拒绝和多试 Metropolis。<sup>[17](https://utstat.toronto.edu/craiu/Papers/final_ARS.pdf)</sup> 切片采样通过两步吉布斯式更新在密度曲线下方区域均匀抽取，并保留水平坐标。<sup>[1](https://www.intechopen.com/chapters/71603)</sup>

HMC 可以看作使用辅助变量和微分方程路径的 Metropolis-Hastings 特例，即使状态移动幅度很大也能达到高接受率。NUTS 是 HMC 的一种改进，它自适应地选择蛙跳步长和步数，递归地构造候选点，并在轨迹开始折返时停止；它还使用原始-对偶平均动态调整步长，因此可以无需手动调参运行。<sup>[18](https://www.jmlr.org/papers/volume15/hoffman14a/hoffman14a.pdf)</sup> 在一项大型基准测试中，随机游走 Metropolis 方法的效率在 0.1% 至 1% 范围内，而切片采样和基于 HMC 的方法达到约 2% 至 40%，其中 NUTS 表现最佳。<sup>[7](https://arxiv.org/html/1712.06006)</sup> 在 MCMC 家族之外，当能够设计出良好的提议密度时，重要性采样和带重采样思想的序贯蒙特卡洛可以提升复杂随机系统的效率。<sup>[19](https://people.bordeaux.inria.fr/pierre.delmoral/fearnhed-alternative-smc-mcmc.pdf)</sup>

维度决定成本。对于蛙跳积分器，当维度 \( d \to \infty \) 时，HMC 需要 \( O(d^{1/4}) \) 步来遍历状态空间，而随机游走 Metropolis 需要 \( O(d) \) 步，MALA 需要 \( O(d^{1/3}) \) 步；在蛙跳步长 \( h = l \cdot d^{-1/4} \) 下，平均接受概率保持为 \( O(1) \)。<sup>[6](https://beta.sanzserna.org/wp-content/uploads/2013/01/906_revised_bernoulli.pdf)</sup> 在对具有独立同分布分量的目标进行缩放分析时，高维下 HMC 的渐近最优接受概率为 0.651。<sup>[6](https://beta.sanzserna.org/wp-content/uploads/2013/01/906_revised_bernoulli.pdf)</sup> 对于 RWM，在使用期望平方跳跃距离准则时，当目标分布满足一般且切合实际的充分条件时，0.234 的渐近最优接受率成立。<sup>[5](https://ora.ox.ac.uk/objects/uuid:f2472c8e-dc3c-4a45-87d1-d7d60a63b3c8/files/svd66w033r)</sup> 在一项 250 维多元正态比较中，NUTS 运行了 2,000 次迭代（1,000 次预热），总共需要约 1,000,000 次梯度和似然评估，而 RWM 运行了 1,000,000 次迭代，提议被调到最优的 0.234 接受率，每次迭代成本与一次 NUTS 梯度评估实际相同。<sup>[18](https://www.jmlr.org/papers/volume15/hoffman14a/hoffman14a.pdf)</sup>

## 应用

继 Gelfand 和 Smith 1990 年的工作之后，MCMC 成为贝叶斯数据分析的标准计算主力，也是具有实际意义的统计模型的基本工具集。<sup>[16](https://doi.org/10.1080/01621459.1990.10476213)</sup> 此后，MCMC 和重要性采样算法被应用于遗传学、生物学、神经科学、天体物理学、图像分析、生态学、流行病学、工程学、经济学、政治学、市场营销和金融等领域，将贝叶斯分析带入统计学主流。<sup>[20](https://ar5iv.labs.arxiv.org/html/2112.10342)</sup>

## 局限与替代方案

MCMC 存在结构性局限。由于 MCMC 抽取的样本是相关的，长度为 \( N \) 的链给出的估计精度远低于 \( N \) 个独立样本，且精度取决于预热时间（依赖于初始化）和混合时间（是提议的函数）。<sup>[7](https://arxiv.org/html/1712.06006)</sup> 当目标分布是多峰的时候，像 RWM 这样的简单算法可能在一个模态区域内探索得很好，但在模态之间移动需要不可行的时间，导致收敛极慢、估计很差。<sup>[17](https://utstat.toronto.edu/craiu/Papers/final_ARS.pdf)</sup> 诊断本身也可能误导：有许多方法可以构造出 MCMC 过程不可检测地失败的例子，包括远距离模态、Neal 漏斗和极端病态条件；诊断的第一类错误率低，但对第二类错误没有保证。<sup>[7](https://arxiv.org/html/1712.06006)</sup>

主要的替代方案以偏差换速度。诸如拉普拉斯近似和变分贝叶斯之类的近似方法带有固有偏差，通常无法通过更密集的计算来减少，而采样方法的精度只受计算资源限制。<sup>[1](https://www.intechopen.com/chapters/71603)</sup> 生成变分近似通常比精确后验模拟快得多，往往快几个数量级，并且即使参数维度达到数千或数万也能变得可行。<sup>[20](https://ar5iv.labs.arxiv.org/html/2112.10342)</sup> Stan 的变分推断输出可以像 MCMC 输出一样分析，尽管它不是马尔可夫链，其拉普拉斯算法从以无约束空间中众数为中心的正态近似中产生一个样本。<sup>[4](https://mc-stan.org/docs/reference-manual/analysis.html)</sup>

## References

1. [A Brief Tour of Bayesian Sampling Methods (IntechOpen)](https://www.intechopen.com/chapters/71603)
2. [Chapter 7: Bayesian inference with MCMC | Computational Statistics I](https://www.cs.helsinki.fi/u/ahonkela/teaching/compstats1/book/bayesian-inference-with-mcmc.html)
3. [Markov Chain Monte Carlo (Berkeley Stat 210A reader)](https://stat210a.berkeley.edu/fall-2025/reader/bayes-computation.html)
4. [Stan Reference Manual: Posterior Analysis](https://mc-stan.org/docs/reference-manual/analysis.html)
5. [Optimal Scaling of Random-Walk Metropolis Algorithms on General Target Distributions](https://ora.ox.ac.uk/objects/uuid:f2472c8e-dc3c-4a45-87d1-d7d60a63b3c8/files/svd66w033r)
6. [Optimal tuning of the Hybrid Monte-Carlo Algorithm](https://beta.sanzserna.org/wp-content/uploads/2013/01/906_revised_bernoulli.pdf)
7. [Beyond ELBOs: A Large-Scale Evaluation of Variational Methods for Sampling](https://arxiv.org/html/1712.06006)
8. [Bayesian computation: a summary of the current state, and samples backwards and forwards (Statistics and Computing)](https://link.springer.com/article/10.1007/s11222-015-9574-5)
9. [Stan Reference Manual: MCMC Sampling](https://mc-stan.org/docs/reference-manual/mcmc.html)
10. [Bayesian Data Analysis chapter: Inference and diagnostics for MCMC (Gelman)](https://stat.columbia.edu/~gelman/research/published/GelmanMCMCchapter4.pdf)
11. [MCMC Diagnostics (BlackJAX)](https://blackjax-devs.github.io/blackjax/examples/diagnostics.html)
12. [W. K. Hastings (1970). Monte Carlo sampling methods using Markov chains and their applications. Biometrika.](https://doi.org/10.1093/biomet/57.1.97)
13. [Stuart Geman, Donald Geman (1984). Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images. IEEE Transactions on Pattern Analysis and Machine Intelligence.](https://doi.org/10.1109/tpami.1984.4767596)
14. [S. Kirkpatrick, C. D. Gelatt, M. P. Vecchi (1983). Optimization by Simulated Annealing. Science.](https://doi.org/10.1126/science.220.4598.671)
15. [Martin A. Tanner, Wing Hung Wong (1987). The Calculation of Posterior Distributions by Data Augmentation. Journal of the American Statistical Association.](https://doi.org/10.1080/01621459.1987.10478458)
16. [Alan E. Gelfand, Adrian F. M. Smith (1990). Sampling-Based Approaches to Calculating Marginal Densities. Journal of the American Statistical Association.](https://doi.org/10.1080/01621459.1990.10476213)
17. [Bayesian Computation Via Markov Chain Monte Carlo (Annual Review of Statistics and Its Application)](https://utstat.toronto.edu/craiu/Papers/final_ARS.pdf)
18. [The No-U-Turn Sampler: Adaptively Setting Path Lengths in Hamiltonian Monte Carlo](https://www.jmlr.org/papers/volume15/hoffman14a/hoffman14a.pdf)
19. [Computational Methods for Complex Stochastic Systems (Fearnhead & Del Moral, SMC as alternative to MCMC)](https://people.bordeaux.inria.fr/pierre.delmoral/fearnhed-alternative-smc-mcmc.pdf)
20. [Approximating Bayes in the 21st Century](https://ar5iv.labs.arxiv.org/html/2112.10342)

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

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Bayesian statistics › Bayesian computation and software

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/bayesian-sampling

English original: https://www.edgepedia.org/bayesian-sampling
