How to calculate cumulative normal distribution?

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.

123k 29 29 gold badges 177 177 silver badges 309 309 bronze badges asked Apr 30, 2009 at 22:13

8 Answers 8

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) 
answered Apr 30, 2009 at 22:24 Alex Reynolds Alex Reynolds 96.6k 58 58 gold badges 248 248 silver badges 349 349 bronze badges

Also, you can specify the mean (loc) and variance (scale) as parameters. e.g, d = norm(loc=10.0, scale=2.0); d.cdf(12.0); Details here: docs.scipy.org/doc/scipy-0.14.0/reference/generated/…

Commented Oct 31, 2014 at 13:41 @Irvan, the scale parameter is actually the standard deviation, NOT the variance. Commented Jun 2, 2015 at 19:08

Why does scipy name these as loc and scale ? I used the help(norm.ppf) but then what the heck are loc and scale - need a help for the help..

Commented Dec 22, 2016 at 20:31

@javadba - location and scale are more general terms in statistics that are used to parameterize a wide range of distributions. For the normal distribution, they line up with mean and sd, but not so for other distributions.

Commented Aug 25, 2017 at 17:59

@MichaelOhlrogge . Thx! Here is a page from NIST explaining further itl.nist.gov/div898/handbook/eda/section3/eda364.htm

Commented Aug 25, 2017 at 18:03

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 
2,600 22 22 silver badges 21 21 bronze badges answered Mar 26, 2015 at 7:40 1,020 9 9 silver badges 13 13 bronze badges

This was exactly what I was looking for. If someone else than me wonders how this can be used to calculate "percentage of data that lies within the standard distribution", well: 1 - (1 - phi(1)) * 2 = 0.6827 ("68% of data within 1 standard deviation")

Commented Jul 10, 2017 at 18:30

For a general normal distribution, it would be def phi(x, mu, sigma): return (1 + erf((x - mu) / sigma / sqrt(2))) / 2 .

Commented Mar 15, 2020 at 19:18

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 NormalDist NormalDist(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.9750021048517796 NormalDist().cdf(-1.96) # 0.024997895148220428