結果を確認する
それでは、いくつかサンプルを生成し、自分で期待値と分散を計算してみましょう。そのうえで、binom に用意されたメソッドを使って、サンプル値が理論値と一致しているか確かめます。
scipy.stats から binom オブジェクトと describe() メソッドはすでに読み込まれているので、すぐに計算できます。
この演習はコースの一部です
Pythonで学ぶ確率の基礎
演習の手順
- 変数
sampleのサンプル平均とサンプル分散を計算します。 n=10、p=0.3を使って期待値を計算し、分散はmeanと1 - pを用いて計算します。binomのメソッドを使い、p=0.3のコインを10回投げたときの期待値と分散を求めます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
sample = binom.rvs(n=10, p=0.3, size=2000)
# Calculate the sample mean and variance from the sample variable
sample_describe = describe(____)
# Calculate the sample mean using the values of n and p
mean = ____*____
# Calculate the sample variance using the value of 1-p
variance = mean*____
# Calculate the sample mean and variance for 10 coin flips with p=0.3
binom_stats = binom.stats(n=____, p=____)
print(sample_describe.mean, sample_describe.variance, mean, variance, binom_stats)