检查结果
现在请您自己生成一些样本并计算期望与方差,然后使用 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)