How to calculate cumulative normal distribution? How to calculate cumulative normal distribution? python python

How to calculate cumulative normal distribution?


Here's an example:

>>> from scipy.stats import norm>>> norm.cdf(1.96)0.9750021048517795>>> norm.cdf(-1.96)0.024997895148220435

In other words, approximately 95% of the standard normal interval lies within two standard deviations, centered on a standard mean of zero.

If you need the inverse CDF:

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


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?


Starting Python 3.8, the standard library provides the NormalDist object as part of the statistics module.

It can be used to get the cumulative distribution function (cdf - probability that a random sample X will be less than or equal to x) for a given mean (mu) and standard deviation (sigma):

from statistics import NormalDistNormalDist(mu=0, sigma=1).cdf(1.96)# 0.9750021048517796

Which can be simplified for the standard normal distribution (mu = 0 and sigma = 1):

NormalDist().cdf(1.96)# 0.9750021048517796NormalDist().cdf(-1.96)# 0.024997895148220428