Notice
Recent Posts
Recent Comments
05-03 21:44
Archives
관리 메뉴

develop myself

pdf, cdf, ppf (SciPy의 method 활용) 본문

DataScience/통계분석

pdf, cdf, ppf (SciPy의 method 활용)

insightous 2023. 1. 27. 14:44

용어

- pdf: Probability density function

- cdf: Cumulative distribution function.

- ppf: Percent point function (inverse of cdf: cdf와 역함수 관계)

pdf: 확률밀도함수

확률변수(X)의 분포를 나타내는 함수.

즉, 함수 값(그래프의 y축)은 분포를 나타낸다.

 

- 일반 확률밀도함수

정규분포 함수

 

- 표준 확률밀도 함수

위 함수에서 $\mu$ = 0, $\sigma$=1 인 경우, 즉 $N(0,1)$

표준정규분포 함수

 

연속 확률 분포이므로 특정 값에서는 확률을 구할 수 없다.

특정 구간에서의 확률을 구할 수 있고, 확률은 구간에서의 적분값이 된다.

$P(a \leq X \leq b) = \int_a^b f(x) dx$

모든 구간에서의 적분값은 1 이 된다.

$\int_{-\infty}^{\infty} f(x) dx = 1$

 

=> 그렇기 때문에 cdf가 필요하게 된다

scipy.stats.norm.pdf

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

z_values = np.linspace(-4, 4, 1000)
pdf_values = norm.pdf(z_values)

fig, axes = plt.subplots()

axes.plot(z_values, pdf_values)

plt.show()

scipy.stats.t.pdf

t 분포는 자유도에 따라 그래프가 달라진다.

아래 예시는 자유도 20에서의 pdf 이다

from scipy.stats import t

t_dist = t(20)
t_values = np.linspace(-4, 4, 1000)
pdf_values = t_dist.pdf(t_values)

fig, axes = plt.subplots()

axes.plot(t_values, pdf_values)
axes.set_xlabel('t value')
axes.set_ylabel('probability for t value')

fig.suptitle('PDF for t distribution with df=20')

plt.show()

cdf: 누적분포함수

$F_X(x) = P(X \leq x) $ 

즉, 함수값(그래프의 y값)은 확률을 나타낸다.

$ \displaystyle \lim_{x \to -\infty} F(x) = 0 \quad and  \quad \lim_{x \to \infty} F(x) = 1 $

 

위의 pdf를 $-\infty$에서 부터 X까지 적분한 값을 함수로 나타낸 것이다.

 

scipy.stats.norm.cdf

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

z_values = np.linspace(-4, 4, 1000)
pdf_values = norm.cdf(z_values)

fig, axes = plt.subplots()

axes.plot(z_values, pdf_values)

plt.show()

scipy.stats.t.cdf

자유도 = 20 일 때

from scipy.stats import t

t_dist = t(20)
t_values = np.linspace(-4, 4, 1000)
pdf_values = t_dist.cdf(t_values)

fig, axes = plt.subplots()

axes.plot(t_values, pdf_values)

fig.suptitle('CDF for t distribution with df=20')

plt.show()

ppf: Percent point function

Percent point function(inverse of cdf — percentiles)

ppf와 cdf는 역함수 관계이다. 

그래프에서 y축은 Z 값(정규 분포), t값(t분포)를 나타낸다

$Z_{0.1}$, $Z_{0.05}$, $Z_{0.01}$, $t_{0.005}(20)$ 등을 구할 때 유용하다. 

scipy.stats.norm.ppf

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

z_values = np.linspace(-1, 2, 1000)
pdf_values = norm.ppf(z_values)

fig, axes = plt.subplots()

axes.plot(z_values, pdf_values)

plt.show()

 

scipy.stats.t.ppf

from scipy.stats import t

t_dist = t(20)
t_values = np.linspace(-1, 2, 1000)
pdf_values = t_dist.ppf(t_values)

fig, axes = plt.subplots()

axes.plot(t_values, pdf_values)

fig.suptitle('PPF for t distribution with df=20')

plt.show()

pdf와 cdf 관계

# Show relationship of PDF and CDF for three example t values.
example_values = (-1.5, 0, 1.5)
pdf_values = t_dist.pdf(t_values)
cdf_values = t_dist.cdf(t_values)
fill_color = (0, 0, 0, 0.1)  # Light gray in RGBA format.
line_color = (0, 0, 0, 0.5)  # Medium gray in RGBA format.
fig, axes = plt.subplots(2, len(example_values), figsize=(10, 6))
for i, x in enumerate(example_values):
    cdf_ax, pdf_ax = axes[:, i]
    cdf_ax.plot(t_values, cdf_values)
    pdf_ax.plot(t_values, pdf_values)
    # Fill area at and to the left of x.
    pdf_ax.fill_between(t_values, pdf_values,
                        where=t_values <= x,
                        color=fill_color)
    pd = t_dist.pdf(x)  # Probability density at this value.
    # Line showing position of x on x-axis of PDF plot.
    pdf_ax.plot([x, x],
                [0, pd], color=line_color)
    cd = t_dist.cdf(x)  # Cumulative distribution value for this x.
    # Lines showing x and CDF value on CDF plot.
    x_ax_min = cdf_ax.axis()[0]  # x position of y axis on plot.
    cdf_ax.plot([x, x, x_ax_min],
                [0, cd, cd], color=line_color)
    cdf_ax.set_title('x = {:.1f}, area = {:.2f}'.format(x, cd))
    # Hide top and right axis lines and ticks to reduce clutter.
    for ax in (cdf_ax, pdf_ax):
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.yaxis.set_ticks_position('left')
        ax.xaxis.set_ticks_position('bottom')

 

출처: https://matthew-brett.github.io/teaching/on_cdfs.html


참고

pdf

- https://en.wikipedia.org/wiki/Normal_distribution

- https://ko.wikipedia.org/wiki/%ED%99%95%EB%A5%A0_%EB%B0%80%EB%8F%84_%ED%95%A8%EC%88%98

- https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy-stats-norm

- https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.t.html#scipy-stats-t

 

cdf

- https://en.wikipedia.org/wiki/Cumulative_distribution_function

https://ko.wikipedia.org/wiki/%EB%88%84%EC%A0%81_%EB%B6%84%ED%8F%AC_%ED%95%A8%EC%88%98#:~:text=%ED%99%95%EB%A5%A0%EB%A1%A0%EC%97%90%EC%84%9C%20%EB%88%84%EC%A0%81%EB%B6%84%ED%8F%AC%ED%95%A8%EC%88%98,%ED%99%95%EB%A5%A0%EC%9D%84%20%EB%82%98%ED%83%80%EB%82%B4%EB%8A%94%20%ED%95%A8%EC%88%98%EC%9D%B4%EB%8B%A4.

 

pdf와 cdf 관계

- https://matthew-brett.github.io/teaching/on_cdfs.html

 

 

 

Comments