全国选举
本练习将让您体验如何在不同复杂度下对 DGP(数据生成过程)建模。
设想一个有两个政党(Red 和 Blue)的国家进行全国选举。该国有 50 个州,赢得州数最多的政党获得大选胜利。您已知 Red 在每个州获胜的概率 $p$,并希望知道 Red 在全国获胜的概率。
让我们对 DGP 建模以理解分布。假设每个州的选举结果服从参数为 \(p\) 的二项分布,其中 \(0\) 表示 Red 失利,\(1\) 表示 Red 获胜。然后我们模拟多次选举结果。最后,我们可以提出更丰富的问题,例如:Red 赢得少于 45% 州的概率是多少?
本练习是课程的一部分
Python 中的统计模拟
练习说明
- 使用
np.random.binomial(),设定p = probs且n=1,模拟一次选举。将结果赋给election。 - 将
election中 Red 获胜的平均值追加到outcomes。 - 计算
outcomes中 Red 赢得少于 45% 州的比例。将其保存为prob_red_wins,并用它打印结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
outcomes, sims, probs = [], 1000, p
for _ in range(sims):
# Simulate elections in the 50 states
election = ____
# Get average of Red wins and add to `outcomes`
outcomes.append(____)
# Calculate probability of Red winning in less than 45% of the states
prob_red_wins = ____
print("Probability of Red winning in less than 45% of the states = {}".format(____))