2020年5月24日日曜日

正規分布と$\chi^2$分布の関係

講義で使える統計素材」シリーズ.今回は,正規分布に従う独立な確率変数の二乗和が$\chi^2$分布に従うという事を説明するための図です.

In [1]:
import numpy as np
import matplotlib.pyplot as plt

標準正規分布に従う乱数を発生

In [2]:
n = 100000
x = np.random.normal(0, 1, n)
In [3]:
y=np.arange(0, n, 1)
plt.plot(x,y,',')

ヒストグラムで分布を確認

In [4]:
# ヒストグラムを出力
plt.hist(x, bins=100)
#plt.hist(x, bins=100, density=True)
plt.show()

自由度4のχ2分布

In [5]:
x1 = np.random.normal(0, 1, n)
x2 = np.random.normal(0, 1, n)
x3 = np.random.normal(0, 1, n)
x4 = np.random.normal(0, 1, n)
In [6]:
chi_sqr = x1*x1 + x2*x2 + x3*x3 + x4*x4
plt.hist(chi_sqr, bins=100, density=True)
plt.show()