標本平均から母平均へ

Pythonで学ぶ確率の基礎

Alexander A. Ramírez M.

CEO @ Synergy Vision

標本平均の復習

$$ $$

大数の法則

標本平均は、標本数が増えるほど期待値に近づく。

ヤーコプ・ベルヌーイの切手

Pythonで学ぶ確率の基礎

標本平均の復習(続き)

$$ \text{標本平均} = \bar{X_2} = \frac{x_1+x_2}{2} $$

Pythonで学ぶ確率の基礎

標本平均の復習(続き)

$$ \text{標本平均} = \bar{X_3} = \frac{x_1+x_2+x_3}{3} $$

Pythonで学ぶ確率の基礎

標本平均の復習(続き)

$$ \text{標本平均} = \bar{X_n} = \frac{x_1+x_2+\cdots +x_n}{n} $$

Pythonで学ぶ確率の基礎

標本平均の復習(続き)

$$ \text{標本平均} = \bar{X_n} = \frac{x_1+x_2+\cdots +x_n}{n} \to \mathbb{E(X)} $$

Pythonで学ぶ確率の基礎

標本の生成

# Import binom and describe
from scipy.stats import binom
from scipy.stats import describe

# Sample of 250 fair coin flips
samples = binom.rvs(n=1, p=0.5, size=250, random_state=42)
# Print first 100 values from the sample
print(samples[0:100])
[0 1 1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 0
 0 1 0 0 0 0 1 0 1 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 1 0 0 1
 1 1 1 0 0 0 1 1 0 0 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0]
Pythonで学ぶ確率の基礎

標本平均の計算

# Calculate the sample mean
print(describe(samples[0:10]).mean)
0.6
Pythonで学ぶ確率の基礎

標本平均は期待値へ収束

Pythonで学ぶ確率の基礎

標本平均のアニメーション:期待値へ

Pythonで学ぶ確率の基礎

標本平均のプロット

$$ $$

from scipy.stats import binom
from scipy.stats import describe
import matplotlib.pyplot as plt

# Define our variables
coin_flips, p, sample_size , averages = 1, 0.5, 1000, []
# Generate the sample
samples = binom.rvs(n=coin_flips, p=p, size=sample_size, random_state=42)
Pythonで学ぶ確率の基礎

標本平均のプロット(続き)

$$ $$

# Calculate the sample mean
for i in range(2,sample_size+1):
    averages.append(describe(samples[0:i]).mean)

# Print the first values of averages
print(averages[0:10])
[0.5, 0.6666666666666666, 0.75, 0.6, 0.5, 0.42857142857142855, 0.5, 
0.5555555555555556,0.6, 0.5454545454545454]
Pythonで学ぶ確率の基礎

標本平均のプロット(続き)

$$ $$

# Add population mean line and sample mean plot
plt.axhline(binom.mean(n=coin_flips, p=p), color='red')
plt.plot(averages, '-')
# Add legend
plt.legend(("Population mean","Sample mean"), loc='upper right')
plt.show()
Pythonで学ぶ確率の基礎

標本平均のプロット

標本平均のプロット

Pythonで学ぶ確率の基礎

演習に進みましょう!

Pythonで学ぶ確率の基礎

Preparing Video For Download...