【发布时间】:2017-06-29 20:12:24
【问题描述】:
我有一个基于概率的简单游戏,每天我们掷硬币,如果我们正面朝上,我们赢了,我们得到 20 美元,如果我们抛硬币,我们反面,我们最后损失 19 美元当月(28 天)我们看到我们损失了多少或赚了多少。
def coin_tossing_game():
random_numbers = [random.randint(0, 1) for x in range(500)] #generate 500 random numbers
for x in random_numbers:
if x == 0: #if we get heads
return 20 #we win $20
elif x == 1: #if we get tails
return -19 #we lose $19
for a in range(1, 28): #for each day of the month
print(coin_tossing_game())
这将返回输出 20 20 -19 -19 -19 -19 -19 20 -19 20 -19 20 -19 20 20 -19 -19 20 20 -19 -19 -19 20 20 20 -19 -19 -19 20 20
这个输出正是我所期望的。我想找到输出和其他描述性统计数据的总和,例如平均值、众数、中位数、标准差、置信区间等。我不得不将这些数据复制并粘贴到 Excel 中来进行数据分析。我希望有一种方法可以在 python 中快速轻松地做到这一点。
【问题讨论】:
-
pandas是一个 python 库,它可以完成许多 excel 所做的事情。 -
也许可以查看 scipy stats 模块。 docs.scipy.org/doc/scipy/reference/stats.html
-
作为起点,您可以从函数中输出一个列表,然后使用一些函数在提供的数字列表上计算统计数据(例如均值、标准差等)。它的基本 python 代码(for 循环,基本数学),你不需要一个库来开始使用这些基本统计数据
-
从 Python 3.4 开始,他内置了一个 statistics 模块
标签: python random simulation python-3.5 montecarlo