큰 수의 법칙 (Law of Large Numbers)

큰 수의 법칙은 동일한 조사를 여러 번 반복할 때, $\bar X$의 분포는 추출하는 표본의 갯수(n)에 의존한다는 것을 보여준다.

하지만, 실제 표본들의 평균($\bar X$)이 어떤 분포를 하는지에 대해서는 정보를 주지 않는다.그저 표본이 많으면 $\bar X$의 분산이 줄어들 것이라 것만 알려준다.

from scipy import stats
def getStdErr(samples, conf=0.95):
    n=len(samples)
    s=np.std(samples)
    return stats.t.ppf((1+conf)/2, n-1)*(s/np.sqrt(n-1))
def confidenceInterval(samples, conf=0.95):
    x= np.mean(samples)
    interval=getStdErr(samples)
               ##mean,     interval ,               interval length
    return round(x,5), [x-interval, x+interval], round(2*interval,5)
import pandas as pd
import numpy as np
dfraw = sns.load_dataset('penguins')
dfraw.dropna(axis=0,inplace=True)
dfraw.reset_index(drop=True, inplace=True)

samp1df=dfraw.sample(20, random_state=42)
samp1=samp1df['bill_depth_mm']
samp2df=dfraw.sample(200, random_state=42)
samp2=samp2df['bill_depth_mm']
len(samp1), len(samp2)
#  (20, 200)
confidenceInterval(samp1),confidenceInterval(samp2)
'''
((17.02, [16.040918665793555, 17.999081334206437], 1.95816),
 (17.2575, [16.987352342158953, 17.52764765784102], 0.5403))'''

신뢰 구간의 길이차이가 sample의 개수에 따라 많아질수록 짧아진다.

샘플수가 200개의 경우 20개보다 많아지다보니 더욱 모평균과 유사해지는 것이며, 동일한 confidence에 대해 더욱 예리해지는것과 같다고 볼 수 있다.

샘플의 수가 20개인 경우, 그것의 신뢰구간 길이는 1.95816이고, 이 경우, 1.95816길이의 구간 내에 모평균이 위치할 확률이 95%라는 의미.
200개인경우 0.5403인데, 200개샘플의 신뢰구간 길이가 더 작다.
이는 0.5403 길이의 구간 내에 모평균이 위치할 확률이 95%라는 의미

샘플 개수가 늘어남에따라 신뢰구간이 어떻게 변화하는지 그려보자

df7=pd.DataFrame({'sampleNumber':[i*10 for i in range(1,20)]})
df7['result']=df7['sampleNumber'].apply(lambda x: confidenceInterval(dfraw.sample(x, random_state=42)['bill_depth_mm']) )
df7['mean']=df7['result'].apply(lambda x: x[0])
df7['length']=df7['result'].apply(lambda x: x[2])
df7['st']=df7['result'].apply(lambda x: x[1][0])
df7['end']=df7['result'].apply(lambda x: x[1][1])
df7

plt.figure(figsize=(10,6))
plt.errorbar(x=df7['sampleNumber'], y=df7['mean'], yerr=df7['length'], fmt='o', capsize=5)
plt.xlabel('Sample Size',fontsize=20)
plt.ylabel('mean, interval',fontsize=20)
plt.show()

중심극한정리 (Central Limit Theorem)

The sample mean will be approximately normally distributed for large sample sizes, regardless of the distribution from which we are sampling.
샘플 데이터의 수가 많아질 수록(사이즈는 대략 30이상), 샘플의 평균은 정규분포에 근사한 형태로 나온다!

모집단에서 랜덤추출한 값의 평균을 반복해서 구하면 이 평균들은 정규분포를 따른다.

mean100list=[dfraw.sample(30)['bill_depth_mm'].mean() for _ in range(100)]
mean1000list=[dfraw.sample(30)['bill_depth_mm'].mean() for _ in range(1000)]
**plt.hist(x=mean100list)**

plt.hist(x=mean1000list)

동일하게 샘플사이즈 30으로 고정, 100번 추출한것과, 1000번 추출한것의 hist차이이다.

'Statistics' 카테고리의 다른 글

베이즈정리  (0) 2021.01.27
Covariance and Correlation 상관계수  (0) 2021.01.27
Chi-Square Test  (0) 2021.01.27
P-value란?  (0) 2021.01.27
T-Test, Chi-Square  (0) 2021.01.27

+ Recent posts