如何计算python中正态累积分布函数的倒数? [英] How to calculate the inverse of the normal cumulative distribution function in python?
问题描述
如何在 Python 中计算正态分布的累积分布函数 (CDF) 的倒数?
How do I calculate the inverse of the cumulative distribution function (CDF) of the normal distribution in Python?
我应该使用哪个库?可能是scipy?
Which library should I use? Possibly scipy?
推荐答案
NORMSINV(在注释)是标准正态分布的 CDF 的倒数.使用 scipy
,您可以使用 scipy.stats.norm
对象.首字母缩略词 ppf
代表 百分比点函数,这是分位数函数的另一个名称.
NORMSINV (mentioned in a comment) is the inverse of the CDF of the standard normal distribution. Using scipy
, you can compute this with the ppf
method of the scipy.stats.norm
object. The acronym ppf
stands for percent point function, which is another name for the quantile function.
In [20]: from scipy.stats import norm
In [21]: norm.ppf(0.95)
Out[21]: 1.6448536269514722
检查它是否是 CDF 的倒数:
Check that it is the inverse of the CDF:
In [34]: norm.cdf(norm.ppf(0.95))
Out[34]: 0.94999999999999996
默认情况下,norm.ppf
使用 mean=0 和 stddev=1,这是标准"正态分布.您可以通过分别指定 loc
和 scale
参数来使用不同的均值和标准差.
By default, norm.ppf
uses mean=0 and stddev=1, which is the "standard" normal distribution. You can use a different mean and standard deviation by specifying the loc
and scale
arguments, respectively.
In [35]: norm.ppf(0.95, loc=10, scale=2)
Out[35]: 13.289707253902945
如果您查看 scipy.stats.norm
的源代码,您会发现 ppf
方法最终调用了 scipy.special.ndtri
.因此,要计算标准正态分布的 CDF 的倒数,您可以直接使用该函数:
If you look at the source code for scipy.stats.norm
, you'll find that the ppf
method ultimately calls scipy.special.ndtri
. So to compute the inverse of the CDF of the standard normal distribution, you could use that function directly:
In [43]: from scipy.special import ndtri
In [44]: ndtri(0.95)
Out[44]: 1.6448536269514722
这篇关于如何计算python中正态累积分布函数的倒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!