728x90
반응형

https://knowallworld.tistory.com/310

 

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

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

knowallworld.tistory.com

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

22. 12세 이하의 어린이가 일주일 동안 TV를 시청하는 시간을 조사하여 다음을 얻었다. 이것을 근거로 남자 어린이와 여자 어린이의 시청시간의 분산의 비에 대한 95% 신뢰구간을 구하라.

A = pd.DataFrame({'표본표준편차' : [2.1 , 2.7] , '표본의 크기' : [8 , 10]})
A.index = ['남자' ,'여자']
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 = 8
m = 10
dof = [[n-1 , m-1]] #자유도

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



sample_x = 17.5
stand_x = 2.1 # 표본표준편차

sample_y = 21.2
stand_y = 2.7

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.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.144 , 2.918)

 

23. 표본조사 결과를 이용하여 두 정규모집단의 모분산의 비에 대한 90% 신뢰구간을 구하라.

A = pd.DataFrame({'표본평균' : [201 , 199] , '표본표준편차' : [6.2 , 5.4] , '표본의 크기' : [6 ,9]})
A.index = ['표본 A' ,'표본 B']
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 = 6
m = 9
dof = [[n-1 , m-1]] #자유도

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



sample_x = 201
stand_x = 6.2 # 표본표준편차

sample_y = 199
stand_y = 5.4

#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.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.357 , 6.352)

 

 

728x90
반응형

+ Recent posts