統計検定2級 公式問題集 CBT対応版 解答例まとめ 〜9. カイ二乗検定〜

「統計検定$2$級 公式問題集 CBT対応版」の解答例を取りまとめるにあたって、当記事では「PART.$2$ 分野・項目別 問題・解説」のCategory.$9$「カイ二乗検定」の解答例を作成しました。解答例は「統計の森」オリジナルのコンテンツであり、統計検定の公式とは一切関係ないことにご注意ください。

適合度検定

Q.1

ア)
$1$等、$2$等、ハズレの$3$パターンあるので自由度は$3-1=2$である。

イ)
検定統計量の値$\chi^2$は下記のように計算できる。
$$
\large
\begin{align}
\chi^2 &= \frac{(5-0.2 \cdot 50)^2}{0.2 \cdot 50} + \frac{(12-0.3 \cdot 50)^2}{0.3 \cdot 50} + \frac{(5-0.5 \cdot 50)^2}{0.5 \cdot 50} \\
&= \frac{(5-10)^2}{10} + \frac{(12-15)^2}{15} + \frac{(33-25)^2}{25} \\
&= 5.66
\end{align}
$$

ウ)
自由度$2$の$\chi^2$分布$\chi^2(2)$の上側$5$%点は約$5.99$であるので帰無仮説は棄却できない。

上記より、④が正解である。

Q.2

ア)
確率が$p=1/6$のように既知であるので自由度は$6-1=5$である。

イ)
自由度$5$の$\chi^2$分布の上側$5$%点は$\chi^2_{\alpha=0.05}(5)=11.07>2.59$であるので帰無仮説は棄却できない。

上記より③が正解である。

Q.3

ア)
期待度数は夏季と冬季の値を元に下記のように計算できる。
$$
\large
\begin{align}
\frac{42+25}{2} = 33.5
\end{align}
$$

イ)
自由度$(2-1)(5-1)=4$の$\chi^2$分布の上側$5$%点は$\chi^2_{\alpha=0.05}(4)=9.49$であり、$9.49<20.41$であるので帰無仮説は棄却でき、$2$つの分布は同等でないといえる。

以上より④が正解である。

・参考
下記のような計算を実行することで検定統計量の計算や仮説検定を行うことができる。

import numpy as np
from scipy import stats

observed_x = np.array([[18., 42., 24., 11., 1.], [13., 25., 23., 19., 16.]])
expected_x = np.mean(observed_x,axis=0)
expected_x_mat = np.repeat(expected_x,2).reshape([5,2]).T

chi2 = np.sum((observed_x-expected_x_mat)**2/expected_x_mat)

if stats.chi2.ppf(1-0.05,4) < chi2:
    print("chi^2: {:.2f}, reject H_0.".format(chi2))
else:
    print("chi^2: {:.2f}, accept H_0.".format(chi2))

・実行結果

chi^2: 20.51, reject H_0.

Q.4

ア)
自由度は$5-1=4$である。

イ)
自由度$4$の$\chi^2$分布$\chi^2(4)$の上側$5$%点$\chi^2_{\alpha=0.05}(4)=9.49$であり、$9.49<12.0$より帰無仮説は棄却できるので「平均$2$のポアソン分布に従わない」といえる。

以上より、③が正しい。

・参考
下記のような計算を実行することでポアソン分布の確率関数に基づいて期待度数の計算を行うことができる。

import numpy as np
import math

lamb = 2.
prob = np.zeros(5)

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

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

print(prob*100)

・実行結果

[ 13.53352832  27.06705665  27.06705665  18.04470443  14.28765395]

独立性検定

Q.1

ア)
期待度数は下記のように計算できる。
$$
\large
\begin{align}
100 \times \frac{27}{100} \times \frac{49}{100} = 13.23
\end{align}
$$


自由度は$(2-1)(2-1)=1$である。

よって①が正解である。

・参考
自由度$1$の$\chi^2$分布$\chi^2(1)$を用いた検定は下記より行うことができる。

import numpy as np
from scipy import stats

observed = np.array([[19., 30.], [8., 43.]])
expected_p = np.array([[(27.*49.)/(100.**2), (73.*49.)/(100.**2)], [(27.*51.)/(100.**2), (73.*51.)/(100.**2)]])

chi2 = np.sum((observed-expected_p*100.)**2/(expected_p*100.))

if stats.chi2.ppf(1-0.05,1) < chi2:
    print("chi^2: {:.3f}, reject H_0.".format(chi2))
else:
    print("chi^2: {:.3f}, accept H_0.".format(chi2))

・実行結果

chi^2: 6.759, reject H_0.

Q.2

検定統計量の$\chi^2$の値は表$B$の総和の$6.60$である。また、自由度$(2-1)(3-1)=2$の片側$5$%検定を考えるので、$\chi^2_{\alpha=0.05}=5.99 < 6.60$より帰無仮説は棄却される。よって②が正解である。

・参考
表$B$は下記のような計算を行うことで作成できる。

import numpy as np

observed_x = np.array([[5., 10., 15.], [10., 5., 5.]])
expected_x = np.array([[15.*30./50., 15.*30./50., 20.*30./50.], [15.*20./50., 15.*20./50., 20.*20./50.]])

chi2_mat = (observed_x-expected_x)**2/expected_x

print(chi2_mat)

・実行結果

[[ 1.77777778  0.11111111  0.75      ]
 [ 2.66666667  0.16666667  1.125     ]]

Q.3

ア)
期待度数は下記のように計算できる。
$$
\large
\begin{align}
120 \times \frac{90}{120} \times \frac{64}{120} = 48
\end{align}
$$

イ)
下記のような計算を実行することで帰無仮説$H_0が$5$%で棄却できることが確認できる。

import numpy as np
from scipy import stats

observed_x = np.array([[53., 11.], [37., 19.]])
expected_x = np.array([[90.*64./120., 30.*64./120.], [90.*56./120., 30.*56./120.]])

chi2 = np.sum((observed_x-expected_x)**2/expected_x)

if stats.chi2.ppf(1.-0.05,1) < chi2:
    print("chi^2: {:.2f} > {:.2f}, reject H_0".format(chi2, stats.chi2.ppf(1.-0.05,1)))
else:
    print("chi^2: {:.2f} < {:.2f}, accept H_0".format(chi2, stats.chi2.ppf(1.-0.05,1)))

・実行結果

chi^2: 4.46 > 3.84, reject H_0

以上より、③が正解である。

Q.4

自由度$1$の$\chi^2$分布の上側$5$%点は$\chi^2_{\alpha=0.05}(1)=3.84$であり、$3.84<69.04$であるので有意水準$5$%で帰無仮説を棄却し、「風向きと季節には関連がある」といえる。よって⑤が正解である。

・参考
$\chi^2$統計量は下記のように計算できる。

import numpy as np

observed_x = np.array([[105., 15.], [102., 143.]])
n = np.sum(observed_x)
expected_x = np.array([[207.*120./n, 158.*120./n], [207.*245./n, 158.*245./n]])

chi2 = np.sum((observed_x-expected_x)**2/expected_x)

print("chi^2: {:.2f}".format(chi2))

・実行結果

chi^2: 69.03

参考

・【統計検定$2$級対応】統計学入門まとめ
https://www.hello-statisticians.com/stat_basic