Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the cumulative distribution function in python without using scipy

Tags:

python

scipy

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?

like image 798
Kritz Avatar asked Apr 02 '18 18:04

Kritz


People also ask

How do you calculate CDF from data?

Given a random variable X, its cdf is the function F(x) = Prob(X <= x) where the variable x runs through the real numbers. The distribution is called continuous if F(x) is the integral from -infinity to x of a function f called the density function.

What is CDF in pandas?

A CDF or cumulative distribution function plot is basically a graph with on the X-axis the sorted values and on the Y-axis the cumulative distribution.


2 Answers

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
like image 186
Jared Wilber Avatar answered Oct 27 '22 06:10

Jared Wilber


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
like image 36
Adrian Tompkins Avatar answered Oct 27 '22 07:10

Adrian Tompkins