如何计算Python中累积正态分布?

时间:2022-04-20 01:17:04

I am looking for a function in Numpy or Scipy (or any rigorous Python library) that will give me the cumulative normal distribution function in Python.

我正在寻找一个Numpy或Scipy(或任何严格的Python库)中的函数,它将为我提供Python中的累积正态分布函数。

6 个解决方案

#1


88  

Here's an example:

这里有一个例子:

>>> from scipy.stats import norm>>> norm.cdf(1.96)array(0.97500210485177952)

If you need the inverse CDF:

如果你需要逆CDF:

>>> norm.ppf(norm.cdf(1.96))array(1.9599999999999991)

#2


25  

It may be too late to answer the question but since Google still leads people here, I decide to write my solution here.

现在回答这个问题可能已经太晚了,但是既然谷歌仍然引导人们来到这里,我决定在这里写下我的解决方案。

That is, since Python 2.7, the math library has integrated the error function math.erf(x)

也就是说,由于Python 2.7,数学库集成了error函数math.erf(x)

The erf() function can be used to compute traditional statistical functions such as the cumulative standard normal distribution:

erf()函数可用于计算传统的统计函数,如累积标准正态分布:

from math import *def phi(x):    #'Cumulative distribution function for the standard normal distribution'    return (1.0 + erf(x / sqrt(2.0))) / 2.0

Ref:

裁判:

https://docs.python.org/2/library/math.html

https://docs.python.org/2/library/math.html

https://docs.python.org/3/library/math.html

https://docs.python.org/3/library/math.html

How are the Error Function and Standard Normal distribution function related?

误差函数和标准正态分布函数有什么关系?

#3


18  

Adapted from here http://mail.python.org/pipermail/python-list/2000-June/039873.html

改编自这里http://mail.python.org/pipermail/python-list/2000-June/039873.html

from math import *def erfcc(x):    """Complementary error function."""    z = abs(x)    t = 1. / (1. + 0.5*z)    r = t * exp(-z*z-1.26551223+t*(1.00002368+t*(.37409196+        t*(.09678418+t*(-.18628806+t*(.27886807+        t*(-1.13520398+t*(1.48851587+t*(-.82215223+        t*.17087277)))))))))    if (x >= 0.):        return r    else:        return 2. - rdef ncdf(x):    return 1. - 0.5*erfcc(x/(2**0.5))

#4


12  

To build upon Unknown's example, the Python equivalent of the function normdist() implemented in a lot of libraries would be:

在Unknown示例的基础上,在许多库中实现的与函数normdist()等价的Python将是:

def normcdf(x, mu, sigma):    t = x-mu;    y = 0.5*erfcc(-t/(sigma*sqrt(2.0)));    if y>1.0:        y = 1.0;    return ydef normpdf(x, mu, sigma):    u = (x-mu)/abs(sigma)    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)    return ydef normdist(x, mu, sigma, f):    if f:        y = normcdf(x,mu,sigma)    else:        y = normpdf(x,mu,sigma)    return y

#5


7  

Alex's answer shows you a solution for standard normal distribution (mean = 0, standard deviation = 1). If you have normal distribution with mean and std (which is sqr(var)) and you want to calculate:

Alex的回答给出了一个标准正态分布(均值= 0,标准差= 1)的解,如果正态分布为均值和std (var),需要计算:

from scipy.stats import norm# cdf(x < val)print norm.cdf(val, m, s)# cdf(x > val)print 1 - norm.cdf(val, m, s)# cdf(v1 < x < v2)print norm.cdf(v2, m, s) - norm.cdf(v1, m, s)

Read more about cdf here and scipy implementation of normal distribution with many formulas here.

这里有更多关于cdf的信息,以及在这里有很多公式的正态分布的scipy实现。

#6


-6  

As Google gives this answer for the search netlogo pdf, here's the netlogo version of the above python code

谷歌给出了搜索netlogo pdf的答案,下面是上面python代码的netlogo版本

    ;; Normal distribution cumulative density function    to-report normcdf [x mu sigma]        let t x - mu        let y 0.5 * erfcc [ - t / ( sigma * sqrt 2.0)]        if ( y > 1.0 ) [ set y 1.0 ]        report y    end    ;; Normal distribution probability density function    to-report normpdf [x mu sigma]        let u = (x - mu) / abs sigma        let y = 1 / ( sqrt [2 * pi] * abs sigma ) * exp ( - u * u / 2.0)        report y    end    ;; Complementary error function    to-report erfcc [x]        let z abs x        let t 1.0 / (1.0 + 0.5 * z)        let r t *  exp ( - z * z -1.26551223 + t * (1.00002368 + t * (0.37409196 +            t * (0.09678418 + t * (-0.18628806 + t * (.27886807 +            t * (-1.13520398 +t * (1.48851587 +t * (-0.82215223 +            t * .17087277 )))))))))        ifelse (x >= 0) [ report r ] [report 2.0 - r]    end

#1


88  

Here's an example:

这里有一个例子:

>>> from scipy.stats import norm>>> norm.cdf(1.96)array(0.97500210485177952)

If you need the inverse CDF:

如果你需要逆CDF:

>>> norm.ppf(norm.cdf(1.96))array(1.9599999999999991)

#2


25  

It may be too late to answer the question but since Google still leads people here, I decide to write my solution here.

现在回答这个问题可能已经太晚了,但是既然谷歌仍然引导人们来到这里,我决定在这里写下我的解决方案。

That is, since Python 2.7, the math library has integrated the error function math.erf(x)

也就是说,由于Python 2.7,数学库集成了error函数math.erf(x)

The erf() function can be used to compute traditional statistical functions such as the cumulative standard normal distribution:

erf()函数可用于计算传统的统计函数,如累积标准正态分布:

from math import *def phi(x):    #'Cumulative distribution function for the standard normal distribution'    return (1.0 + erf(x / sqrt(2.0))) / 2.0

Ref:

裁判:

https://docs.python.org/2/library/math.html

https://docs.python.org/2/library/math.html

https://docs.python.org/3/library/math.html

https://docs.python.org/3/library/math.html

How are the Error Function and Standard Normal distribution function related?

误差函数和标准正态分布函数有什么关系?

#3


18  

Adapted from here http://mail.python.org/pipermail/python-list/2000-June/039873.html

改编自这里http://mail.python.org/pipermail/python-list/2000-June/039873.html

from math import *def erfcc(x):    """Complementary error function."""    z = abs(x)    t = 1. / (1. + 0.5*z)    r = t * exp(-z*z-1.26551223+t*(1.00002368+t*(.37409196+        t*(.09678418+t*(-.18628806+t*(.27886807+        t*(-1.13520398+t*(1.48851587+t*(-.82215223+        t*.17087277)))))))))    if (x >= 0.):        return r    else:        return 2. - rdef ncdf(x):    return 1. - 0.5*erfcc(x/(2**0.5))

#4


12  

To build upon Unknown's example, the Python equivalent of the function normdist() implemented in a lot of libraries would be:

在Unknown示例的基础上,在许多库中实现的与函数normdist()等价的Python将是:

def normcdf(x, mu, sigma):    t = x-mu;    y = 0.5*erfcc(-t/(sigma*sqrt(2.0)));    if y>1.0:        y = 1.0;    return ydef normpdf(x, mu, sigma):    u = (x-mu)/abs(sigma)    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)    return ydef normdist(x, mu, sigma, f):    if f:        y = normcdf(x,mu,sigma)    else:        y = normpdf(x,mu,sigma)    return y

#5


7  

Alex's answer shows you a solution for standard normal distribution (mean = 0, standard deviation = 1). If you have normal distribution with mean and std (which is sqr(var)) and you want to calculate:

Alex的回答给出了一个标准正态分布(均值= 0,标准差= 1)的解,如果正态分布为均值和std (var),需要计算:

from scipy.stats import norm# cdf(x < val)print norm.cdf(val, m, s)# cdf(x > val)print 1 - norm.cdf(val, m, s)# cdf(v1 < x < v2)print norm.cdf(v2, m, s) - norm.cdf(v1, m, s)

Read more about cdf here and scipy implementation of normal distribution with many formulas here.

这里有更多关于cdf的信息,以及在这里有很多公式的正态分布的scipy实现。

#6


-6  

As Google gives this answer for the search netlogo pdf, here's the netlogo version of the above python code

谷歌给出了搜索netlogo pdf的答案,下面是上面python代码的netlogo版本

    ;; Normal distribution cumulative density function    to-report normcdf [x mu sigma]        let t x - mu        let y 0.5 * erfcc [ - t / ( sigma * sqrt 2.0)]        if ( y > 1.0 ) [ set y 1.0 ]        report y    end    ;; Normal distribution probability density function    to-report normpdf [x mu sigma]        let u = (x - mu) / abs sigma        let y = 1 / ( sqrt [2 * pi] * abs sigma ) * exp ( - u * u / 2.0)        report y    end    ;; Complementary error function    to-report erfcc [x]        let z abs x        let t 1.0 / (1.0 + 0.5 * z)        let r t *  exp ( - z * z -1.26551223 + t * (1.00002368 + t * (0.37409196 +            t * (0.09678418 + t * (-0.18628806 + t * (.27886807 +            t * (-1.13520398 +t * (1.48851587 +t * (-0.82215223 +            t * .17087277 )))))))))        ifelse (x >= 0) [ report r ] [report 2.0 - r]    end