2020年5月16日土曜日

様々な確率(密度)分布の分布

講義で使える統計素材」シリーズ.今回は,統計学で登場する様々な確率密度関数の関係を説明するために作ったグラフ達.ここの図の簡易版を作ろうとした.

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

棒グラフ

In [2]:
left = np.array([1, 2, 3, 4, 5, 6])
height = np.array([1/6, 1/6, 1/6, 1/6, 1/6, 1/6])
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.bar(left, height)

plt.savefig('plot_out.svg')

ベルヌーイ試行の確率

In [3]:
x = np.array(["0", "1"])
y = np.array([0.4, 0.6])
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.bar(x, y)

plt.savefig('plot_out.svg')

2項分布

In [4]:
p = 0.4
x = np.arange(0,8, 1)
n = 8
y = st.binom.pmf(x, n, p)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.bar(x, y)

plt.savefig('plot_out.svg')

ポアソン分布

In [5]:
p = 0.4
x = np.arange(0,8, 1)
n = 8
l = n*p
y = st.poisson.pmf(x, l)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.bar(x, y)

plt.savefig('plot_out.svg')

正規分布

In [6]:
sigma = n*p*(1-p)
mu = 3.2

x = np.arange(0.0, 8.0, 0.01)
y = st.norm.pdf(x, mu, sigma)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.plot(x, y)
plt.fill_between( x, y )

plt.savefig('plot_out.svg')

標準正規分布

In [7]:
sigma = 1.0
mu = 0.0

x = np.arange(-4.0, 4.0, 0.01)
y = st.norm.pdf(x, mu, sigma)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.plot(x, y)
plt.fill_between( x, y )

plt.savefig('plot_out.svg')

混合正規分布(任意分布として)

In [8]:
sigma = n*p*(1-p)
mu = 3.2

x = np.arange(0.0, 8.0, 0.01)
y = (st.norm.pdf(x, 1.0, 1.0) + st.norm.pdf(x, 5.0, 0.5) + st.norm.pdf(x, 4.0, 0.2))/3.0
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.plot(x, y)
plt.fill_between( x, y )

plt.savefig('plot_out.svg')
In [9]:
sigma = 1.0
mu = 0.0

x = np.arange(-4.0, 4.0, 0.01)
y = st.t.pdf(x, df=1)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.plot(x, y)
plt.fill_between( x, y )

plt.savefig('plot_out.svg')

カイ二乗分布

In [10]:
x = np.arange(0.0, 4.0, 0.01)
y = st.chi2.pdf(x, 3)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.plot(x, y)
plt.fill_between( x, y )

plt.savefig('plot_out.svg')

F分布

In [11]:
# 分子、分母の自由度
dfn, dfd = 3, 10
x = np.arange(0.0, 4.0, 0.01)
y = st.f.pdf(x, dfn, dfd)
plt.figure(figsize=(4,4))
plt.ylim(0, 1)
plt.plot(x, y)
plt.fill_between( x, y )

plt.savefig('plot_out.svg')