问题:如何计算累积正态分布?
我正在寻找Numpy或Scipy(或任何严格的Python库)中的函数,该函数将为我提供Python中的累积正态分布函数。
回答 0
这是一个例子:
>>> from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435换句话说,大约95%的标准法线间隔位于两个标准偏差之内,以标准平均值零为中心。
如果需要逆CDF:
>>> norm.ppf(norm.cdf(1.96))
array(1.9599999999999991)回答 1
回答这个问题可能为时已晚,但是由于Google仍然领导这里的人们,因此我决定在此处编写解决方案。
也就是说,自Python 2.7起,该math库集成了error函数math.erf(x)
该erf()函数可用于计算传统的统计函数,例如累积标准正态分布:
from math import *
def phi(x):
    #'Cumulative distribution function for the standard normal distribution'
    return (1.0 + erf(x / sqrt(2.0))) / 2.0参考:
https://docs.python.org/2/library/math.html
回答 2
从这里改编http://mail.python.org/pipermail/python-list/2000-June/039873.html
from math import *
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 ncdf(x):
    return 1. - 0.5*erfcc(x/(2**0.5))回答 3
以Unknown的示例为基础,在许多库中实现的功能normdist()的Python等效项为:
def normcdf(x, mu, sigma):
    t = x-mu;
    y = 0.5*erfcc(-t/(sigma*sqrt(2.0)));
    if y>1.0:
        y = 1.0;
    return y
def normpdf(x, mu, sigma):
    u = (x-mu)/abs(sigma)
    y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)
    return y
def normdist(x, mu, sigma, f):
    if f:
        y = normcdf(x,mu,sigma)
    else:
        y = normpdf(x,mu,sigma)
    return y回答 4
从开始Python 3.8,标准库将NormalDist对象作为statistics模块的一部分提供。
对于给定的均值()和标准差(),它可用于获取累积分布函数(cdf-随机样本X小于或等于x的概率):musigma
from statistics import NormalDist
NormalDist(mu=0, sigma=1).cdf(1.96)
# 0.9750021048517796对于标准正态分布(mu = 0和sigma = 1)可以简化:
NormalDist().cdf(1.96)
# 0.9750021048517796
NormalDist().cdf(-1.96)
# 0.024997895148220428回答 5
Alex的答案为您显示了标准正态分布的解决方案(均值= 0,标准差= 1)。如果您使用mean和进行正态分布std(是sqr(var)),并且要计算:
from scipy.stats import norm
# cdf(x < val)
print norm.cdf(val, m, s)
# cdf(x > val)
print 1 - norm.cdf(val, m, s)
# cdf(v1 < x < v2)
print norm.cdf(v2, m, s) - norm.cdf(v1, m, s)回答 6
从上方拍摄:
from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435对于两尾测试:
Import numpy as np
z = 1.96
p_value = 2 * norm.cdf(-np.abs(z))
0.04999579029644087回答 7
像这样简单:
import math
def my_cdf(x):
    return 0.5*(1+math.erf(x/math.sqrt(2)))我在此页面中找到了公式https://www.danielsoper.com/statcalc/formulas.aspx?id=55
回答 8
当Google针对搜索netlogo pdf提供此答案时,这是上述python代码的netlogo版本
    ;; 正态分布累积密度函数
    报告normcdf [x mu sigma]
        让TX-亩
        让y 0.5 * erfcc [-t /(sigma * sqrt 2.0)]
        如果(y> 1.0)[设置y 1.0]
        报告y
    结束
    ;; 正态分布概率密度函数
    报告normpdf [x mu sigma]
        设u =(x-mu)/ abs sigma
        令y = 1 /(sqrt [2 * pi] * abs sigma)* exp(-u * u / 2.0)
        报告y
    结束
    ;; 互补误差函数
    报告erfcc [x]
        让z abs x
        令t 1.0 /(1.0 + 0.5 * z)
        令rt * exp(-z * z -1.26551223 + t *(1.00002368 + t *(0.37409196 +
            t *(0.09678418 + t *(-0.18628806 + t *(.27886807 +
            t *(-1.13520398 + t *(1.48851587 + t *(-0.82215223 +
            t * .17087277)))))))))
        ifelse(x> = 0)[报告r] [报告2.0-r]
    结束

