Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate cumulative normal distribution in python?

I am currently using Excel to calculate the cumulative normal distribution using the following

x = 0
mean = 0.03
standard deviation = 0.055

I then use the formula

=1-NORMDIST(0,0.03,0.055,TRUE)

This yields an output of 0.7, which is what I'm looking for.

How can I achieve the same result on python?

Thanks

like image 525
Michael Avatar asked Oct 14 '25 23:10

Michael


2 Answers

If you are able to use scipy you can do this:

from scipy import stats
stats.norm.cdf(0)
0.5  

but it looks like what you want is actually the upper tail probability which in scipy.stats is referred to as a survival function of sf for short.

stats.norm.sf(0, 0.03, 0.055)
0.7072795327155363

should give you what you want. There are many continuous and discrete distributions within the scipy package. The docs are also available online to read:

https://docs.scipy.org/doc/scipy-0.16.1/reference/stats.html

other distribution functions are supported as are other common calculations on distributions, e.g. random sampling, mean, mass/density function, etc.

If you wanted you could directly calculate via:

>>> 1-stats.norm.cdf(0, 0.03, 0.055)
0.7072795327155363

For arguments near the bulk of the probability mass the two values will usually be the same but for very low probability events it is often the case that different numerical algorithms are used in the "tails" of the probability distributions so it can sometimes be beneficial to use the .sf() version.

like image 194
Lucas Roberts Avatar answered Oct 17 '25 13:10

Lucas Roberts


You can use scipy.stats.norm:

from scipy.stats import norm
print(norm.sf(x=0, loc=0.03, scale=0.055))
Out[10]: 0.7072795327155363
like image 31
Nicolas Gervais Avatar answered Oct 17 '25 13:10

Nicolas Gervais