檢查結果
現在試著自己產生一些樣本並計算期望值與變異數,接著使用 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)