Statistics Test#

통계적 검정

모수적 방법#

Parametric Method

통계학의 중심극한정리에 따라 무작위로 복원 추출된 랜덤 샘플로 구성된 연속형 표본의 평균 분포는 정규분포를 따르게 됩니다. 이런 정규성을 가진다는 모수적 특성을 이용하여 비교하고자 하는 집단이 모두 정규 분포를 따르고 집단내의 분산이 같다면 집단간의 평균을 비교함으로써 차이를 밝히는 방법을 모수적 방법(Parametric Method)이라고 합니다. 일반적으로 각 집단의 랜덤 샘플 표본이 30개 이상이면 정규성을 따른다고 가정합니다. 대표적인 모수적 검정 방법으로는 F-Test, T-Test, ANOVA 등이 있습니다.

F-Test

F-Test 는 두 표본의 분산 차이를 통계적으로 검증하는 분석 방법입니다. 이는 등분산 검정 (Equal-variance Test)이라고도 합니다. F-Test 의 귀무가설은 ‘두 표본 간 분산의 차이가 없다’입니다. 따라서 F-Test 후 계산된 p-value 가 0.05 보다 작으면 (p < 0.05) 두 표본 간 분산의 차이가 있는 것으로 판단합니다.

T-Test

T-Test 는 두 표본의 평균 차이를 통계적으로 검증하는 분석 방법입니다. T-Test 의 귀무가설은 ‘두 표본 간 평균의 차이가 없다’입니다. 따라서 T-Test 후 계산된 p-value 가 0.05 보다 작으면 (p < 0.05) 두 표본 간 평균의 차이가 있는 것으로 판단합니다. 두 집단의 평균을 비교하는 분석방법에는 Z-Test, T-Test가 있지만, Z-Test는 표본의 크기가 30개 이상일 경우에 사용가능하기때문에, 이에 영향이 없는 T-Test를 일반적으로 사용합니다. 표본이 30개 이상일 경우는 Z-Test 와 T-Test의 검증 결과가 동일합니다.

ANOVA(분산분석)

ANOVA 는 세 표본 이상의 평균 차이를 통계적으로 검증하는 분석 방법입니다. ANOVA는 종속 변수의 수에 따라 One-way(1개), Two-way(2개), Multi-way(3개 이상) ANOVA로 나뉘어 집니다. ANOVA 의 귀무가설은 ‘표본 간 평균의 차이가 없다’입니다. 따라서 ANOVA 후 계산된 p-value 가 0.05 보다 작으면 (p < 0.05) 표본 간 평균의 차이가 있는 것으로 판단합니다.

import numpy as np
from scipy import stats

random_seed = 1
np.random.seed(random_seed)

# 샘플의 숫자만 다르고, 같은 평균과 분산을 가진 세 그룹을 만듭니다.
# norm: normal distribution
# A normal continuous random variable.
# The location (loc) keyword specifies the mean. The scale (scale) keyword specifies the standard deviation.
# rvs: random value sampling
np_data1 = stats.norm(loc=1, scale=1).rvs(size=50, random_state=random_seed)
np_data2 = stats.norm(loc=1, scale=1).rvs(size=100, random_state=random_seed)
np_data3 = stats.norm(loc=1, scale=1).rvs(size=200, random_state=random_seed)

# 독립 표본 t-검정: Independent Two Sample's T-Test
# Calculate the T-test for the means of two independent samples of scores.
# This is a two-sided test for the null hypothesis 
# that 2 independent samples have identical average (expected) values. 
# This test assumes that the populations have identical variances by default.

# 두 표본은 같은 평균을 가지기 때문에 p < 0.05 보다 낮게 나옵니다.
statistic, p = stats.ttest_ind(np_data1, np_data2)
print("Independent Two Sample's T-Test: statistic={:.5f}, p={:.2f}".format(statistic, p))

# 독립 표본 f-검정: Independent Two Sample's F-Test
# f-검정은 두 표본의 분산을 비교한 값이 F 분포상에 위치하는 확률 값을 나타내기 때문에 아래와 같이 구할 수 있지만, 이상치에 민감합니다.

# 두 표본은 같은 분산을 가지기 때문에 p < 0.05 보다 낮게 나옵니다.
statistic = np.var(np_data1) / np.var(np_data2)
df1 = len(np_data1) - 1
df2 = len(np_data2) - 1
p = stats.f.cdf(statistic, df1, df2)
print("Independent Two Sample's F-Test: statistic={:.5f}, p={:.2f}".format(statistic, p))

# levene 방식은 이를 보완한 함수 입니다.
# Perform Levene test for equal variances.
# The Levene test tests the null hypothesis that all input samples are from populations with equal variances.
statistic, p = stats.levene(np_data1, np_data2)
print("Independent Two Sample's F-Test(Levene): statistic={:.5f}, p={:.2f}".format(statistic, p))


# Oneway ANOVA:
# Performs a 1-way ANOVA.
# The one-way ANOVA tests the null hypothesis that two or more groups have the same population mean. 
# The test is applied to samples from two or more groups, possibly with differing sizes.
# The ANOVA test has important assumptions that must be satisfied in order for the associated p-value to be valid.
#
#  1.The samples are independent.
#  2. Each sample is from a normally distributed population.
#  3. The population standard deviations of the groups are all equal. This property is known as homoscedasticity.

# 세 표본은 같은 평균을 가지기 때문에 p < 0.05 보다 낮게 나옵니다.
statistic, p = stats.f_oneway(np_data1, np_data2, np_data3)
print("Oneway ANOVA: statistic={:.5f}, p={:.2f}".format(statistic, p))
Independent Two Sample's T-Test: statistic=-0.54012, p=0.59
Independent Two Sample's F-Test: statistic=1.19987, p=0.78
Independent Two Sample's F-Test(Levene): statistic=0.59023, p=0.44
Oneway ANOVA: statistic=0.43331, p=0.65