从带有替换的 urn 中抽取 2 个数字 - Python

问题描述

我的骨灰盒包含数字 1.3 和 0.9,我想每次模拟绘制 35 次并替换。然后执行最终计算,将结果附加到列表中。 我总共要执行 10000 次模拟。

我的代码如下:

#Draw either 1.3 or 0.9
returns = [1.3,0.9]

#No. of simulations
simulations = 10000

#10000 for loops
for i in range(simulations):
    lst = []

    #each iteration should include 35 random draws with replacement
    for i in range(35):
        lst.append(random.choices(returns,1))
        
    lst = np.array(lst)

#Do final calculation and append solution to list
ret = []
ret.append((prod(lst)^(1/35))-1)

我收到的错误TypeError: 'int' object is not iterable。我明白为什么它不起作用,因为我试图将整数转换为列表对象......但我只是不知道如何解决这个问题?

完整的堆栈跟踪:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-5d61655781f6> in <module>
      9     #each iteration should include 35 random draws with replacement
     10     for i in range(35):
---> 11         lst.append(random.choices(returns,1))
     12 
     13     lst = np.array(lst)

~/opt/anaconda3/lib/python3.7/random.py in choices(self,population,weights,cum_weights,k)
    355                 total = len(population)
    356                 return [population[_int(random() * total)] for i in range(k)]
--> 357             cum_weights = list(_itertools.accumulate(weights))
    358         elif weights is not None:
    359             raise TypeError('Cannot specify both weights and cumulative weights')

TypeError: 'int' object is not iterable

解决方法

如果要将 lst 转换为 numpy 数组,可以改用 numpy.random.choice。这也将消除 for 循环的需要。

import numpy as np
#Draw either 1.3 or 0.9
urn = [1.3,0.9]

#No. of simulations
simulations = 10000

#No. of draws
draws = 35

# simulate the draws from the urn
X=np.random.choice(urn,(draws,simulations))

# print first 10 elements as a check
print(X[1:10])

# print shape as a check
print(X.shape)

输出:

[[1.3 1.3 1.3 ... 0.9 1.3 1.3]
 [0.9 1.3 0.9 ... 0.9 0.9 0.9]
 [0.9 1.3 0.9 ... 1.3 1.3 0.9]
 ...
 [1.3 0.9 0.9 ... 1.3 0.9 0.9]
 [1.3 1.3 1.3 ... 0.9 0.9 1.3]
 [1.3 1.3 0.9 ... 0.9 1.3 1.3]]
(35,10000)

我将退货名称更改为 urn。返回在python中有点混乱。

,

当你打电话时:

random.choices(returns,1)

python 认为 1 对应的是权重,如果对应的是 k 可以选择返回的元素个数,必须这样指定:

random.choices(returns,k=1)

但默认情况下它是1,所以没有必要通知它