0

我的骨灰盒包含数字 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
4

2 回答 2

3

如果要将 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 中返回有点令人困惑。

于 2021-04-08T08:25:48.930 回答
1

你打电话时:

random.choices(returns,1)

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

random.choices(returns,k=1)

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

于 2021-04-08T08:29:10.687 回答