수식이 깨질 경우 새로고침을 눌러주세요.
Estimation (추정) ①¶
$$ \{x_i\} \sim \text{Distribution}? $$랜덤 변수( X )의 샘플 집단으로부터 해당 랜덤 변수의 확률 분포에 대한 정보를 알아내는 일
Parameter Estimation(모수 추정)¶
1. Point Estimation (점 추정)¶
파라미터가 특정한 하나의 값이라고 가정하여 해당 값을 계산한다.
MM (Method of Moments) 적률 추정법
- sample의 모멘트가 실제 모멘트와 같다고 가정
- MM → 확장하면 GMM(General Method of Moments)
MLE (Maximization Likelihood Estimation) 최대 우도 추정법
우도 (Likelihood) 가 최대가 되는 파라미터 값을 찾음
2. Distribution Estimation(분포 추정)¶
파라미터에 대한 정보 분포를 계산
- MAP (Maximum A Posteriori) 최대 사후 확률 추정법
베이지안 사후 확률(Posterior)이 최대가 되는 파라미터의 분포를 구함
- 파라미터의 분포가 Beta, Gamma, Dirichlet 분포를 따를 때는 Parametric Estimation(모수적 방법)으로 설명
- 하이퍼 파라미터
- 그렇지 않을 경우는 Non-Parametric 방법으로 그림그려서 설명
참고 Bayes' Rule¶
ex_1)¶
Bernoulli 분포의 성공 확률 = ? $$\theta = ? $$
# -*- coding: utf-8 -*-
import seaborn as sns
import pandas as pd
import scipy as sp
import matplotlib as mpl
import matplotlib.pylab as plt
import numpy as np
%matplotlib inline
x = sp.stats.bernoulli(0.5).rvs(20)
x
x.sum() / len(x)
ex_2)¶
Normal 분포의 평균과 분산은 = ? $$\mu, \; \sigma^2 = ? $$
y = sp.stats.norm(0, 1).rvs(20)
y
mu = y.mean()
mu
s_2 = y.var(ddof = 0)
s_2
MM (Method of Moments) 적률 추정법¶
Point Estimation (점 추정)의 한 방법
Sample의 Moment가 실제 확률 변수의 이론적인 Moment가 같다고 가정하고 모수를 구하는 방법이다.
- N-th Moment $$ E[(x - \mu)^N] $$
Parameter(모수) 수 만큼 Moment 식이 필요하다.
- Parameter 수 = Moment 식
- ex)
베르누이 Parameter 1개 $$ \theta ← \bar{x}$$ 가우시안 정규 분포 Parameter 2개 $$\mu, \; \sigma^2\;\; ← \;\; \bar{x},\; s^2$$
1차 모멘트의 경우, 다음 식이 성립한다고 가정한다.¶
$$ 1st-Moment : \; E[(x-\mu)] $$$$ 이론적인\; 모형\; 평균\; \mu \;=\; \text{E}[X] \;=\; 샘플\; 평균\; \bar{x}$$$$ \bar{x} = \dfrac{1}{N} \sum_{i=1}^N x_i $$2차 모멘트(분산)의 경우¶
$$ 2nd-Moment : \; E[(x-\mu)^2] $$$$ 이론적인\; 모형\; 분산\; \sigma^2 \;=\; \text{E}[(X-\mu)^2] \;=\; 샘플\; 분산\; s^2 $$$$ s^2 = \dfrac{1}{N} \sum_{i=1}^N (x_i - \bar{x})^2 $$베르누이 분포의 모수 추정¶
$$ \text{E}[X] = \theta = \dfrac{1}{N} \sum_{i=1}^N x_i = \dfrac{N\cdot1}{N} = 1 $$$$ ∵)\;\; x = 0,\; x = 1$$MM(적률 추정법)으로 베르누이 분포의 모수를 구하면 다음과 같다.
정규 분포의 모수 추정¶
$$ \text{E}[X] = \mu = \dfrac{1}{N} \sum_{i=1}^N x_i = \bar{x} $$$$ \text{E}[(X-\mu)^2] = \sigma^2 = \dfrac{1}{N} \sum_{i=1}^N (x_i - \mu)^2 = s^2 $$MM(적률 추정법)으로 정규 분포의 모수를 구하면 다음과 같다.
Biased, Unbiased 인 경우에 따라 표본 분산의 자유도를 고르자.
- Biased → N
- Unbiased → N-1
베타 분포의 모수 추정¶
$$ \text{E}[X] = \dfrac{a}{a+b} = \bar{x} $$$$ \text{E}[(X-\mu)^2] = Var(X) = \dfrac{ab}{(a+b)^2(a+b+1)} = s^2 $$MM(적률 추정법)으로 베타 분포의 모수를 구하면 다음과 같다.
$$ a = \bar{x} \left( \frac{\bar{x} (1 - \bar{x})}{s^2} - 1 \right) $$$$ b = (1 - \bar{x}) \left( \frac{\bar{x} (1 - \bar{x})}{s^2} - 1 \right) $$이를 풀면 다음과 같다.
np.random.seed(0)
x = sp.stats.beta(15,12).rvs(1000)
def estimate_beta(x):
x_bar = x.mean()
s2 = x.var()
a = x_bar * (x_bar * (1 - x_bar) / s2 - 1)
b = (1 - x_bar) * (x_bar * (1 - x_bar) / s2 - 1)
return a, b
print(estimate_beta(x))
MLE(Maximum Likelihood Estimation)¶
Point Estimation (점 추정)의 한 방법
MLE(Maximum Likelihood Estimation)은 확률 모형의 모수 추정에서 가장 일반적으로 사용되는 방법이다.
Sample의 분포를 그렸을 때, Likelihood를 최대화하는 파라미터 값을 찾는 방법이다.
- Likelihood와 확률밀도함수의 차이를 알아보자.
- Likelihood와 로그 Likelihood에 대해서도 공부해 보자.
Likelihood¶
확률 밀도 함수를 랜덤변수( X )의 함수가 아닌 파라미터(theta)의 함수로 보는 것
확률 분포로부터 특정한 샘플 값( x = a )가 발생했을 때, 이 샘플 값( x = a )가 나오게 하는 파라미터의 가능성
확률 분포로부터 특정한 샘플 값( x = a )가 발생하였을 때, 샘플 값( x = a )와 파라미터의 확률(밀도함수)
다음 예를 보자.¶
확률 변수 X ~ 정규분포 (평규, 분산 = 1)
- 분산은 알고 평균을 모른다. 평균을 추정하자.
이때, x_0 = 1의 샘플을 하나 갖고 있다. x_0 =1 일 때, 어떤 평균값에서 가장 큰 확률(Likelihood)은 갖는가?
x = np.linspace(-5, 5, 100)
plt.figure(figsize=(12, 5))
plt.plot(x, sp.stats.norm(loc=0).pdf(x))
plt.hold(True)
# (1,0) 좌표에 검은색 점 찍어라.
plt.scatter(1, 0, s=100, c='k')
# (x, y_min, y_max) 점선으로 수직선을 그어라.
plt.vlines(1, -0.05, 0.45, linestyle=":")
plt.text(1-0.08, -0.06, "$x_0$")
plt.plot(x, sp.stats.norm(loc=-1).pdf(x))
plt.plot(x, sp.stats.norm(loc=1).pdf(x))
plt.scatter(1, sp.stats.norm(loc=-1).pdf(1), s=100, c='g');
plt.scatter(1, sp.stats.norm(loc=0).pdf(1), s=100, c='b');
plt.scatter(1, sp.stats.norm(loc=1).pdf(1), s=100, c='r');
plt.text(-3.3, 0.35, "$f_X(x;-1)$")
plt.text(-0.5, 0.44, "$f_X(x;0)$")
plt.text(1.9, 0.35, "$f_X(x;1)$");
평균 = 1일 때 가장 큰 값의 Likelihood를 갖는다.¶
- x=1일 때, 이 확률값(Likelihood) 중 최대가 되는 확률값이 MLE다.
- 여기서는 녹색의 분포일 때, 최대 확률이 MLE!!이며, 그 때의 theta를 다음과 같이 표현한다.
일반적으로는 추정을 위해 확보하고 있는 확률변수 표본의 수가 하나가 아니라 복수개이다.¶
likelihood도 복수 샘플값에 대한 결합확률밀도에서 구해야 한다. $$ (x_1, x_2, \cdots x_N) → f_{X_1, X_2, \cdots, X_N}(x_1, x_2, \cdots, x_N ; \theta) $$
확률 밀도 함수와 Likelihood의 차이점을 보자.¶
확률 밀도 함수¶
$$ 확률\; 밀도\; 함수\;\; P(x) = P(x; \theta) $$$$ \theta는\; 상수,\; x는\; 변수 $$
- 파라미터(theta)가 이미 정해져 있는 상황에서의 P(x)의 값
확률 밀도 함수에 대해 좀 더 알아 보자.¶
확률 변수 X에 대한 확률 모형은 확률 밀도 함수 f에 의해 정의된다. 확률 밀도 함수는 일반적으로 아래와 같이 표기한다. $$f_X(x;\theta)$$
- x : 확률 변수가 가질 수 있는 실수값
- theta : 확률 모형의 모수(parameter) 집합을 의미한다.
$$ f_X (x; \theta) = f_X (x; \mu, \sigma^2) = \dfrac{1}{\sqrt{2\pi\sigma^2}} \exp\left({-\frac{(x-\mu)^2}{2\sigma^2}}\right) \\ \\ 여기에서\; \theta \;=\; (\mu, \sigma^2)\; 이다. $$예를 들어 가우시안 확률 변수의 확률 밀도 함수는 다음과 같다.
함수의 관점에서 일반적으로 theta 고정된 값, 즉 상수 계수
x는 변수(variable)로 가정한다.
즉, 확률 변수 모형은 고정되어 있다.
Likelihood¶
$$ x는\; 상수,\; \theta는\; 변수 $$수식은 다음과 같다. $$ L(\theta) = p(x\;|\;\theta) $$
- x가 이미 정해져 있는 상황에서의 파라미터(theta) 값의 상대적 확률 $$ (x=a)\; 일\; 때,\; p(x\;|\;\theta)의\; 값 $$
likelihood에 대해 좀 더 알아 보자.¶
$$ \mathcal{L}(\theta;x) = f_X(x ; \theta) = f_X(x \;|\; \theta) $$모수 추정에서는 이미 실현된 샘플값(x)은 알고 있지만 모수를 모르고 있다.
- x : (x = a)와 같이 이미 결정되어진 상수 계수
- theta : 찾아야 할 확률 모형의 모수(parameter) 집합
- likelihood : 아래의 확률값 $$ f_X(x \;|\; \theta) $$
- 모수를 상수가 아닌 확률변수로 보는 경우에는 조건부 확률밀도함수로 쓸 수도 있다.