如何计算python中正态累积分布函数的倒数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20626994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate the inverse of the normal cumulative distribution function in python?
提问by Yueyoum
How do I calculate the inverse of the cumulative distribution function (CDF) of the normal distribution in Python?
如何计算 Python 中正态分布的累积分布函数 (CDF) 的倒数?
Which library should I use? Possibly scipy?
我应该使用哪个库?可能是scipy?
采纳答案by Warren Weckesser
NORMSINV(mentioned in a comment) is the inverse of the CDF of the standard normal distribution. Using scipy, you can compute this with the ppfmethod of the scipy.stats.normobject. The acronym ppfstands for percent point function, which is another name for the quantile function.
NORMSINV(在评论中提到)是标准正态分布的 CDF 的倒数。使用scipy,您可以使用对象的ppf方法计算它scipy.stats.norm。首字母缩写词ppf代表百分比点函数,它是分位数函数的另一个名称。
In [20]: from scipy.stats import norm
In [21]: norm.ppf(0.95)
Out[21]: 1.6448536269514722
Check that it is the inverse of the CDF:
检查它是否是 CDF 的倒数:
In [34]: norm.cdf(norm.ppf(0.95))
Out[34]: 0.94999999999999996
By default, norm.ppfuses mean=0 and stddev=1, which is the "standard" normal distribution. You can use a different mean and standard deviation by specifying the locand scalearguments, respectively.
默认情况下,norm.ppf使用 mean=0 和 stddev=1,这是“标准”正态分布。您可以通过分别指定loc和scale参数来使用不同的均值和标准差。
In [35]: norm.ppf(0.95, loc=10, scale=2)
Out[35]: 13.289707253902945
If you look at the source code for scipy.stats.norm, you'll find that the ppfmethod ultimately calls scipy.special.ndtri. So to compute the inverse of the CDF of the standard normal distribution, you could use that function directly:
如果查看 的源代码scipy.stats.norm,您会发现该ppf方法最终调用scipy.special.ndtri. 因此,要计算标准正态分布的 CDF 的倒数,您可以直接使用该函数:
In [43]: from scipy.special import ndtri
In [44]: ndtri(0.95)
Out[44]: 1.6448536269514722
回答by o0omycomputero0o
# given random variable X (house price) with population muy = 60, sigma = 40
import scipy as sc
import scipy.stats as sct
sc.version.full_version # 0.15.1
#a. Find P(X<50)
sct.norm.cdf(x=50,loc=60,scale=40) # 0.4012936743170763
#b. Find P(X>=50)
sct.norm.sf(x=50,loc=60,scale=40) # 0.5987063256829237
#c. Find P(60<=X<=80)
sct.norm.cdf(x=80,loc=60,scale=40) - sct.norm.cdf(x=60,loc=60,scale=40)
#d. how much top most 5% expensive house cost at least? or find x where P(X>=x) = 0.05
sct.norm.isf(q=0.05,loc=60,scale=40)
#e. how much top most 5% cheapest house cost at least? or find x where P(X<=x) = 0.05
sct.norm.ppf(q=0.05,loc=60,scale=40)
回答by Xavier Guihot
Starting Python 3.8, the standard library provides the NormalDistobject as part of the statisticsmodule.
开始Python 3.8,标准库提供NormalDist对象作为statistics模块的一部分。
It can be used to get the inverse cumulative distribution function(inv_cdf- inverse of the cdf), also known as the quantile functionor the percent-point functionfor a given mean(mu) and standard deviation(sigma):
它可用于获得逆累积分布函数(inv_cdf- 的倒数cdf),也称为分位数函数或给定均值( ) 和标准差( )的百分比函数:musigma
from statistics import NormalDist
NormalDist(mu=10, sigma=2).inv_cdf(0.95)
# 13.289707253902943
Which can be simplified for the standard normal distribution(mu = 0and sigma = 1):
对于标准正态分布(mu = 0和sigma = 1)可以简化:
NormalDist().inv_cdf(0.95)
# 1.6448536269514715

