How to calculate cumulative normal distribution in Python

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

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.

回答1:

Here's an example:

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

If you need the inverse CDF:

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


回答2:

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)

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

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/3/library/math.html

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



回答3:

Adapted from here 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. - r  def ncdf(x):     return 1. - 0.5*erfcc(x/(2**0.5)) 


回答4:

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

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 y  def normpdf(x, mu, sigma):     u = (x-mu)/abs(sigma)     y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)     return y  def normdist(x, mu, sigma, f):     if f:         y = normcdf(x,mu,sigma)     else:         y = normpdf(x,mu,sigma)     return y 


回答5:

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:

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

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



回答6:

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

      ;; 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  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!