Skip to content

Instantly share code, notes, and snippets.

@luhn
Created May 9, 2012 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luhn/2649874 to your computer and use it in GitHub Desktop.
Save luhn/2649874 to your computer and use it in GitHub Desktop.
CDF and PDF without SciPy
#Taken from http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html
def pdf(x):
"""Normal probably distribution function."""
return exp(-x**2/2)/sqrt(2*pi)
#Taken from http://stackoverflow.com/a/809402/600247
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 cdf(x):
"""Cumulative normal distribution function."""
return 1. - 0.5*erfcc(x/(2**0.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment