programing tip

파이썬에서 평균, 표준이 주어지면 정규 분포에서 확률 계산

itbloger 2020. 11. 4. 07:45
반응형

파이썬에서 평균, 표준이 주어지면 정규 분포에서 확률 계산


파이썬에서 평균, 표준이 주어진 정규 분포에서 확률을 계산하는 방법은 무엇입니까? 이 질문의 OP와 같은 정의에 따라 항상 내 자신의 함수를 명시 적으로 코딩 할 수 있습니다. Python의 분포에서 랜덤 변수의 확률 계산

라이브러리 함수 호출이 있는지 궁금하면이를 수행 할 수 있습니다. 내 상상으로는 다음과 같습니다.

nd = NormalDistribution(mu=100, std=12)
p = nd.prob(98)

Perl에도 비슷한 질문이 있습니다. Perl 의 정규 분포가 주어진 지점에서 확률을 어떻게 계산할 수 있습니까? . 하지만 파이썬에서는 보지 못했습니다.

Numpyrandom.normal기능을하지만, 내가 원하는 정확히 무엇을, 샘플링 같다.


scipy.stats에 하나가 있습니다 .

>>> import scipy.stats
>>> scipy.stats.norm(0, 1)
<scipy.stats.distributions.rv_frozen object at 0x928352c>
>>> scipy.stats.norm(0, 1).pdf(0)
0.3989422804014327
>>> scipy.stats.norm(0, 1).cdf(0)
0.5
>>> scipy.stats.norm(100, 12)
<scipy.stats.distributions.rv_frozen object at 0x928352c>
>>> scipy.stats.norm(100, 12).pdf(98)
0.032786643008494994
>>> scipy.stats.norm(100, 12).cdf(98)
0.43381616738909634
>>> scipy.stats.norm(100, 12).cdf(100)
0.5

[주의해야 할 한 가지-단지 팁-매개 변수 전달이 약간 광범위하다는 것입니다. 코드가 설정되는 방식 때문에 또는 scipy.stats.norm(mean=100, std=12)대신 실수로 작성하면 수락하지만 추가 키워드 인수를 자동으로 삭제하고 기본값 (0,1)을 제공합니다.]scipy.stats.norm(100, 12)scipy.stats.norm(loc=100, scale=12)


Scipy.stats는 훌륭한 모듈입니다. 다른 접근 방식을 제공하기 위해 다음을 사용하여 직접 계산할 수 있습니다.

import math
def normpdf(x, mean, sd):
    var = float(sd)**2
    denom = (2*math.pi*var)**.5
    num = math.exp(-(float(x)-float(mean))**2/(2*var))
    return num/denom

여기에있는 공식을 사용합니다. http://en.wikipedia.org/wiki/Normal_distribution#Probability_density_function

테스트하려면 :

>>> normpdf(7,5,5)  
0.07365402806066466
>>> norm(5,5).pdf(7)
0.073654028060664664

여기에 더 많은 정보가 있습니다. 먼저 고정 분포를 처리합니다 (이 경우 고정은 매개 변수가 특정 값으로 설정됨을 의미 함). 고정 배포를 생성하려면

import scipy.stats
scipy.stats.norm(loc=100, scale=12)
#where loc is the mean and scale is the std dev
#if you wish to pull out a random number from your distribution
scipy.stats.norm.rvs(loc=100, scale=12)

#To find the probability that the variable has a value LESS than or equal
#let's say 113, you'd use CDF cumulative Density Function
scipy.stats.norm.cdf(113,100,12)
Output: 0.86066975255037792
#or 86.07% probability

#To find the probability that the variable has a value GREATER than or
#equal to let's say 125, you'd use SF Survival Function 
scipy.stats.norm.sf(125,100,12)
Output: 0.018610425189886332
#or 1.86%

#To find the variate for which the probability is given, let's say the 
#value which needed to provide a 98% probability, you'd use the 
#PPF Percent Point Function
scipy.stats.norm.ppf(.98,100,12)
Output: 124.64498692758187

Starting Python 3.8, the standard library provides the NormalDist object as part of the statistics module.

It can be used to get the probability density function (pdf - likelihood that a random sample X will be near the given value x) for a given mean (mu) and standard deviation (sigma):

from statistics import NormalDist

NormalDist(mu=100, sigma=12).pdf(98)
# 0.032786643008494994

Also note that the NormalDist object also provides the cumulative distribution function (cdf - probability that a random sample X will be less than or equal to x):

NormalDist(mu=100, sigma=12).cdf(98)
# 0.43381616738909634

The formula cited from wikipedia mentioned in the answers cannot be used to calculate normal probabilites. You would have to write a numerical integration approximation function using that formula in order to calculate the probability.

That formula computes the value for the probability density function. Since the normal distribution is continuous, you have to compute an integral to get probabilities. The wikipedia site mentions the CDF, which does not have a closed form for the normal distribution.


I wrote this program to do the math for you. Just enter in the summary statistics. No need to provide an array:

One-Sample Z-Test for a Population Proportion:

To do this for mean rather than proportion, change the formula for z accordingly


In case you would like to find the area between 2 values of x mean = 1; standard deviation = 2; the probability of x between [0.5,2]

import scipy.stats
scipy.stats.norm(1, 2).cdf(2) - scipy.stats.norm(1,2).cdf(0.5)

You can just use the error function that's built in to the math library, as stated on their website.

참고URL : https://stackoverflow.com/questions/12412895/calculate-probability-in-normal-distribution-given-mean-std-in-python

반응형