如何在不使用scipy的情况下计算python中的累积分布函数

Joh*_*han 3 python scipy

如何在不使用scipy的情况下计算python中正态分布的累积分布函数?

我特别指的是这个功能:

from scipy.stats import norm
norm.cdf(1.96)
Run Code Online (Sandbox Code Playgroud)

我有一个在Heroku上运行的Django应用程序,并且在Heroku上运行起来非常麻烦.因为我只需要这个来自scipy的函数,所以我希望我可以使用替代方法.我已经在使用numpy和pandas了,但我找不到那里的功能.我可以使用任何替代包,甚至自己实现它?

Jar*_*ber 7

只需使用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
Run Code Online (Sandbox Code Playgroud)

编辑以显示与scipy以下对比:

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

normal_cdf(1.96)
# 0.9750021048517796
Run Code Online (Sandbox Code Playgroud)