# 逆变换采样

**逆变换采样**（亦称反演法、逆CDF法，或反向的概率积分变换）是一种从概率分布生成随机样本的方法：将该分布累积分布函数（CDF）的逆函数作用于均匀随机数。若 \( U \sim \mathrm{Unif}(0,1) \) 且 \( F \) 为目标CDF，则 \( X = F^{-1}(U) \) 恰好服从分布 \( F \)。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup> 该方法是蒙特卡洛模拟的基本构件——计算机可以轻易产生均匀随机数，但天生无法直接产生指数型或钟形随机数。<sup>[2](https://jovanaeducation.com/guides/prob-trf-4)</sup>

| 关键事实 | 细节 |
|---|---|
| 核心构造 | 抽取 \( U \sim \mathrm{Unif}(0,1) \)，返回 \( X = F^{-1}(U) \)；结果恰好具有CDF \( F \)。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup> |
| 适用范围 | 适用于CDF可计算且可逆的连续分布，也通过广义逆适用于离散分布。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup><sup> • </sup><sup>[3](https://edgekit-docs.vercel.app/probstats/monte-carlo-methods)</sup> |
| 广义逆 | \( F^{-1}(u) = \inf\{x : F(x) \geq u\} \) 可处理非严格递增的CDF。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup> |
| 指数分布示例 | 对于速率 \( \lambda \)，\( F^{-1}(u) = -\frac{1}{\lambda}\log(1-u) \)。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup> |
| 主要局限 | 需要可用的分位数函数；正态CDF没有闭式逆函数，这促使了Box-Muller变换等替代方法的出现。<sup>[2](https://jovanaeducation.com/guides/prob-trf-4)</sup> |
| 实际应用 | R的`rnorm`自2003年起通过反演法生成正态随机数。<sup>[4](https://link.springer.com/chapter/10.1007/978-3-030-83640-5_2)</sup> |
| 数值注意事项 | 接近1的有限精度均匀数限制尾部采样；重写逆函数使取值大的样本对应接近0的均匀数，可保持尾部行为。<sup>[5](https://treese41528.github.io/ComputationalDataScience/Website/part1_foundations/chapter2/ch2_3-inverse-cdf-method.html)</sup> |

## 原理

该方法建立在概率积分变换之上：对任意连续随机变量 \( X \)，将其自身的CDF施加于其样本，\( U = F(X) \)，产生 \( (0,1) \) 上的均匀随机变量；逆映射 \( X = F^{-1}(U) \) 则将此过程反向运行。<sup>[6](https://tudelft-citg.github.io/MORE-lecture-notes/main/mcmc/sampling.html)</sup> 证明很短。若 \( Y = F^{-1}(U) \)，则

\[ \mathbb{P}(Y \leq y) = \mathbb{P}(F^{-1}(U) \leq y) = \mathbb{P}(U \leq F(y)) = F(y), \]

因此 \( Y \) 与 \( X \) 具有相同的CDF。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup> 中间一步利用了 \( F^{-1} \) 的递增性，最后一步利用了均匀变量的性质 \( \mathbb{P}(U \leq u) = u \)。

算法相应地简单：从 \( U(0,1) \) 生成 \( u \)，计算 \( x = F^{-1}(u) \)，返回 \( x \)。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup> 当 \( F \) 非严格递增时，普通逆函数不存在，但广义逆 \( F^{-1}(u) = \inf\{x : F(x) \geq u\} \) 仍能产生正确样本；离散情形也使用这一方法，逐级累加概率直至超过 \( u \)。<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup><sup> • </sup><sup>[3](https://edgekit-docs.vercel.app/probstats/monte-carlo-methods)</sup>

## 实例演算

**指数分布。** 对于在 \( x \geq 0 \) 上密度为 \( f(x) = \lambda e^{-\lambda x} \) 的 \( X \)，其CDF为 \( F(x) = 1 - e^{-\lambda x} \)。对 \( x \) 解方程 \( u = 1 - e^{-\lambda x} \)，得

\[ F^{-1}(u) = -\frac{1}{\lambda}\log(1-u). \]<sup>[1](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)</sup>

**Gumbel分布。** 其CDF为 \( u = F(x) = e^{-e^{-(x-\mu)/\beta}} \)，对 \( x \) 求解得分位数函数

\[ F^{-1}(u) = \mu - \beta \ln(-\ln(u)). \]

抽取五个均匀数并通过此函数映射（取 \( \mu = 0 \)、\( \beta = 1 \)），即产生Gumbel分布的样本。<sup>[6](https://tudelft-citg.github.io/MORE-lecture-notes/main/mcmc/sampling.html)</sup>

**离散分布。** 同样的构造适用：将 \( (0,1) \) 划分为若干区间，区间长度等于各结果的概率，抽取 \( u \)，返回包含 \( u \) 的区间所对应的结果。<sup>[3](https://edgekit-docs.vercel.app/probstats/monte-carlo-methods)</sup>

## 逆函数不可得时

该方法坦率的短板在于，第二步要求一个可用的 \( F^{-1} \)，而许多著名分布并没有简洁的逆函数。<sup>[2](https://jovanaeducation.com/guides/prob-trf-4)</sup> 正态分布是经典案例：其CDF没有闭式逆函数，因此朴素的逆变换法在此搁浅，这正是Box-Muller变换存在的原因——它通过变量替换，从两个均匀数产生两个独立正态数。<sup>[2](https://jovanaeducation.com/guides/prob-trf-4)</sup> 实践中存在若干条路径：

- **数值反演。** 当仅知密度函数时，可通过数值积分计算CDF，再用多项式插值求逆。SciPy的`NumericalInversePolynomial`构建此类近似，多项式阶数在3到17之间，默认容忍u误差为 \( 10^{-10} \)；考虑到大多数均匀数生成器的分辨率约为 \( 2.3 \times 10^{-10} \)，\( 10^{-10} \) 实际上已是精确的。<sup>[7](https://vaje.um.si/docs/scipy-html/reference/generated/scipy.stats.sampling.NumericalInversePolynomial.html)</sup> 该方法在重尾分布中会遇到困难——此时逆CDF在0或1附近变得非常陡峭；当要求的u分辨率低于 \( 10^{-12} \) 时，柯西分布便暴露此问题。<sup>[7](https://vaje.um.si/docs/scipy-html/reference/generated/scipy.stats.sampling.NumericalInversePolynomial.html)</sup>
- **Chebyshev逼近。** Olver和Townsend于2013年证明，使用Chebyshev多项式逼近的逆变换采样在一维中计算高效且稳健，当CDF可由低秩函数良好近似时可推广至二维。Chebsampling工具（2022年）去除了低秩要求，将该方法应用于空间等离子体中的非高斯分布函数。<sup>[8](https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2021JA030031)</sup>
- **矩匹配替代。** 在某些物理和生物医学模拟中，目标击中时间分布包含无法高效求逆的表达式。一种方法是将逆函数可高效计算的两参数F分布变换为四参数分布，并使其前四阶矩与精确击中时间分布匹配；该近似产生的误差非常小。<sup>[9](https://onlinelibrary.wiley.com/doi/abs/10.1002/eng2.12302)</sup>
- **其他方法。** 接受-拒绝采样需要另一种资源：一个易于采样的提议密度 \( g \) 和一个常数 \( M \)，使得处处有 \( f(y) \leq M g(y) \)，并以概率 \( 1/M \) 接受每个提议。<sup>[10](https://chikuang.github.io/teaching/2026F_stat8670/04-generating-rv.html)</sup> 然而，在使用低差异序列的拟蒙特卡洛设置中，接受-拒绝法相比逆变换法可显著改善计算时间。<sup>[11](https://arxiv.org/abs/1403.5599)</sup>

## 数值精度与尾部

浮点表示在 \( (0,1) \) 上是不对称的：数可以表示得比接近1更接近0。在单精度IEEE算术下，零可以从上方被逼近至约 \( 10^{-44} \) 的微小值，而低于1的最接近可表示值仅为约 \( 1 - 10^{-7} \)。对于通过CDF反演进行高斯采样，这意味着最大可产生值约为+5，而最小值为-14，且大值出现更频繁，因为其输入覆盖了均匀范围中更宽的区段。标准修正方法是仅对小于0.5的值应用逆CDF，随后附加随机符号。<sup>[12](https://www.researchgate.net/publication/220566074_Gaussian_random_number_generators)</sup>

同样的问题出现在指数采样中。在float64中，接近1的数之间的最小间隔约为 \( 10^{-16} \)，因此 \( -\ln(1-U) \) 实际上被截断在36到37左右，截去了最右侧的尾部。由于 \( U \) 和 \( 1-U \) 同样服从(0,1)上的均匀分布，等价形式 \( X = -\ln(U)/\lambda \) 将取值大的样本置于浮点分辨率更精细的位置——接近 \( U = 0 \) 处——从而远更好地保持尾部行为。<sup>[5](https://treese41528.github.io/ComputationalDataScience/Website/part1_foundations/chapter2/ch2_3-inverse-cdf-method.html)</sup>

## 应用

逆变换采样出现在一切需要非均匀随机数来驱动更大蒙特卡洛算法的模拟中，尤其是在数值积分不具竞争力的高维情形。<sup>[4](https://link.springer.com/chapter/10.1007/978-3-030-83640-5_2)</sup> 拉丁超立方抽样通过在单位区间上分散取点（每区间一个），并在这些点处求逆CDF值，生成分层样本。<sup>[13](https://nehrpsearch.nist.gov/static/files/NSF/PB98108988.pdf)</sup> 在计算物理中，NIST一种用于多普勒增宽原子光谱的方法，通过CDF的逆变换对一维热分布进行等概率采样，比常规的速度加权采样更高效地达到平滑极限，并可广泛适用于正态分布。<sup>[14](https://www.nist.gov/publications/inverse-transform-sampling-efficient-doppler-averaged-spectroscopy-simulations)</sup>

当分位数函数必须在另一算法内部进行数值反演时，该方法也带来可测量的成本：在一个基于分位数的贝叶斯模型中，分位数函数的数值反演每条2000样本的链平均增加6.43秒，尽管对后验的主体或尾部的有效样本量没有产生显著差异。<sup>[15](https://www.sciencedirect.com/science/article/pii/S0167947323001068)</sup>

## References

1. [Inverse Transform Sampling, Monte Carlo Methods Lecture Notes](https://apurvanakade.github.io/Monte-Carlo-Methods/chapters/sampling/inverse_transform.html)
2. [The Probability Integral Transform, JOVANA Education](https://jovanaeducation.com/guides/prob-trf-4)
3. [Simulation & Monte Carlo, edgekit docs](https://edgekit-docs.vercel.app/probstats/monte-carlo-methods)
4. [Sampling from Complex Probability Distributions: A Monte Carlo Primer for Engineers, Springer](https://link.springer.com/chapter/10.1007/978-3-030-83640-5_2)
5. [Section 2.3 Inverse CDF Method, STAT 418](https://treese41528.github.io/ComputationalDataScience/Website/part1_foundations/chapter2/ch2_3-inverse-cdf-method.html)
6. [Monte Carlo simulation, MORE lecture notes, TU Delft](https://tudelft-citg.github.io/MORE-lecture-notes/main/mcmc/sampling.html)
7. [NumericalInversePolynomial, SciPy v1.16.2 Manual](https://vaje.um.si/docs/scipy-html/reference/generated/scipy.stats.sampling.NumericalInversePolynomial.html)
8. [Fast Inverse Transform Sampling of Non-Gaussian Distribution Functions in Space Plasmas, JGR Space Physics (2022)](https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2021JA030031)
9. [Inverse functions for Monte Carlo simulations with applications to hitting time distributions, Engineering Reports (2021)](https://onlinelibrary.wiley.com/doi/abs/10.1002/eng2.12302)
10. [Generating Random Variables, STAT 8670 Computational Methods in Statistics](https://chikuang.github.io/teaching/2026F_stat8670/04-generating-rv.html)
11. [The acceptance-rejection method for low-discrepancy sequences, arXiv:1403.5599](https://arxiv.org/abs/1403.5599)
12. [Gaussian random number generators, ACM Computing Surveys (2007)](https://www.researchgate.net/publication/220566074_Gaussian_random_number_generators)
13. [Probabilistic and hazard analysis report, NIST/NSF archive](https://nehrpsearch.nist.gov/static/files/NSF/PB98108988.pdf)
14. [Inverse Transform Sampling for Efficient Doppler-Averaged Spectroscopy Simulations, NIST (2023)](https://www.nist.gov/publications/inverse-transform-sampling-efficient-doppler-averaged-spectroscopy-simulations)
15. [The tenets of quantile-based inference in Bayesian models, ScienceDirect (2023)](https://www.sciencedirect.com/science/article/pii/S0167947323001068)

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

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Probability theory › Random variables › Algebra and transformations of random variables › Functions of random variables

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/inverse-transform-sampling

English original: https://www.edgepedia.org/inverse-transform-sampling
