「統計検定2級テキスト」 練習問題解答例 Ch.6 「その他の分析法」

当記事は「統計検定$2$級対応 統計学基礎(東京図書)」の読解サポートにあたって第$6$章の「その他の分析法ー正規性の検討、適合度と独立性の$\chi^2$検定ー」に関して演習問題を中心に解説を行います。適合度検定はよく用いられるので演習を通して抑えておくと良いのではないかと思います。

本章のまとめ

練習問題解説

問$6$.$1$

・$[1]$
$$
\large
\begin{align}
p(x) = \frac{\lambda^{x} e^{-\lambda}}{x!}, \, x=0,1,2,\cdots
\end{align}
$$

上記の数式に基づいて下記を実行することで計算を行える。

import numpy as np
import math

lamb = 2.
x = np.array([0, 1, 2, 3])
p = np.zeros(5)

for i in range(x.shape[0]):
    p[i] = lamb**x[i] * np.e**(-lamb) / math.factorial(x[i])

p[-1] = 1-np.sum(p[:-1])

for i in range(x.shape[0]):
    print("x: {:.0f}, prob: {:.3f}".format(i, p[i]))

print("x: {}, prob: {:.3f}".format("more than 4", p[-1]))

実行結果

x: 0, prob: 0.135
x: 1, prob: 0.271
x: 2, prob: 0.271
x: 3, prob: 0.180
x: more than 4, prob: 0.143

・$[2]$
$[1]$で計算を行ったpを元に$\chi^2$統計量が$\chi^{2}(5-1)$に従うと考えて下記を実行することで計算できる。

from scipy import stats

observed = np.array([21., 32., 29., 12., 6.])
estimated = 100.*p

chi2_stat = np.sum((observed-estimated)**2/estimated)

if chi2_stat > stats.chi2.ppf(1.-0.05, 5-1):
    print("chi2_stat: {:.2f}, c: {:.2f}, reject H0.".format(chi2_stat, stats.chi2.ppf(1.-0.05, 5-1)))
else:
    print("chi2_stat: {:.2f}, c: {:.2f}, accept H0.".format(chi2_stat, stats.chi2.ppf(1.-0.05, 5-1)))

実行結果

chi2_stat: 11.99, c: 9.49, reject H0.

上記より、帰無仮説の「$\lambda=2$のポアソン分布に従う」が棄却される。

・$[3]$
$\lambda$は下記を実行することで推定できる。

x_ = np.array([0, 1, 2, 3, 4])
estimated_lamb = np.sum(x_*observed)/100.
print("Estimated lambda: {:.2f}".format(estimated_lamb))

実行結果

Estimated lambda: 1.50

このとき発生確率は$[1]$と同様に下記のように計算できる。

p2 = np.zeros(5)

for i in range(x.shape[0]):
    p2[i] = estimated_lamb**x[i] * np.e**(-estimated_lamb) / math.factorial(x[i])

p2[-1] = 1-np.sum(p2[:-1])

for i in range(x.shape[0]):
    print("x: {:.0f}, prob: {:.3f}".format(i, p2[i]))

print("x: {}, prob: {:.3f}".format("more than 4", p2[-1]))

実行結果

x: 0, prob: 0.223
x: 1, prob: 0.335
x: 2, prob: 0.251
x: 3, prob: 0.126
x: more than 4, prob: 0.066

・$[4]$
$[3]$で計算を行ったp2を元に、$\chi^2$統計量が$\chi^{2}(5-2)$に従うと考えて下記を実行することで計算できる。

from scipy import stats

observed = np.array([21., 32., 29., 12., 6.])
estimated2 = 100.*p2

chi2_stat2 = np.sum((observed-estimated2)**2/estimated2)

if chi2_stat2 > stats.chi2.ppf(1.-0.05, 5-2):
    print("chi2_stat2: {:.2f}, c: {:.2f}, reject H0.".format(chi2_stat2, stats.chi2.ppf(1.-0.05, 5-2)))
else:
    print("chi2_stat2: {:.2f}, c: {:.2f}, accept H0.".format(chi2_stat2, stats.chi2.ppf(1.-0.05, 5-2)))

実行結果

chi2_stat2: 0.82, c: 7.81, accept H0.

問$6$.$2$

・$[1]$

・$[2]$

・$[3]$

参考

・統計検定$2$級 まとめ