전체 글

728x90
반응형

6. 점심시간에 식당가 부근에 있는 공영 주차장에 주차된 승용차의 평균 주차 시간이 1시간을 초과하는지 알아보기 위하여 어느 점심시간에 주차 시간을 조사한 결과 다음과 같았다. 물음에 답하라.

 

H_0 : 주차시간 <= 60(분) ==> 상단측 검정

 

 

1> 평균 주차시간이 1시간을 초과하는지 유의수준 5%에서 조사하라.

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))


#
A = "53 47 68 62 65 65 68 65 64 56 68 76 55 63 56 62 69 60 62 60"
A = list(map(float, A.split(' ')))
print(A)

MEANS = round(np.mean(A) , 4)
STDS = round(np.std(A , ddof =1 ) ,4)
MO_MEAN = 60


n = len(A) #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4) #오차한계


ax.set_title('상단측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{%d} = t_{{\alpha}}*\dfrac{s}{\sqrt{n}}$' % ((1-  trust)*100 ) +f'= {E}' ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}>T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = ($-\infty, MEANS + e_{\alpha}$)' +f'\n' + r' = $(-\infty, , {%.4f} + {%.4f})$' % ( MEANS , E)  +f'\n' +r'$ = (-\infty, , {%.4f})$' % ( MEANS+E)  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(2.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_r + '\n' +r'${\alpha}$ =' +f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
# ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(1- float(scipy.stats.t(dof_2).cdf(t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X>=t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5, annotate_len+0.03 , f'P-value : \nP(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 상단측 검정

H_0 : m<= 60 (상단측 검정)

 

p-value : 0.0752

 

alpha = 0.05

 

p-value > alpha ==> 0.0752 > 0.05 ==> H_0: m<= 60 채택 ==> 유의수준 5%에서 평균 주차시간이 1시간 이하이다.

 

 

7. 자동차 베터리를 판매하는 한 판매상이 자신이 판매하는 배터리의 평균 수명은 36개월을 초과한다고 말한다. 임의로 배터리 10개를 측정하여 평균 37.8개월, 표준편차 4.3개월을 얻었다. 이 자료를 근거로 배터리의 평균 수명이 36개월을 초과하는지 유의수준 5%에서 조사하라.

 

|X = 37.8

s = 4.3

n = 10

 

H_0 : m <= 36 (상단측 검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))


#
# A = "53 47 68 62 65 65 68 65 64 56 68 76 55 63 56 62 69 60 62 60"
# A = list(map(float, A.split(' ')))
# print(A)

MEANS = 37.8
STDS = 4.3
MO_MEAN = 36


n = 10 #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4) #오차한계


ax.set_title('상단측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{%d} = t_{{\alpha}}*\dfrac{s}{\sqrt{n}}$' % ((1-  trust)*100 ) +f'= {E}' ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}>T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = ($-\infty, MEANS + e_{\alpha}$)' +f'\n' + r' = $(-\infty, , {%.4f} + {%.4f})$' % ( MEANS , E)  +f'\n' +r'$ = (-\infty, , {%.4f})$' % ( MEANS+E)  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(2.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_r + '\n' +r'${\alpha}$ =' +f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
# ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(1- float(scipy.stats.t(dof_2).cdf(t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X>=t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5, annotate_len+0.03 , f'P-value : \nP(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 상단측 검정

 

 

H_0 : m<= 36 (상단측 검정)

 

p-value : 0.1091

 

alpha = 0.05

 

p-value > alpha ==> 0.1091 > 0.05 ==> H_0: m<= 36 채택 ==> 유의수준 5%에서 평균 수명이 36개월을 초과하지 않는다.

 

 

 

 

728x90
반응형
728x90
반응형

1. 어느 회사에서 제조되는 1.5V 소형 건전지의 평균 수명을 알아보기 위하여 15개를 임의로 조사한 결과, 평균 71.5시간 , 표준편차 3.8시간으로 측정되었다. 이 회사에서 제조되는 소형 건전지의 평균 수명에 대한 95% 신뢰구간을 구하라.

 

|X = 71.5

s = 3.8

n = 15

 

==> 모분산 모른다 ==> t-분포 활용

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))




# A = [3.1 , 1.9 , 2.4 , 2.8 , 2.9 , 3.0 , 2.8 , 2.3, 2.2 , 2.6]

MEANS = 71.5
STDS = 3.8




n = 15 #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4)



# =========================================================
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=t_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .27, f'평균(MEANS) = {MEANS}\n alpha = {round(1-area,4)}\n' + r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r +f'\n n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{95\% } = t_{\dfrac{\alpha}{2}}*\dfrac{s}{\sqrt{n}}$'+f'= {E}',fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')

plt.annotate('' , xy=(3.0, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-3.0, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))

ax.text(1.71 , .13, r'$P(T>t_{%.3f})$' % trust + f'= {round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$P(T<t_{%.3f})$' % trust + f'= {round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)





b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 신뢰구간

신뢰구간 : (69.3956 , 73.6044)

 

2. 어느 회사에서는 직원들의 후생 복지를 지원하기 위하여 먼저 직원들이 여가 시간에 자기 계발을 위하여 하루 동안 투자하는 시간을 조사하였고, 그 결과는 다음과 같았다. 물음에 답하라.

A = "40 30 70 60 50 60 60 30 40 50 90 60 50 30 30"
A = list(map(int , A.split(' ')))

A = [40, 30, 70, 60, 50, 60, 60, 30, 40, 50, 90, 60, 50, 30, 30]

 

1> 전 직원이 자기 계발을 위하여 투자하는 평균 시간에 대한 95% 신뢰구간을 구하라.

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))




A = "40 30 70 60 50 60 60 30 40 50 90 60 50 30 30"
A = list(map(int , A.split(' ')))
print(A)
MEANS = np.mean(A)
STDS = np.std(A , ddof=1)




n = len(A) #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4)



# =========================================================
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=t_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .27, f'평균(MEANS) = {MEANS}\n alpha = {round(1-area,4)}\n' + r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r +f'\n n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{95\% } = t_{\dfrac{\alpha}{2}}*\dfrac{s}{\sqrt{n}}$'+f'= {E}',fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')

plt.annotate('' , xy=(3.0, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-3.0, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))

ax.text(1.71 , .13, r'$P(T>t_{%.3f})$' % trust + f'= {round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$P(T<t_{%.3f})$' % trust + f'= {round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)





b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 신뢰구간

 

2> 직원들의 자기 계발을 위한 평균 투자 시간이 1시간에 미달하는지 유의수준 5%에서 조사하라.

 

H_0 : m>= 1 (하단측 검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))


#
# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


A = "40 30 70 60 50 60 60 30 40 50 90 60 50 30 30"
A = list(map(int , A.split(' ')))
print(A)
MEANS = np.mean(A)
STDS = np.std(A , ddof=1)




n = len(A) #표본개수
dof_2 = [n-1] #자유도c

MO_MEAN = 60

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4) #오차한계


ax.set_title('하단측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=-t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(1- scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{%d} = t_{{\alpha}}*\dfrac{s}{\sqrt{n}}$' % ((1-  trust)*100 ) +f'= {E}' ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha} , \infty$)' +f'\n' + r' = $({%.4f} - {%.4f} , \infty)$' % (MEANS, E)  +f'\n' +r'$ = ({%.4f} , \infty)$' % (MEANS-E)  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_l + '\n' +r'${\alpha}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

# ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

# ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
# plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(0.7, annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 하단측검정

 

H_0 : 평균시간 >= 60(하단측 검정)

 

p-value : 0.0211

 

alpha = 0.05

 

p-value < alpha ==> 0.0211 < 0.05 ==> H_0: 평균시간 >= 1 기각 ==> 평균시간은 1시간에 미달한다.

 

 

3. 건강에 관심이 많은 어느 사회단체는 건강한 성인이 하루에 소비하는 물의 양은 2L 이상이라고 하였다. 이것을 확인하기 위하여 12명의 건강한 성인을 임의로 선정하여 하루에 소비하는 물의 양을 다음과 같이 조사하였다. 건강한 성인이 하루에 평균 2L 이상 소비하는 지 유의수준 1%에서 조사하라.

 

H_0 : m>= 2 (하단측 검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))


#
# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


A = "2.1 2.2 1.5 1.7 2.0 1.6 1.7 1.5 2.4 1.6 2.5 1.9"
A = list(map(float , A.split(' ')))
print(A)
MEANS = np.mean(A)
STDS = np.std(A , ddof=1)




n = len(A) #표본개수
dof_2 = [n-1] #자유도c

MO_MEAN = 2

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 99 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4) #오차한계


ax.set_title('하단측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=-t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(1- scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{%d} = t_{{\alpha}}*\dfrac{s}{\sqrt{n}}$' % ((1-  trust)*100 ) +f'= {E}' ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha} , \infty}$)' +f'\n' + r' = $({%.4f} - {%.4f}, \infty)$' % (MEANS, E )  +f'\n' +r'$ = ({%.4f} , \infty)$' % (MEANS-E )  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_l + '\n' +r'${\alpha}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

# ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

# ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
# plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(0.7, annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 하단측검정

H_0 : m>= 2 (하단측 검정)

 

p-value : 0.1517

 

alpha = 0.01

 

p-value > alpha ==> 0.1517 > 0.01 ==> H_0: m>= 2 채택 ==> 유의수준 1%에서 건강한 성인이 하루에 평균 2L이상 소비한다.

 

 

4. 정규모집단의 모평균을 알아보기 위하여 크기 10인 표본을 조사하여 |x = 24.04 , s = 1.2를 얻었다.

 

n = 10

|x = 24.04

s = 1.2

 

==> 모분산 모른다! ==> t-분포 활용

 

1> 신뢰도 95%인 모평균에 대한 신뢰구간

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))



# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


MEANS = 24.04
STDS = 1.2
MO_MEAN = 25



n = 10 #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4)



# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{95\% } = t_{\dfrac{\alpha}{2}}*\dfrac{s}{\sqrt{n}}$'+f'= {E}',fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_l + '\n' +r'$\dfrac{\alpha}{2}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) + 1 - (scipy.stats.t(dof_2).cdf(t_1))),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_1) | (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=-t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5 , annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) + P(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 신뢰구간

신뢰구간 : (23.1816 , 24.8984)

 

2> 유의수준 alpha = 0.01에서 H_0 : m = 25를 조사하라.(양측검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))



# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


MEANS = 24.04
STDS = 1.2
MO_MEAN = 25



n = 10 #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 99 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4)



# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{95\% } = t_{\dfrac{\alpha}{2}}*\dfrac{s}{\sqrt{n}}$'+f'= {E}',fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_l + '\n' +r'$\dfrac{\alpha}{2}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) + 1 - (scipy.stats.t(dof_2).cdf(t_1))),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_1) | (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=-t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5 , annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) + P(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 양측검정

H_0 : m= 25 (양측 검정)

 

p-value : 0.0322

 

alpha = 0.01

 

p-value > alpha ==> 0.0322 > 0.01 ==> H_0: m= 25 채택 ==> 유의수준 1%에서 m = 25

 

3> 유의수준 alpha = 0.05에서 H_0 : m = 25를 조사하라.(양측검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))



# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


MEANS = 24.04
STDS = 1.2
MO_MEAN = 25



n = 10 #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4)



# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{95\% } = t_{\dfrac{\alpha}{2}}*\dfrac{s}{\sqrt{n}}$'+f'= {E}',fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_l + '\n' +r'$\dfrac{\alpha}{2}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) + 1 - (scipy.stats.t(dof_2).cdf(t_1))),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_1) | (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=-t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5 , annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) + P(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 양측검정

H_0 : m= 25 (양측 검정)

 

p-value : 0.0322

 

alpha = 0.05

 

p-value > alpha ==> 0.0322 < 0.05 ==> H_0: m= 25 기각 ==> 유의수준 1%에서 m = 25의 주장은 기각한다.

 

 

5. 어느 공업 지역 부근을 흐르는 하천 물의 평균 pH농도가 7이라고 한다. 이것을 알아보기 위하여 임의로 21곳의 물을 선정하여 조사한 결과, 평균 7.2 , 표준편차 0.32였다. 이 하천의 pH농도는 정규분포를 따른다고 할 때, 평균 pH농도가 7인지 유의수준 5%에서 조사하라.

 

 

|X = 7.2

s = 0.32

 

H_0 : m = 7 (양측검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))



# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


MEANS = 7.2
STDS = 0.32
MO_MEAN = 7



n = 21 #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4)


ax.set_title('양측 검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{95\% } = t_{\dfrac{\alpha}{2}}*\dfrac{s}{\sqrt{n}}$'+f'= {E}',fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_l + '\n' +r'$\dfrac{\alpha}{2}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) + 1 - (scipy.stats.t(dof_2).cdf(t_1))),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_1) | (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=-t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5 , annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) + P(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

t-분포 양측검정

H_0 : m= 7 (양측 검정)

 

p-value : 0.0096

 

alpha = 0.05

 

p-value < alpha ==> 0.0096 < 0.05 ==> H_0: m= 7 기각 ==> 유의수준 5%에서 pH농도가  7이라는  주장은 기각한다.

 

 

728x90
반응형
728x90
반응형

EX-01) 두 지역에서 각각 10가구씩 표본추출하여 소비 지출을 조사한 결과가 다음과 같다. 두 지역의 소비지출의 분산이 동일한지 유의수준 5%에서 조사하라.

 

A = '72 75 75 80 100 110 125 150 160 200'
B = '50 60 72 90 100 125 125 130 132 170'

A = list(map(int , A.split()))
B = list(map(int ,B.split()))

A  : [72, 75, 75, 80, 100, 110, 125, 150, 160, 200]
B : [50, 60, 72, 90, 100, 125, 125, 130, 132, 170]

 

 

H_0 : 모분산_A = 모분산_B (양단측 검정)

 

X = np.arange(0,10, .01)

fig = plt.figure(figsize=(20,12))


A = '72 75 75 80 100 110 125 150 160 200'
B = '50 60 72 90 100 125 125 130 132 170'

A = list(map(int , A.split()))
B = list(map(int ,B.split()))

# Vars = np.var(A , ddof=1)
# Vars = 0.2**2
n = len(A)
m = len(B)
dof = [[n-1 , m-1]] #자유도

trust = 95
trust = round((1- trust/100)/2,3)



sample_x = np.mean(A)
stand_x = np.std(A , ddof=1)
print(f'|x : {sample_x}')
print(f's_x : {stand_x}')
sample_y = np.mean(B)
stand_y = np.std(B , ddof=1)
print(f'|y : {sample_y}')
print(f's_y : {stand_y}')
#
# STDS = math.sqrt(Vars)
# MO_std = 0.3


for i in dof:
    ax = sns.lineplot(X , scipy.stats.f(i[0] , i[1]).pdf(X))




X_r = scipy.stats.f(dof[0][0], dof[0][1]).ppf(1-trust)

X_l = scipy.stats.f(dof[0][0], dof[0][1]).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)



ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/2), xytext=(X_r+2 ,.1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2), xytext=(X_l + .5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2)  , arrowprops = dict(facecolor = 'black'))
area = round(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_r) ,4)
ax.text(X_r+1.5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) + .08 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)
ax.text(X_l + .8 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)

ax.text(X_l + 0.2 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/6 , r'$P(f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) +   f'= {round(X_l , 2)}' , fontsize = 14)
ax.text(X_r - 1.5, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/6, r'$P(f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1])  + f'= {round(X_r , 2)}'  , fontsize = 14)


ax.text(6 , 0.6 , r'$\overline{x} = {%.3f} , \overline{y} = {%.3f}$' % (sample_x, sample_y) + f'\n' + r'$s_1 = {%.3f} , s_2 = {%.3f}$' %(stand_x, stand_y), fontsize = 15)
ax.text(6 , 0.4 , f'{ (1- (trust*2))*100}%신뢰도 에서의 신뢰구간\n' + r'$\left(\dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.05 , n-1 , m-1}} , \dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.95 , n-1 , m-1}}\right)$' + f'\n =' + r'$\left( {%.3f} , {%.3f} \right)$' % (round(stand_x**2 / stand_y**2 / X_r,4) , round(stand_x**2 / stand_y**2 / X_l,4))  , fontsize = 16)

ax.text(6 , 0.2 ,  r'$\alpha = {%.3f}$' % ((area*2)) +'\n' + r'$H_0 : \sigma^2_A = \sigma^2_B$' , Fontsize = 15 )

# #=================================가설검정=====================================
#
ax.set_title('양측 검정' , fontsize = 17)
#
X_L_1 = stand_x**2 / stand_y**2 #검정값

X_L_1 = abs(round(X_L_1,4))
if scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1) < 0.5:
    X_R_1 = round(float(scipy.stats.f(dof[0][0] , dof[0][1]).ppf(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1))),4)
else:
    X_R_1 = X_L_1
    X_L_1 = round(float(scipy.stats.f(dof[0][0] , dof[0][1]).ppf(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1))),4)
print(f'X_R_1 : {X_R_1}' )
print(f'X_L_1 : {X_L_1}' )

ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_R_1) | (X<=X_L_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔
#
area = round(float(scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1) + 1 - (scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_R_1))),4)
#
#
ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.f(dof[0][0] , dof[0][1]).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.f(dof[0][0] , dof[0][1]).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# #
annotate_len = stats.f(dof[0][0] , dof[0][1]).pdf(X_R_1) /2
plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1+X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1) -.5, annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_R_1-X_L_1)/2 , annotate_len+0.008 , f'P-value : \nP(X<={X_L_1}) + P(X>={X_R_1}) \n = {area}',fontsize=15)

b = ['F({},{})'.format(i,j) for i,j in dof]
plt.legend(b , fontsize= 15)

F-분포의 양측검정

H_0 : 모분산_A = 모분산_B(양측검정)

 

 

p-value : 0.6705

 

alpha : 0.05

 

p-value > alpha ==> 0.6705 >  0.05 ==> 귀무가설 H_0 : X_모분산_A = 모분산_B(양측검정) 채택한다. 즉 , 유의수준 5%에서 두 지역의 소비 지출의 분산이 같다.

 

 

EX-02) 두 지역에서 서식하고 있는 어떤 식물의 줄기 굵기를 측정한 결과, 다음과 같은 결과를 얻었다. 두 지역의 식물의 줄기 굵기에 대한 분산이 서로 같은지를 유의수준 5%에서 검정하라.

 


X = np.arange(0,10, .01)

fig = plt.figure(figsize=(20,12))


A = '0.8 1.8 1.0 0.1 0.9 1.7 1.4 1.0 0.9 1.2 0.5'
B = '1.0 0.8 1.6 2.6 1.3 1.1 2.4 1.8 2.5 1.4 1.9 2.0 1.2'

A= list(map(float , A.split(' ')))
B = list(map(float , B.split(' ')))

# Vars = np.var(A , ddof=1)
# Vars = 0.2**2
n = len(A)
m = len(B)
dof = [[n-1 , m-1]] #자유도

trust = 95
trust = round((1- trust/100)/2,3)



sample_x = np.mean(A)
stand_x = np.std(A , ddof=1)
print(f'|x : {sample_x}')
print(f's_x : {stand_x}')
sample_y = np.mean(B)
stand_y = np.std(B , ddof=1)
print(f'|y : {sample_y}')
print(f's_y : {stand_y}')
#
# STDS = math.sqrt(Vars)
# MO_std = 0.3


for i in dof:
    ax = sns.lineplot(X , scipy.stats.f(i[0] , i[1]).pdf(X))




X_r = scipy.stats.f(dof[0][0], dof[0][1]).ppf(1-trust)

X_l = scipy.stats.f(dof[0][0], dof[0][1]).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)



ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/2), xytext=(X_r+2 ,.1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2), xytext=(X_l + .5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2)  , arrowprops = dict(facecolor = 'black'))
area = round(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_r) ,4)
ax.text(X_r+1.5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) + .08 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)
ax.text(X_l + .8 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)

ax.text(X_l + 0.2 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/6 , r'$P(f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) +   f'= {round(X_l , 2)}' , fontsize = 14)
ax.text(X_r - 1.5, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/6, r'$P(f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1])  + f'= {round(X_r , 2)}'  , fontsize = 14)


ax.text(6 , 0.6 , r'$\overline{x} = {%.3f} , \overline{y} = {%.3f}$' % (sample_x, sample_y) + f'\n' + r'$s_1 = {%.3f} , s_2 = {%.3f}$' %(stand_x, stand_y), fontsize = 15)
ax.text(6 , 0.4 , f'{ (1- (trust*2))*100}%신뢰도 에서의 신뢰구간\n' + r'$\left(\dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.05 , n-1 , m-1}} , \dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.95 , n-1 , m-1}}\right)$' + f'\n =' + r'$\left( {%.3f} , {%.3f} \right)$' % (round(stand_x**2 / stand_y**2 / X_r,4) , round(stand_x**2 / stand_y**2 / X_l,4))  , fontsize = 16)

ax.text(6 , 0.2 ,  r'$\alpha = {%.3f}$' % ((area*2)) +'\n' + r'$H_0 : \sigma^2_A = \sigma^2_B$' , Fontsize = 15 )

# #=================================가설검정=====================================
#
ax.set_title('양측 검정' , fontsize = 17)
#
X_L_1 = stand_x**2 / stand_y**2 #검정값

X_L_1 = abs(round(X_L_1,4))
if scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1) < 0.5:
    X_R_1 = round(float(scipy.stats.f(dof[0][0] , dof[0][1]).ppf(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1))),4)
else:
    X_R_1 = X_L_1
    X_L_1 = round(float(scipy.stats.f(dof[0][0] , dof[0][1]).ppf(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1))),4)
print(f'X_R_1 : {X_R_1}' )
print(f'X_L_1 : {X_L_1}' )

ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_R_1) | (X<=X_L_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔
#
area = round(float(scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_L_1) + 1 - (scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_R_1))),4)
#
#
ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.f(dof[0][0] , dof[0][1]).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.f(dof[0][0] , dof[0][1]).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# #
annotate_len = stats.f(dof[0][0] , dof[0][1]).pdf(X_R_1) /2
plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1+X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1) -.5, annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_R_1-X_L_1)/2 , annotate_len+0.008 , f'P-value : \nP(X<={X_L_1}) + P(X>={X_R_1}) \n = {area}',fontsize=15)

b = ['F({},{})'.format(i,j) for i,j in dof]
plt.legend(b , fontsize= 15)

F-분포의 양측검정

H_0 : 모분산_A = 모분산_B(양측검정)

 

 

p-value : 0.569

 

alpha : 0.05

 

p-value > alpha ==> 0.569 >  0.05 ==> 귀무가설 H_0 : X_모분산_A = 모분산_B(양측검정) 채택한다. 즉 , 유의수준 5%에서 두 지역의 식물의 줄기 굵기의 분산은 동일하다.

 

728x90
반응형
728x90
반응형

1. 모분산 비에 대한 소표본 추론

 

==> 어느 회사에서 생산된 측정 기구의 정밀도가 더 정확한지 비교하는 경우도 있다. 이러한 상황에 대한 통계적 추론은 두 집단 사이의 모분산을 비교함으로써 해결할 수 있다.

 

https://knowallworld.tistory.com/310

 

★F-분포★두 표본분산의 비에 대한 표본분포★기초통계학-[모집단 분포와 표본분포 -11]

1. 두 표본분산의 비에 대한 표본분포 ==> 서로 독립인 두 정규모집단의 모분산이 다를때, 모분산 중에서 어느 것이 더큰지 비교하는 경우 ==> 모분산은 양수이므로, 두 모분산의 비의 값을 이용하

knowallworld.tistory.com

두 표본분산의 비의 표본분포는 F-분포

EX-01) 서로 독립인 두 정규모집단으로 각각 표본으로 선정하여 다음 결과를 얻었다. 모분산의 비 모분산_1/ 모분산_2 에 대한 90% 신뢰구간을 구하라.

A = pd.DataFrame({'표본 1 ' : [10 , '|x = 17.5' , 's_1 = 3.5'] , '표본 2 ' : [8 , '|y = 21.2' , 's_2 = 2.8']})
A

X = np.arange(0,10, .01)

fig = plt.figure(figsize=(20,12))


# A = '12.5 11.5 6.0 5.5 15.5 11.5 10.5 17.5 10.0 9.5 13.5 8.5 11.5 15.5'
# A= list(map(float , A.split(' ')))

# Vars = np.var(A , ddof=1)
Vars = 0.2**2
n = 10
m = 8
dof = [[n-1 , m-1]] #자유도

trust = 90
trust = round((1- trust/100)/2,3)



sample_x = 17.5
stand_x = 3.1

sample_y = 21.2
stand_y = 2.8

STDS = math.sqrt(Vars)
MO_std = 0.3


for i in dof:
    ax = sns.lineplot(X , scipy.stats.f(i[0] , i[1]).pdf(X))




X_r = scipy.stats.f(dof[0][0], dof[0][1]).ppf(0.95)

X_l = scipy.stats.f(dof[0][0], dof[0][1]).ppf(0.05)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)



ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')
ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔


ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/2), xytext=(X_r+2 ,.1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2), xytext=(X_l + .5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2)  , arrowprops = dict(facecolor = 'black'))
area = round(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_r) ,4)
ax.text(X_r+1.5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) + .08 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)
ax.text(X_l + .8 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)

ax.text(X_l + 0.2 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/6 , r'$P(f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) +   f'= {round(X_l , 2)}' , fontsize = 14)
ax.text(X_r - 1.5, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/6, r'$P(f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1])  + f'= {round(X_r , 2)}'  , fontsize = 14)


ax.text(6 , 0.6 , r'$\overline{x} = {%.3f} , \overline{y} = {%.3f}$' % (sample_x, sample_y) + f'\n' + r'$s_1 = {%.3f} , s_2 = {%.3f}$' %(stand_x, stand_y), fontsize = 15)
ax.text(6 , 0.4 , f'{ (1- (trust*2))*100}%신뢰도 에서의 신뢰구간\n' + r'$\left(\dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.05 , n-1 , m-1}} , \dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.95 , n-1 , m-1}}\right)$' + f'\n =' + r'$\left( {%.3f} , {%.3f} \right)$' % (round(stand_x**2 / stand_y**2 / X_r,4) , round(stand_x**2 / stand_y**2 / X_l,4))  , fontsize = 16)

F-구간의 신뢰구간

신뢰구간 : (0.333 , 4.036)

 

 

EX-02) 서로 독립인 두 정규모집단으로 각각 표본을 선정하여 다음 결과를 얻었다. 모분산의 비에 대한 95% 신뢰구간을 구하라.

 

A = pd.DataFrame({'표본 1 ' : [7 , '|x = 161' , 's_1 = 7.4'] , '표본 2 ' : [6 , '|y = 169' , 's_2 = 9.1']})
A

X = np.arange(0,10, .01)

fig = plt.figure(figsize=(20,12))


# A = '12.5 11.5 6.0 5.5 15.5 11.5 10.5 17.5 10.0 9.5 13.5 8.5 11.5 15.5'
# A= list(map(float , A.split(' ')))

# Vars = np.var(A , ddof=1)
Vars = 0.2**2
n = 10
m = 8
dof = [[n-1 , m-1]] #자유도

trust = 90
trust = round((1- trust/100)/2,3)



sample_x = 17.5
stand_x = 3.1

sample_y = 21.2
stand_y = 2.8

STDS = math.sqrt(Vars)
MO_std = 0.3


for i in dof:
    ax = sns.lineplot(X , scipy.stats.f(i[0] , i[1]).pdf(X))




X_r = scipy.stats.f(dof[0][0], dof[0][1]).ppf(0.95)

X_l = scipy.stats.f(dof[0][0], dof[0][1]).ppf(0.05)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)



ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')
ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔


ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/2), xytext=(X_r+2 ,.1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2), xytext=(X_l + .5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2)  , arrowprops = dict(facecolor = 'black'))
area = round(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_r) ,4)
ax.text(X_r+1.5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) + .08 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)
ax.text(X_l + .8 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)

ax.text(X_l + 0.2 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/6 , r'$P(f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) +   f'= {round(X_l , 2)}' , fontsize = 14)
ax.text(X_r - 1.5, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/6, r'$P(f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1])  + f'= {round(X_r , 2)}'  , fontsize = 14)


ax.text(6 , 0.6 , r'$\overline{x} = {%.3f} , \overline{y} = {%.3f}$' % (sample_x, sample_y) + f'\n' + r'$s_1 = {%.3f} , s_2 = {%.3f}$' %(stand_x, stand_y), fontsize = 15)
ax.text(6 , 0.4 , f'{ (1- (trust*2))*100}%신뢰도 에서의 신뢰구간\n' + r'$\left(\dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.05 , n-1 , m-1}} , \dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.95 , n-1 , m-1}}\right)$' + f'\n =' + r'$\left( {%.3f} , {%.3f} \right)$' % (round(stand_x**2 / stand_y**2 / X_r,4) , round(stand_x**2 / stand_y**2 / X_l,4))  , fontsize = 16)

# #=================================가설검정=====================================
#
ax.set_title('양측 검정' , fontsize = 17)
#
# X_L_1 = (n-1) * Vars / (MO_std**2) #검정값
# print(f'X_L_1 : {X_L_1}' )
# X_L_1 = abs(round(X_L_1,4))
#
# X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
# print(X_R_1)

# ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔
#
#
# area = round(float(scipy.stats.chi2(dof_2).cdf(X_L_1) + 1 - (scipy.stats.chi2(dof_2).cdf(X_R_1))),4)
#
#
# ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# #
# annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
# plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# ax.text( (X_R_1-X_L_1)/2 + 5, annotate_len+0.005 , f'P-value : \nP(X<={X_L_1}) + P(X>={X_R_1}) \n = {area}',fontsize=15)

X = np.arange(0,10, .01)

fig = plt.figure(figsize=(20,12))


# A = '12.5 11.5 6.0 5.5 15.5 11.5 10.5 17.5 10.0 9.5 13.5 8.5 11.5 15.5'
# A= list(map(float , A.split(' ')))

# Vars = np.var(A , ddof=1)
Vars = 0.2**2
n = 10
m = 8
dof = [[n-1 , m-1]] #자유도

trust = 90
trust = round((1- trust/100)/2,3)



sample_x = 17.5
stand_x = 3.1

sample_y = 21.2
stand_y = 2.8

STDS = math.sqrt(Vars)
MO_std = 0.3


for i in dof:
    ax = sns.lineplot(X , scipy.stats.f(i[0] , i[1]).pdf(X))




X_r = scipy.stats.f(dof[0][0], dof[0][1]).ppf(0.95)

X_l = scipy.stats.f(dof[0][0], dof[0][1]).ppf(0.05)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)



ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')
ax.fill_between(X, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔


ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/2), xytext=(X_r+2 ,.1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2), xytext=(X_l + .5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2)  , arrowprops = dict(facecolor = 'black'))
area = round(1- scipy.stats.f(dof[0][0] , dof[0][1]).cdf(X_r) ,4)
ax.text(X_r+1.5 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r) + .08 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)
ax.text(X_l + .8 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/2 , r'$P(F\leqq f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) + f'= {area}' , fontsize = 14)

ax.text(X_l + 0.2 , scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_l)/6 , r'$P(f_{%.3f,%2d,%2d})$' % (1-trust,dof[0][0] , dof[0][1]) +   f'= {round(X_l , 2)}' , fontsize = 14)
ax.text(X_r - 1.5, scipy.stats.f(dof[0][0] , dof[0][1]).pdf(X_r)/6, r'$P(f_{%.3f,%2d,%2d})$' % (trust,dof[0][0] , dof[0][1])  + f'= {round(X_r , 2)}'  , fontsize = 14)


ax.text(6 , 0.6 , r'$\overline{x} = {%.3f} , \overline{y} = {%.3f}$' % (sample_x, sample_y) + f'\n' + r'$s_1 = {%.3f} , s_2 = {%.3f}$' %(stand_x, stand_y), fontsize = 15)
ax.text(6 , 0.4 , f'{ (1- (trust*2))*100}%신뢰도 에서의 신뢰구간\n' + r'$\left(\dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.05 , n-1 , m-1}} , \dfrac{s^2_1}{s^2_2}*\dfrac{1}{f_{0.95 , n-1 , m-1}}\right)$' + f'\n =' + r'$\left( {%.3f} , {%.3f} \right)$' % (round(stand_x**2 / stand_y**2 / X_r,4) , round(stand_x**2 / stand_y**2 / X_l,4))  , fontsize = 16)

# #=================================가설검정=====================================
#
ax.set_title('양측 검정' , fontsize = 17)
#
# X_L_1 = (n-1) * Vars / (MO_std**2) #검정값
# print(f'X_L_1 : {X_L_1}' )
# X_L_1 = abs(round(X_L_1,4))
#
# X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
# print(X_R_1)

# ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔
#
#
# area = round(float(scipy.stats.chi2(dof_2).cdf(X_L_1) + 1 - (scipy.stats.chi2(dof_2).cdf(X_R_1))),4)
#
#
# ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# #
# annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
# plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# ax.text( (X_R_1-X_L_1)/2 + 5, annotate_len+0.005 , f'P-value : \nP(X<={X_L_1}) + P(X>={X_R_1}) \n = {area}',fontsize=15)

F-구간의 신뢰구간

신뢰구간 : (0.095 , 3.959)

728x90
반응형
728x90
반응형

1. 모분산에 대한 양측검정

 

EX-01) 어느 지역에 거주하는 청소년이 자원봉사에 참여하는 평균시간이 하루에 4.5시간이고 표준편차는 0.3시간이라고 한다. 이것을 알아보기 위하여 이 지역의 청소년 21명을 조사한 결과, 평균 4.3시간, 표준편차 0.2시간 이었다. 유의수준 5%에서 표준편차가 0.3시간이라는 주장을 조사하라.

 

 

m = 4.5

s  = 0.3

 

|X = 4.3

n = 21

s= 0.2

 

H_0 : s= 0.3(양측 검정)

 

X = np.arange(0,40 , .01)

fig = plt.figure(figsize=(20,12))


# A = '12.5 11.5 6.0 5.5 15.5 11.5 10.5 17.5 10.0 9.5 13.5 8.5 11.5 15.5'
# A= list(map(float , A.split(' ')))

# Vars = np.var(A , ddof=1)
Vars = 0.2**2
n = 21
dof_2 = [n-1] #자유도

trust = 95
trust = round((1- trust/100)/2,3)



STDS = math.sqrt(Vars)
MO_std = 0.3


ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )




X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)



ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.chi2(dof_2).pdf(X_r)/2), xytext=(X_r+2 ,.01)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.chi2(dof_2).pdf(X_l)/2), xytext=(X_l - 2 , .01)  , arrowprops = dict(facecolor = 'black'))
area = 1- scipy.stats.chi2(dof_2).cdf(X_r)
ax.text(X_r , .011, r'P(X>$\chi^2_{%.3f})$' % trust + f'= {area}',fontsize=15)
ax.text(X_l- 5 , .011, r'P(X>$\chi^2_{%.3f})$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(28 , 0.05 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(28 , 0.03 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha/2 , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(0 , 0.05 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS} \n ' + r'$\alpha = {%.2f}$' % (area*2) +'\n' + r'$H_0 : \sigma = {%.3f}$' % MO_std , fontsize = 16)


ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


#=================================가설검정=====================================

ax.set_title('양측 검정' , fontsize = 17)

X_L_1 = (n-1) * Vars / (MO_std**2) #검정값
print(f'X_L_1 : {X_L_1}' )
X_L_1 = abs(round(X_L_1,4))

X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
print(X_R_1)
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_R_1) | (X<=X_L_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔


area = round(float(scipy.stats.chi2(dof_2).cdf(X_L_1) + 1 - (scipy.stats.chi2(dof_2).cdf(X_R_1))),4)


ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
#
annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_R_1-X_L_1)/2 + 5, annotate_len+0.005 , f'P-value : \nP(X<={X_L_1}) + P(X>={X_R_1}) \n = {area}',fontsize=15)

카이제곱분포의 양측검정

 

H_0 : 모표준편차 = 0.3(양측검정)

 

p-value : 0.0317

 

alpha : 0.05

 

p-value < alpha ==> 0.0317 <  0.05 ==> 귀무가설 H_0 : 모표준편차 = 0.3 기각한다. 즉 , 유의수준 5%에서 모표준편차는 0.3이 아니다.

 

 

EX-02) 정규모집단으로부터 크기 10인 표본을 임의로 선정하여 조사한 결과가 다음과 같았다. 모분산이 0.8이라는 주장에 대하여 유의수준 1%에서 조사하라.

 

X = np.arange(0,40 , .01)

fig = plt.figure(figsize=(20,12))


A = '1.5 1.1 3.6 1.5 1.7 2.1 3.2 2.5 2.8 2.9'
A= list(map(float , A.split(' ')))

Vars = np.var(A , ddof=1)
# Vars = 0.2**2
n = len(A)
dof_2 = [n-1] #자유도

trust = 99
trust = round((1- trust/100)/2,3)



STDS = math.sqrt(Vars)
MO_std = math.sqrt(0.8)


ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )




X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(f'X_r : {X_r}')
print(f'X_l : {X_l}')



ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.chi2(dof_2).pdf(X_r)/2), xytext=(X_r+2 ,.01)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.chi2(dof_2).pdf(X_l)/2), xytext=(X_l - 2 , .01)  , arrowprops = dict(facecolor = 'black'))
area = 1- scipy.stats.chi2(dof_2).cdf(X_r)
ax.text(X_r , .011, r'P(X>$\chi^2_{%.3f})$' % trust + f'= {area}',fontsize=15)
ax.text(X_l- 5 , .011, r'P(X>$\chi^2_{%.3f})$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(28 , 0.05 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(28 , 0.03 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha/2 , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(15 , 0.05 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS} \n ' + r'$\alpha = {%.2f}$' % (area*2) +'\n' + r'$H_0 : \sigma = {%.3f}$' % MO_std , fontsize = 16)


ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


#=================================가설검정=====================================

ax.set_title('양측 검정' , fontsize = 17)

X_L_1 = (n-1) * Vars / (MO_std**2) #검정값
print(f'X_L_1 : {X_L_1}' )
X_L_1 = abs(round(X_L_1,4))

X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
print(f'X_R_1 : {X_R_1}' )
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_R_1) | (X<=X_L_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔


area = round(float(scipy.stats.chi2(dof_2).cdf(X_L_1) + 1 - (scipy.stats.chi2(dof_2).cdf(X_R_1))),4)


ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
#
annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_R_1-X_L_1)/2 + 5, annotate_len+0.005 , f'P-value : \nP(X<={X_L_1}) + P(X>={X_R_1}) \n = {area}',fontsize=15)

카이제곱분포의 양측검정

H_0 : 모분산 = 0.8(양측검정)

 

p-value : 0.8985

 

alpha : 0.01

 

p-value > alpha ==> 0.8985 >  0.01 ==> 귀무가설 H_0 : 모분산 = 0.8 채택한다. 즉 , 유의수준 1%에서 모분산은 0.8이다.

 

2. 모분산에 대한 하단측 검정

 

EX-03) 어느 단체가, 특정한 수입 자동차 연비의 표준편차가 1.2km/L 이상이라고 주장한다. 이 주장에 대해 조사하기 위하여 동일한 모델의 자동차 13대를 주행 시험한 결과 , 표준편차가 0.82km/L 였다. 자동차 연비가 정규분포를 따른다고 할 때, 이 단체의 주장을 수용할 수 잇는지 유의수준 5%에서 조사하라.

 

 

H_0 : (모표준편차)sigma >= 1.2 (하단측 검정)

n = 13

s = 0.82

 

X = np.arange(0,40 , .01)

fig = plt.figure(figsize=(20,12))


# A = '1.5 1.1 3.6 1.5 1.7 2.1 3.2 2.5 2.8 2.9'
# A= list(map(float , A.split(' ')))

# Vars = np.var(A , ddof=1)
Vars = 0.82**2
n = 13
dof_2 = [n-1] #자유도

trust = 95
trust = round((1- trust/100),3)



STDS = math.sqrt(Vars)
MO_std = 1.2
print(MO_std)

ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )




# X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
# print(f'X_r : {X_r}')
print(f'X_l : {X_l}')



ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where =  (X>=X_l) , facecolor = 'orange')



# ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
# plt.annotate('' , xy=(X_r , scipy.stats.chi2(dof_2).pdf(X_r)/2), xytext=(X_r+2 ,.01)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.chi2(dof_2).pdf(X_l)/2), xytext=(X_l - 2 , .02)  , arrowprops = dict(facecolor = 'black'))
area = scipy.stats.chi2(dof_2).cdf(X_l)
# ax.text(X_r , .011, r'P(X>$\chi^2_{%.3f})$' % trust + f'= {area}',fontsize=15)
ax.text(X_l- 5 , .021, r'P(V>$\chi^2_{%.3f})$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(28 , 0.05 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(28 , 0.03 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(16 , 0.05 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS} \n ' + r'$\alpha = {%.2f}$' % (area) +'\n' + r'$H_0 : \sigma = {%.3f}$' % MO_std , fontsize = 16)


ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
# ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


#=================================가설검정=====================================

ax.set_title('하단측 검정' , fontsize = 17)

X_L_1 = (n-1) * Vars / (MO_std**2) #검정값
print(f'X_L_1 : {X_L_1}' )
X_L_1 = abs(round(X_L_1,4))

# X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
# print(f'X_R_1 : {X_R_1}' )
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where =  (X<=X_L_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where =  (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔


area = round(float(scipy.stats.chi2(dof_2).cdf(X_L_1)),4)


ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
#
annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
plt.annotate('' , xy=(X_L_1, annotate_len/2), xytext=((X_L_1)*2 , annotate_len/2)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_L_1)*2 , annotate_len/2 , f'P-value : \nP(V<={X_L_1})) \n = {area}',fontsize=15)

카이제곱분포의 하단측검정

 

H_0 : (모표준편차)sigma >= 1.2 (하단측 검정)

 

p-value : 0.0653

 

alpha : 0.05

 

p-value > alpha ==> 0.0653 >  0.05 ==> 귀무가설 H_0 : (모표준편차)sigma >= 1.2 (하단측 검정) 채택한다. 즉 , 자동차 연비의 표준편차가 1.2km/L 이상이다.

 

EX-04) 정규모집단에서 모표준편차가 0.09보다 작은지 알아보기 위하여, 크기 12인 표본을 추출하여 조사한 결과 표본표준편차가 0.05였다. 모표준편차가 0.09보다 작은지 유의수준 5%에서 조사하라.

 

 

H_0 : sigma(o) >= 0.09 (하단측 검정)

 

 

n = 12

s = 0.05

 

X = np.arange(0,40 , .01)

fig = plt.figure(figsize=(20,12))


# A = '1.5 1.1 3.6 1.5 1.7 2.1 3.2 2.5 2.8 2.9'
# A= list(map(float , A.split(' ')))

# Vars = np.var(A , ddof=1)
Vars = 0.05**2
n = 12
dof_2 = [n-1] #자유도

trust = 95
trust = round((1- trust/100),3)



STDS = math.sqrt(Vars)
MO_std = 0.09
print(MO_std)

ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )




# X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
# print(f'X_r : {X_r}')
print(f'X_l : {X_l}')



ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where =  (X>=X_l) , facecolor = 'orange')



# ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
# plt.annotate('' , xy=(X_r , scipy.stats.chi2(dof_2).pdf(X_r)/2), xytext=(X_r+2 ,.01)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l , scipy.stats.chi2(dof_2).pdf(X_l)/2), xytext=(X_l - 2 , .02)  , arrowprops = dict(facecolor = 'black'))
area = scipy.stats.chi2(dof_2).cdf(X_l)
# ax.text(X_r , .011, r'P(X>$\chi^2_{%.3f})$' % trust + f'= {area}',fontsize=15)
ax.text(X_l- 5 , .021, r'P(V>$\chi^2_{%.3f})$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(28 , 0.05 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(28 , 0.03 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(16 , 0.05 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS} \n ' + r'$\alpha = {%.2f}$' % (area) +'\n' + r'$H_0 : \sigma = {%.3f}$' % MO_std , fontsize = 16)


ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
# ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


#=================================가설검정=====================================

ax.set_title('하단측 검정' , fontsize = 17)

X_L_1 = (n-1) * Vars / (MO_std**2) #검정값
print(f'X_L_1 : {X_L_1}' )
X_L_1 = abs(round(X_L_1,4))

# X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
# print(f'X_R_1 : {X_R_1}' )
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where =  (X<=X_L_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where =  (X<=X_l) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔


area = round(float(scipy.stats.chi2(dof_2).cdf(X_L_1)),4)


ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
#
annotate_len = stats.chi2(dof_2).pdf(X_L_1) /2
plt.annotate('' , xy=(X_L_1, annotate_len/2), xytext=((X_L_1)*2 , annotate_len/2)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_L_1)*2 , annotate_len/2 , f'P-value : \nP(V<={X_L_1})) \n = {area}',fontsize=15)

카이제곱분포의 하단측검정

H_0 : (모표준편차)sigma >= 0.09 (하단측 검정)

 

p-value : 0.0156

 

alpha : 0.05

 

p-value < alpha ==> 0.0156 <  0.05 ==> 귀무가설 H_0 : (모표준편차)sigma >= 0.09 (하단측 검정) 기각한다. 즉 , 모표준편차가 0.09보다 작다.

 

 

 

3. 모분산에 대한 상단측 검정

 

EX-05) 어느 생수 회사에서 자신들이 생산하는 생수 페트병의 순수 용량은 평균 1.5L , 분산은 0.3L 이하라고 주장한다. 이것을 확인하기 위하여 임의로 페트병 15개를 수거하여 측정한 결과 , 분산이 0.48L였다. 이 생수 회사의 주장이 타당한지 유의수준 5%에서 조사하라.

 

 

H_0 : 모분산 <= 0.3 (상단측검정)

 

n = 15

s**2 = 0.48

 

X = np.arange(0,40 , .01)

fig = plt.figure(figsize=(20,12))


# A = '1.5 1.1 3.6 1.5 1.7 2.1 3.2 2.5 2.8 2.9'
# A= list(map(float , A.split(' ')))

Vars = 0.48
# Vars = 0.2**2
n = 15
dof_2 = [n-1] #자유도

trust = 95
trust = round((1- trust/100),3)



STDS = math.sqrt(Vars)
MO_std = math.sqrt(0.3)
print(MO_std)

ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )




X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(f'X_r : {X_r}')
print(f'X_l : {X_l}')



ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X<=X_r) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
# ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.chi2(dof_2).pdf(X_r)/2), xytext=(X_r+2 ,.01)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(X_l , scipy.stats.chi2(dof_2).pdf(X_l)/2), xytext=(X_l - 2 , .01)  , arrowprops = dict(facecolor = 'black'))
area = 1- scipy.stats.chi2(dof_2).cdf(X_r)
ax.text(X_r , .011, r'P(X>$\chi^2_{%.3f})$' % trust + f'= {area}',fontsize=15)
# ax.text(X_l- 5 , .011, r'P(X>$\chi^2_{%.3f})$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(28 , 0.05 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(28 , 0.03 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(15 , 0.05 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS} \n ' + r'$\alpha = {%.2f}$' % (area) +'\n' + r'$H_0 : \sigma^2 <= {%.3f}$' % (MO_std**2) , fontsize = 16)


# ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


#=================================가설검정=====================================

ax.set_title('상단측 검정' , fontsize = 17)

X_R_1 = (n-1) * Vars / (MO_std**2) #검정값
print(f'X_R_1 : {X_R_1}' )
X_R_1 = abs(round(X_R_1,4))
#
# X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
# print(f'X_R_1 : {X_R_1}' )
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_R_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔


area = round(float( 1 - (scipy.stats.chi2(dof_2).cdf(X_R_1))),4)


# ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
#
annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
# plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_R_1)/2 + 5, annotate_len+0.005 , f'P-value : \n P(X>={X_R_1}) \n = {area}',fontsize=15)

카이제곱분포의 상단측 검정

 

H_0 : (모분산)sigma^2 <= 0.548 (상단측 검정)

 

p-value : 0.0708

 

alpha : 0.05

 

p-value > alpha ==> 0.0708 >  0.05 ==> 귀무가설 H_0 : (모분산)sigma^2 <= 0.3 (상단측 검정) 채택한다. 즉 , 모분산이 0.3보다 작다.

 

EX-06) 정규모집단에서 크기 17인 표본을 조사하여 표본평균 15.1과 표준편차 2.7을 얻었다. 가설 H_0: 모분산<=4.5를 유의수준 5%에서 조사하라.

 

 

H_0 : 모분산 <= 4.5(상단측 검정)

 

n = 17

|x = 15.1

s = 2.7

 

X = np.arange(0,40 , .01)

fig = plt.figure(figsize=(20,12))


# A = '1.5 1.1 3.6 1.5 1.7 2.1 3.2 2.5 2.8 2.9'
# A= list(map(float , A.split(' ')))

Vars = 2.7**2
# Vars = 0.2**2
n = 17
dof_2 = [n-1] #자유도

trust = 95
trust = round((1- trust/100),3)



STDS = math.sqrt(Vars)
MO_std = math.sqrt(4.5)
print(MO_std)

ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )




X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(f'X_r : {X_r}')
print(f'X_l : {X_l}')



ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X<=X_r) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
# ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r , scipy.stats.chi2(dof_2).pdf(X_r)/2), xytext=(X_r+2 ,.01)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(X_l , scipy.stats.chi2(dof_2).pdf(X_l)/2), xytext=(X_l - 2 , .01)  , arrowprops = dict(facecolor = 'black'))
area = 1- scipy.stats.chi2(dof_2).cdf(X_r)
ax.text(X_r , .011, r'P(X>$\chi^2_{%.3f})$' % trust + f'= {area}',fontsize=15)
# ax.text(X_l- 5 , .011, r'P(X>$\chi^2_{%.3f})$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(28 , 0.05 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(28 , 0.03 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(15 , 0.05 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS} \n ' + r'$\alpha = {%.2f}$' % (area) +'\n' + r'$H_0 : \sigma^2 <= {%.3f}$' % (MO_std**2) , fontsize = 16)


# ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/4 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


#=================================가설검정=====================================

ax.set_title('상단측 검정' , fontsize = 17)

X_R_1 = (n-1) * Vars / (MO_std**2) #검정값
print(f'X_R_1 : {X_R_1}' )
X_R_1 = abs(round(X_R_1,4))
#
# X_R_1 = round(float(scipy.stats.chi2(dof_2).ppf(1- scipy.stats.chi2(dof_2).cdf(X_L_1))),4)
# print(f'X_R_1 : {X_R_1}' )
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_R_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔


area = round(float( 1 - (scipy.stats.chi2(dof_2).cdf(X_R_1))),4)


# ax.vlines(x= X_L_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_L_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= X_R_1, ymin= 0 , ymax= stats.chi2(dof_2).pdf(X_R_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
#
annotate_len = stats.chi2(dof_2).pdf(X_R_1) /2
# plt.annotate('' , xy=(X_L_1, annotate_len), xytext=((X_R_1-X_L_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_R_1, annotate_len), xytext=((X_R_1)/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text( (X_R_1)/2 + 5, annotate_len+0.005 , f'P-value : \n P(X>={X_R_1}) \n = {area}',fontsize=15)

카이제곱분포의 상단측 검정

H_0 : (모분산)sigma^2 <= 4.5 (상단측 검정)

 

p-value : 0.0552

 

alpha : 0.05

 

p-value > alpha ==> 0.0552 >  0.05 ==> 귀무가설 H_0 : (모분산)sigma^2 <= 4.5 (상단측 검정) 채택한다. 즉 , 모분산이 4.5보다 작다.

 

 

 

 

728x90
반응형
728x90
반응형

1. 모분산에 대한 소표본 추론

==> 정규모집단의 모분산과 모표준편차에 대한 구간추정 및 가설검정 방법

 

https://knowallworld.tistory.com/321

 

★종합선물세트★F-분포(표본분산의 비 이용!)★표본평균의 차에 따른 확률분포!★합동표본분산

29. 두 정규모집단 A와 B의 모분산은 동일하고, 평균은 각각 뮤_1 = 700 , 뮤_2 = 680이라 한다. 두 모집단으로 부터 표본을 추출하여 A 표본 [n = 17 , |x = 704 , s_1 = 39.25] , B표본 [m = 10 , |y = 675 , s_2 = 43.75] h

knowallworld.tistory.com

https://knowallworld.tistory.com/305

 

★표본분산 S**2 , 관찰 표본분산 s_0**2★카이제곱분포표★모분산의 표본분포★기초통계학-[모집

1. 모분산의 표본분포 정규모집단 N(뮤 , 모분산) 으로부터 크기 n인 표본을 선정할 때 표본분산 ==> 표본분산 S**2에 대한 표본분포는 X**2-통계량 V에 대하여 자유도가 n-1인 카이제곱분포이다. https:

knowallworld.tistory.com

카이제곱분포

EX-01) 어느 제약회사에서 생산되는 캡슐 감기약의 무게에 대한 분산을 알아보기 위하여 시중에서 판매되는 감기약 16개를 수거하여 무게를 측정한 결과 다음과 같았다. 이 자료를 이용하여 다음을 구하라.

X = np.arange(0,30 , .01)

fig = plt.figure(figsize=(20,12))


A = '4.23 4.26 4.26 4.24 4.27 4.23 4.19 4.27 4.21 4.25 4.23 4.29 4.30 4.24 4.20 4.24'
A= list(map(float , A.split(' ')))

Vars = np.var(A , ddof=1)

dof_2 = [len(A)-1] #자유도

trust = 95
trust = round((1- trust/100)/2,3)

n = len(A)

STDS = math.sqrt(Vars)
ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )
X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)


ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r + 2, .001), xytext=(28 , .01)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l - 2, .001), xytext=(2 , .01)  , arrowprops = dict(facecolor = 'black'))
area = 1- scipy.stats.chi2(dof_2).cdf(X_r)
ax.text(X_r , .011, r'$\chi^2_{%.3f}$' % trust + f'= {area}',fontsize=15)
ax.text(X_l- 5 , .011, r'$\chi^2_{%.3f}$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(18 , 0.07 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(18 , 0.05 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha/2 , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(0 , 0.07 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS}' , fontsize = 16)


ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

카이제곱분포를 따르는 신뢰구간

 

==> 모분산에 의한 신뢰구간 ==> (0.0005 , 0.0022)

 

==> 모표준편차에 의한 신뢰구간 ==> (0.0224 , 0.0469)

 

 

EX-02) 제주도에서 여름 휴가를 보내기 위해 무작위로 동일한 규모의 펜션 사용료를 조사한 결과 다음과 같았다. 물음에 답하라.

 

X = np.arange(0,30 , .01)

fig = plt.figure(figsize=(20,12))


A = '12.5 11.5 6.0 5.5 15.5 11.5 10.5 17.5 10.0 9.5 13.5 8.5 11.5 15.5'
A= list(map(float , A.split(' ')))

Vars = np.var(A , ddof=1)

dof_2 = [len(A)-1] #자유도

trust = 95
trust = round((1- trust/100)/2,3)

n = len(A)

STDS = math.sqrt(Vars)
ax = sns.lineplot(x = X , y=scipy.stats.chi2(dof_2).pdf(X) )
X_r =  scipy.stats.chi2(dof_2).ppf(1-trust)
X_l = scipy.stats.chi2(dof_2).ppf(trust)
# t_r = round( (x_0 - (0)) / (math.sqrt(33.463) * math.sqrt(1/16 + 1/16)), 3)
print(X_r)


ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X>=X_r) | (X<=X_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.chi2(dof_2).pdf(X) , 0 , where = (X<=X_r) & (X>=X_l) , facecolor = 'orange')



ax.vlines(x = X_r ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_r) , colors = 'black')
ax.vlines(x = X_l ,ymin=0 , ymax= scipy.stats.chi2(dof_2).pdf(X_l) , colors = 'black')
plt.annotate('' , xy=(X_r + 2, .001), xytext=(28 , .01)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(X_l - 2, .001), xytext=(2 , .01)  , arrowprops = dict(facecolor = 'black'))
area = 1- scipy.stats.chi2(dof_2).cdf(X_r)
ax.text(X_r , .011, r'$\chi^2_{%.3f}$' % trust + f'= {area}',fontsize=15)
ax.text(X_l- 5 , .011, r'$\chi^2_{%.3f}$' % (1-trust) + f'= {area}',fontsize=15)


ax.text(18 , 0.07 , f'n< 10에 대한 신뢰구간\n' + r'$(e_{%d})= \left(\dfrac{(n-1)s^{2}}{X^2_{\alpha/2 , n-1}} ,\dfrac{(n-1)s^{2}}{X^2_{-(\alpha/2) , n-1}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(float((n-1) * STDS**2 / X_r),5)} , {round(float((n-1) * STDS**2 / X_l),5)})' , fontsize = 16)
ax.text(18 , 0.05 ,f'n>= 10에 대한 신뢰구간\n' +  r'신뢰구간$(e_{%d}) = \left(s\sqrt{\dfrac{(n-1)}{X^2_{\alpha/2 , n-1}}} ,s\sqrt{\dfrac{(n-1)}{X^2_{-(\alpha/2) , n-1}}} \right)$' % ((1- (trust*2))*100) + f'\n = ({round(math.sqrt((n-1) * STDS**2 / X_r),4)} , {round(math.sqrt((n-1) * STDS**2 / X_l),4)})' , fontsize = 16)
ax.text(0 , 0.07 , r'카이제곱분포(V) = $\left(\dfrac{(n-1)s^{2}}{\sigma^{2}}\right)$' + f'\n n = {n}\n s = {STDS}' , fontsize = 16)


ax.text(X_l + 0.4 , scipy.stats.chi2(dof_2).pdf(X_l)/2 , r'$\chi^2_L= {%.4f}$' % X_l  , fontsize = 13)
ax.text(X_r - 3 , scipy.stats.chi2(dof_2).pdf(X_r)/2 , r'$\chi^2_R= {%.4f}$' % X_r   , fontsize = 13)


b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

카이제곱분포를 따르는 신뢰구간

==> 모분산에 의한 신뢰구간 ==> (6.27493 , 30.98862)

 

==> 모표준편차에 의한 신뢰구간 ==> (2.505 , 5.5667)

728x90
반응형
728x90
반응형

1. 쌍체 t-검정

 

==> 서로 독립인 두 모집단으로 부터 각각 표본을 추출하여 모평균을 비교하였으며, 두 표본의 대상은 서로 독립이었다.

 

==> 쌍체 t-검정은 결과 전·후 자료의 집단의 평균의 차에 대한 가설을 검정

 

t-분포
t-분포 활용

==> |d ==> 관찰값의 차

 

쌍체 t-검정

EX-01) 골프채를 생산하는 한 회사가 자신들이 개발한 신형 골프채를 사용하면 타수를 낮출 수 있다고 주장한다. 6명의 골퍼를 무작위로 선정하여 가장 최근에 기록한 5번의 평균 타수를 물어본 다음, 신형 골프채를 사용하여 각각 5번씩 골플를 치게 한 후에 평균 타수를 물어본 결과 다음 표와 같았다. 유의수준 5%에서 신형 골프채를 사용하면 타수가 줄어드는지 조사하라.

 

 

 

H_0 : 구형골프채의 타수 <= 신형골프채의 타수 (상단측)

A = pd.DataFrame({'골퍼' : [1 , 2, 3, 4, 5, 6] , '구형 골프채' : [95 , 102 , 83 , 92 ,85 , 96] , '신형 골프채' : [92 , 96 , 83 , 88 , 84 , 91]}).T

A

A = pd.DataFrame({'골퍼' : [1 , 2, 3, 4, 5, 6] , '구형 골프채' : [95 , 102 , 83 , 92 ,85 , 96] , '신형 골프채' : [92 , 96 , 83 , 88 , 84 , 91]}).T
A.loc['타수 차이 d_i'] = A.iloc[1 , :] - A.iloc[2 , :]
A

 

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))


#
# A = "14.8 9.5 11.2 9.8 10.2 9.4"
# A = list(map(float, A.split(' ')))
# print(A)


MEANS = np.mean(A.iloc[-1 , :])
STDS = np.std(A.iloc[-1 , :] , ddof= 1)
MO_MEAN = 0



n = len(A.iloc[-1 , :]) #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4) #오차한계


ax.set_title('상단측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{%d} = t_{{\alpha}}*\dfrac{s}{\sqrt{n}}$' % ((1-  trust)*100 ) +f'= {E}' ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}>T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(2.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_r + '\n' +r'${\alpha}$ =' +f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
# ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(1- float(scipy.stats.t(dof_2).cdf(t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X>=t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5, annotate_len+0.03 , f'P-value : \nP(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)


쌍체 t-검정에 대한 상단측 검정

 H_0 : 구형골프채의 타수 <= 신형골프채의 타수 (상단측)

 

p-value : 0.0102

 

alpha : 0.05

 

p-value < alpha ==> 0.0102 <  0.05 ==> 귀무가설 H_0 : 구형골프채의 타수 <= 신형골프채의 타수 기각한다. 즉 , 유의수준 5%에서 신형골프채의 타수가 더 적다.

 

 

EX-02) 어느 제약회사는 기존의 감기약 A에 비하여 새로 개발한 신약 B가 더 효과적인지 알아보기위해 과거에 감기약 A를 사용했던 감기 환자 8명을 임의로 선정하여 회복기간을 조사하였고, 그 결과가 다음과 같았다. 유의수준 10%에서 신약의 효과가 있는지 조사하라.

 

H_0 : A의회복기간 <= B의 회복기간 (상단측 검정)

 

A = pd.DataFrame({'감기환자' : [1 , 2, 3, 4, 5, 6,7,8] , 'A의 회복 기간' : [6 ,4,3,5,5,7,6,4] , 'B의 회복기간' : [4,3,4,3,4,6,3,3]}).T
A.loc['A의 회복기간 - B의 회복기간'] = A.iloc[1 , :] - A.iloc[2 , :]
A

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,8))


#
# A = "14.8 9.5 11.2 9.8 10.2 9.4"
# A = list(map(float, A.split(' ')))
# print(A)


MEANS = np.mean(A.iloc[-1 , :])
STDS = np.std(A.iloc[-1 , :] , ddof= 1)
MO_MEAN = 0



n = len(A.iloc[-1 , :]) #표본개수
dof_2 = [n-1] #자유도c

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 90 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS / math.sqrt(n)),4) #오차한계


ax.set_title('상단측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .32, f'평균(MEANS) = {MEANS}\n'  +f' n = {n} \n 표준편차(s) = {STDS}\n' +r'오차한계 $e_{%d} = t_{{\alpha}}*\dfrac{s}{\sqrt{n}}$' % ((1-  trust)*100 ) +f'= {E}' ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}>T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = (-$\infty , MEANS + e_{\alpha}$)' +f'\n' + r' = $(-\infty , {%.4f} + {%.4f})$' % (MEANS, E)   +f'\n' +r'$ = (-\infty , {%.4f})$' % (MEANS+E)  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(2.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_r + '\n' +r'${\alpha}$ =' +f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
# ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS - MO_MEAN)/ (STDS / math.sqrt(n)),4)

print(t_1)
t_1 = abs(t_1)
area = round(1- float(scipy.stats.t(dof_2).cdf(t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X>=t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5, annotate_len+0.03 , f'P-value : \nP(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'

ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

쌍체 t-검정에 대한 상단측 검정

H_0 : A의회복기간 <= B의 회복기간 (상단측 검정)

 

p-value : 0.0095

 

alpha : 0.1

 

p-value < alpha ==> 0.0095 <  0.1 ==> 귀무가설 H_0 : A의 회복기간 <= B의 회복기간 기각한다. 즉 , 유의수준 10%에서 B의 회복기간이 더 효과가 있다.

728x90
반응형
728x90
반응형

1. 모평균의 차에 대한 소표본 가설검정

 

EX-01) 배기량이 2000cc인 차량의 rpm이 3000cc인 차량의 rpm보다 높은지 유의수준 5%에서 조사하라.

 

H_0 : m_2000 <= m_3000

 

|X_m_2000 - |Y_m_3000 <= 0 (상단측 검정)

 

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,15))


A = [2360, 2330 , 2350 , 2430 , 2380 , 2360]
B = [2250 , 2230 , 2300 , 2240 , 2260 , 2340]



MEANS_A = np.mean(A)
# print(MEANS_A)
STDS_A = np.std(A , ddof=1)
# print(STDS_A**2)
MEANS_B =  np.mean(B)
# print(MEANS_B)
STDS_B = np.std(B , ddof=1)
# print(STDS_B**2)
n_A = len(A) #표본개수
n_B = len(B)
dof = n_A+n_B-2
dof_2 = [dof] #자유도c

MEANS = round(MEANS_A - MEANS_B,4)
STDS = round(math.sqrt(( (n_A-1) * (STDS_A**2) + (n_B-1) * (STDS_B**2))/(dof)),4) # 합동표본표준편차

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100) , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS * math.sqrt((1/n_A + 1/n_B))),4)


ax.set_title('두 모평균 차에 대한 소표본 상단측 검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<=t_r)  , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))


ax.text(-4.6 , .28, f'평균(MEANS_A - MEANS_B) = {MEANS}\n'  +f' n = {n_A} , m = {n_B}  \n 합동표본분산' +r'$(s^{2})$ = ' + '\n' + r'$S _{p}^{2}=\dfrac{1}{n+m-2}\left[ \left( n-1\right) S _{1}^{2}+\left( m-1\right) S_{2}^{2}\right]$' + f'\n = {STDS}\n' +r'오차한계 $e_{%d} = t_{\dfrac{\alpha}{2}}*{s}*\sqrt{\dfrac{1}{n} + \dfrac{1}{m}}$' % ((1-  trust)*100 ) +f'= {E} \n\n' + r'T = $\dfrac{\overline{X} - \overline{Y} - ({\mu}_{1} - {\mu}_{2})}{s_{p} * \sqrt{\dfrac{1}{n} + \dfrac{1}{m}}}$ ~ t(n+m-2)'  ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))


ax.text(1.6 , .25, r'$P(t_{%.3f}>T)$' % (trust) + f'= {area}\n' + r'신뢰구간 = ( -$\infty$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $(-\infty, {%.4f} + {%.4f})$' % (MEANS, E )  +f'\n' +r'$ = (-\infty , {%.4f})$' % (MEANS+E)  ,fontsize=15)

# ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')




# plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
# ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(2.71 , .13, r'$t_{{\alpha}} = {%.4f}$' % t_r + '\n' +r'${\alpha}$ =' +f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
# ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)

#======



#==================================== 가설 검정 ==========================================



t_1 = round((MEANS)/ (STDS * math.sqrt((1/n_A + 1/n_B))),4)

print(t_1)
t_1 = abs(t_1)
area = round(1- float(scipy.stats.t(dof_2).cdf(t_1) ),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where =  (X>=t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
# ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
# plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5, annotate_len+0.03 , f'P-value : \nP(T>={t_1}) \n = {area}',fontsize=15)

mo = '모평균'
#
# ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { round((MEANS - MO_MEAN)/(STDS / math.sqrt(n)),4) }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

모평균 차에 대한 소표본 상단축 검정

H_0 : m_2000 <= m_3000

 

|X_m_2000 - |Y_m_3000 <= 0 (상단측 검정)

 

p-value : 0.0006

 

alpha : 0.05

 

p-value < alpha ==> 0.0006 <  0.05 ==> 귀무가설 H_0 : X_m_2000 <= Y_m_3000 기각한다. 즉 , 유의수준 5%에서2000cc인 차량의 rpm이 3000cc 차량의 rpm보다 높다고 할 수 있다.

 

 

EX-02) 두 회사의 커피에 함유된 카페인의 양이 같은지 유의수준 5%에서 조사하라.

 

A = [ n= 8 , |x = 109 , s_1 = 루트(4.25) ]

B = [m = 6 , |y = 107 , s_2 = 루트(4.36) ]

 

H_0 : A_m = B_m (양측검정)

X = np.arange(-5,5 , .01)

fig = plt.figure(figsize=(15,15))


#
# A = "1073 1067 1103 1122 1057 1096 1057 1053 1089 1102 1100 1091 1053 1138 1063 1120 1077 1091"
# A = list(map(int, A.split(' ')))


# A = [2360, 2330 , 2350 , 2430 , 2380 , 2360]
# B = [2250 , 2230 , 2300 , 2240 , 2260 , 2340]



MEANS_A = 109
# print(MEANS_A)
STDS_A = math.sqrt(4.25)
# print(STDS_A**2)
MEANS_B =  107
# print(MEANS_B)
STDS_B = math.sqrt(4.36)
# print(STDS_B**2)
n_A = 8 #표본개수
n_B = 6
dof = n_A+n_B-2
dof_2 = [dof] #자유도c

MEANS = round(MEANS_A - MEANS_B,4)
STDS = round(math.sqrt(( (n_A-1) * (STDS_A**2) + (n_B-1) * (STDS_B**2))/(dof)),4) # 합동표본표준편차

ax = sns.lineplot(x = X , y=scipy.stats.t(dof_2).pdf(X) )
trust = 95 #신뢰도
trust = round( (1- trust/100)/2 , 4)
t_r =  scipy.stats.t(dof_2).ppf(1- trust)
print(t_r)
t_l = scipy.stats.t(dof_2).ppf(trust)
print(t_l)

E = round(float(t_r * STDS * math.sqrt((1/n_A + 1/n_B))),4)


ax.set_title('두 모평균 차에 대한 양측검정' , fontsize = 15)
# =========================================================

ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X<t_r) & (X>t_l) , facecolor = 'orange') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=t_l) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
area = round(float(scipy.stats.t(dof_2).cdf(t_r) - scipy.stats.t(dof_2).cdf(t_l)),4)


plt.annotate('' , xy=(0, .2), xytext=(-2.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(-4.6 , .28, f'평균(MEANS_A - MEANS_B) = {MEANS}\n'  +f' n = {n_A} , m = {n_B}  \n 합동표본분산' +r'$(s^{2})$ = ' + '\n' + r'$S _{p}^{2}=\dfrac{1}{n+m-2}\left[ \left( n-1\right) S _{1}^{2}+\left( m-1\right) S_{2}^{2}\right]$' + f'\n = {STDS}\n' +r'오차한계 $e_{%d} = t_{\dfrac{\alpha}{2}}*{s}*\sqrt{\dfrac{1}{n} + \dfrac{1}{m}}$' % ((1-  trust*2)*100 ) +f'= {E} \n\n' + r'T = $\dfrac{\overline{X} - \overline{Y} - ({\mu}_{1} - {\mu}_{2})}{s_{p} * \sqrt{\dfrac{1}{n} + \dfrac{1}{m}}}$ ~ t(n+m-2)'  ,fontsize=15)

plt.annotate('' , xy=(0, .25), xytext=(1.5 , .25)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.6 , .25, r'$P(t_{%.3f}<T<t_{%.3f})$' % (trust , 1-trust) + f'= {area}\n' + r'신뢰구간 = (MEANS -$e_{\alpha}$ , MEANS + $e_{\alpha}$)' +f'\n' + r' = $({%.4f} - {%.4f} , {%.4f} + {%.4f})$' % (MEANS, E , MEANS , E)  +f'\n' +r'$ = ({%.4f} , {%.4f})$' % (MEANS-E , MEANS+E)  ,fontsize=15)

ax.vlines(x = t_r ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_r) , colors = 'black')
ax.vlines(x = t_l ,ymin=0 , ymax= scipy.stats.t(dof_2).pdf(t_l) , colors = 'black')




plt.annotate('' , xy=(t_r, .007), xytext=(2.5 , .1)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(t_l, .007), xytext=(-3.5 , .1)  , arrowprops = dict(facecolor = 'black'))
ax.text(1.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_r + '\n' +r'$\dfrac{\alpha}{2}$ =' + f'{round(float(1- scipy.stats.t(dof_2).cdf(t_r)),3)}',fontsize=15)
ax.text(-3.71 , .13, r'$t_{\dfrac{\alpha}{2}} = {%.4f}$' % t_l + '\n' +r'$\dfrac{\alpha}{2}$ =' +f'{round(float(scipy.stats.t(dof_2).cdf(t_l)),3)}',fontsize=15)

ax.text(t_r - 1 , 0.02 , r'$t_r$' + f'= {t_r}'  , fontsize = 13)
ax.text(t_l + .2 , 0.02 , r'$t_l$' + f'= {t_l}'  , fontsize = 13)




#==================================== 가설 검정 ==========================================



t_1 = round((MEANS )/ (STDS  *math.sqrt((1/n_A) + (1/n_B))),4)

print(t_1)
t_1 = abs(t_1)
area = round(float(scipy.stats.t(dof_2).cdf(-t_1) + 1 - (scipy.stats.t(dof_2).cdf(t_1))),4)
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_1) | (X<=-t_1) , facecolor = 'skyblue') # x값 , y값 , 0 , X조건 인곳 , 색깔
ax.fill_between(X, scipy.stats.t(dof_2).pdf(X) , 0 , where = (X>=t_r) | (X<=-t_r) , facecolor = 'red') # x값 , y값 , 0 , X조건 인곳 , 색깔

ax.vlines(x= t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))
ax.vlines(x= -t_1, ymin= 0 , ymax= stats.t(dof_2).pdf(-t_1) , color = 'green' , linestyle ='solid' , label ='{}'.format(2))

annotate_len = stats.t(dof_2).pdf(t_1) /2
plt.annotate('' , xy=(t_1, annotate_len), xytext=(-t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
plt.annotate('' , xy=(-t_1, annotate_len), xytext=(t_1/2 , annotate_len)  , arrowprops = dict(facecolor = 'black'))
ax.text(-1.5 , annotate_len+0.03 , f'P-value : \nP(T<={-t_1}) + P(T>={t_1}) \n = {area}',fontsize=15)

# mo = '모평균'
#
# ax.text(-4.6 , .22, r'T = $\dfrac{\overline{X} - {\mu}}{\dfrac{s}{\sqrt{n}}}$' + f'= { t_1 }' ,fontsize=15)






b = ['t-(n={})'.format(i) for i in dof_2]
plt.legend(b , fontsize = 15)

모평균 차에 대한 소표본 양측 검정

H_0 : m_A = m_B (양측 검정)

 

p-value : 0.0992

 

alpha : 0.05

 

p-value > alpha ==> 0.0992 >  0.05 ==> 귀무가설 H_0 : m_A = m_B  채택한다. 즉 , 유의수준 5%에서 두 회사의 카페인의 양이 같다.

 

 

728x90
반응형

+ Recent posts