从几何分布抽样
Eva 有一枚偏置硬币,正面朝上的概率只有 20%。Eva 反复抛硬币,并记录得到正面所需的抛掷次数。
几何分布非常适合用来建模"得到正面所需的抛掷次数",其中成功率 p 定义为每次抛掷正面朝上的概率。
您的任务是使用几何分布来模拟 Eva 的抛硬币过程,直到得到正面,共进行 10,000 次试验,并记录每次得到正面所需的抛掷次数。然后,您将可视化结果!
以下内容已为您导入:将 seaborn 导入为 sns,pandas 导入为 pd,SciPy 的 stats 模块导入为 st,以及 matplotlib.pyplot 导入为 plt。
本练习是课程的一部分
Python 中的蒙特卡洛模拟
练习说明
- 将
p设为正确的成功概率,这里的"成功"指抛出正面。 - 使用成功概率
p,从几何分布st.geom中抽样 10,000 次。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Set p to the appropriate probability of success
p = ____
# Sample from the geometric distribution 10,000 times
samples = ____
samples_dict = {"nums":samples}
sns.histplot(x="nums", data=samples_dict)
plt.show()