누적 정규 분포를 계산하는 방법은 무엇입니까?
저는 Numpy 또는 Scipy(또는 엄격한 Python 라이브러리)에서 Python의 누적 정규 분포 함수를 제공할 함수를 찾고 있습니다.
다음은 예입니다.
>>> from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435
즉, 표준 정규 구간의 약 95%가 표준 평균 0을 중심으로 두 표준 편차 내에 있습니다.
역CDF가 필요한 경우:
>>> norm.ppf(norm.cdf(1.96))
array(1.9599999999999991)
질문에 답하기에는 너무 늦었을 수도 있지만, 구글이 여전히 사람들을 이끌고 있기 때문에, 저는 여기에 제 해결책을 쓰기로 결정했습니다.
즉, Python 2.7 이후로math라이브러리가 오류 함수를 통합했습니다.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
https://docs.python.org/3/library/math.html
오류 함수와 표준 정규 분포 함수는 어떤 관계가 있습니까?
시작하는Python 3.8표준 라이브러리는 객체를 모듈의 일부로 제공합니다.
주어진 평균에 대한 누적 분포 cdf함수(- 랜덤 표본 X가 x보다 작거나 같을 확률)를 얻는 데 사용할 수 있습니다.mu) 및 표준 편차(sigma):
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
여기서 적용됨 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))
Unknown의 예를 바탕으로 많은 라이브러리에서 구현된 함수 normdist()에 해당하는 파이썬은 다음과 같습니다.
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
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)
여기에서 cdf에 대한 자세한 내용과 많은 공식을 사용한 정규 분포 구현을 읽어 보십시오.
위에서 찍은 사진:
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
다음과 같이 단순합니다.
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
언급URL : https://stackoverflow.com/questions/809362/how-to-calculate-cumulative-normal-distribution
'programing' 카테고리의 다른 글
| Amazon S3 boto - 폴더를 삭제하는 방법? (0) | 2023.08.12 |
|---|---|
| CSS: 창 너비의 50%에 해당하는 배경색을 설정합니다. (0) | 2023.08.12 |
| Chocolaty 설치 경로는 어디입니까? (0) | 2023.08.12 |
| setCaseSensitiveInfo를 모든 폴더 및 하위 폴더에 재귀적으로 적용 (0) | 2023.08.12 |
| Oracle PL/SQL For Loop을 사용하여 쉼표로 구분된 문자열을 반복합니다. (0) | 2023.08.12 |