5

How can I calculate the cumulative distribution function of a normal distribution in python without using scipy?

I'm specifically referring to this function:

from scipy.stats import norm
norm.cdf(1.96)

I have a Django app running on Heroku and getting scipy up and running on Heroku is quite a pain. Since I only need this one function from scipy, I'm hoping I can use an alternative. I'm already using numpy and pandas, but I can't find the function in there. Are there any alternative packages I can use or even implement it myself?

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Kritz
  • 7,099
  • 12
  • 43
  • 73
  • 2
    See http://mathworld.wolfram.com/NormalDistributionFunction.html for approximations. – Bill Bell Apr 02 '18 at 18:46
  • Possible duplicate of [How to calculate cumulative normal distribution in Python](https://stackoverflow.com/questions/809362/how-to-calculate-cumulative-normal-distribution-in-python) – ClimateUnboxed Aug 15 '19 at 10:10

2 Answers2

9

Just use math.erf:

import math

def normal_cdf(x):
    "cdf for standard normal"
    q = math.erf(x / math.sqrt(2.0))
    return (1.0 + q) / 2.0

Edit to show comparison with scipy:

scipy.stats.norm.cdf(1.96)
# 0.9750021048517795

normal_cdf(1.96)
# 0.9750021048517796
Jared Wilber
  • 6,038
  • 1
  • 32
  • 35
3

This question seems to be a duplicate of How to calculate cumulative normal distribution in Python where there are many alternatives to scipy listed.

I wanted to highlight the answer of Xavier Guihot https://stackoverflow.com/users/9297144/xavier-guihot which shows that from python3.8 the normal is now a built in:

from statistics import NormalDist

NormalDist(mu=0, sigma=1).cdf(1.96)
# 0.9750021048517796
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86