mercredi 20 février 2019

Numba support for evaluating Probability Density Functions?

Numba supports random sampling of multiple distributions in numpy. However, when we want to evaluate the PDF of a distribution using scipy, numba does not seem to support this:

from numba import njit
from scipy import stats
import numpy as np

@njit
def quick_pdf(n):
    """
    Return the pdf of a standard normal evaluated at multiple different values

    :type n: int
    :param n: number of iterations / values to evaluate PDF at

    :rtype: numpy.ndarray
    """
    out = np.empty(n)
    for i in range(n):
        out[i] = stats.norm().pdf(np.sqrt(i))
    return out

quick_pdf(1000)
>>> TypingError
...
    out[i] = stats.norm().pdf(np.sqrt(i))
    ^
This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.

Is there some way to get around this other than manually defining the PDF myself? Sometimes the PDF of the distribution I want to evaluate is quite complicated e.g. t-distribution or beta distribution, so it would be tedious to code these up myself from scratch (especially if they are multivariate).




Aucun commentaire:

Enregistrer un commentaire