lundi 7 août 2017

Numba and random numbers from poisson distribution

I found that one of the bottlenecks in my simulations is the generations of random numbers from a poisson distribution. My original code works like this

#Generating some data. In the actual code this comes from the previous
#steps in the simulation. But this gives an example of the type of data
n = 5000000
pop_n = np.array([range(500000)]

pop_n[:] = np.random.poisson(lam=n*pop_n/np.sum(pop_n))

Now, I read that numba can increase the speed very simply. I defined the function

@jit()
def poisson(n, pop_n):
    return np.random.poisson(lam=n*pop_n/np.sum(pop_n))

This one indeed run faster than the original. However, I try to go further :) When I wrote

@jit(nopython=True)
def poisson(n, pop_n):
    return np.random.poisson(lam=n*pop_n/np.sum(pop_n))

I got

 Failed at nopython (nopython frontend)
Invalid usage of Function(np.random.poisson) with parameters     (array(float64, 1d, C))
Known signatures:
 * (float64,) -> int64
 * () -> int64
 * parameterized

Some questions Why is this error happening and how to fix it. Is there a better optimization?




Aucun commentaire:

Enregistrer un commentaire