mercredi 15 septembre 2021

How to pass an array of random numbers to random.choice while defining a function?

I am trying to a write a python code to calculate randomly stopped sum, but encounter problem when size of the randomly generated numbers is going beyond 5.

MemoryError: Unable to allocate 15.5 PiB for an array with shape (49, 53, 43, 46, 52, 53, 57, 52, 52) and data type float64

in the following code:

#Create a function tn.func to calculate randomly stopped sum
import numpy as np
#Define a function
def tn_fun(n):
    return sum(np.random.choice([50, 100, 200], n, replace=True, p=[0.3, 0.5, 0.2]))
N = np.random.poisson(50, 10)
# #Generate 10000 random values of N, using lambda = 50
TN = tn_fun(N)
print('Sample mean of the randomly stopped sum TN is',np.mean(TN))
print('Sample variance of the randomly stopped sum TN is', np.var(TN))



Aucun commentaire:

Enregistrer un commentaire