Numba是否支持评估概率密度函数?

时间:2019-02-20 08:50:01

标签: python random distribution numba

Numba supports random sampling of multiple distributions in numpy。但是,当我们要使用scipy评估发行版的PDF时,numba似乎不支持此功能:

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.

除了自己手动定义PDF之外,还有其他解决方法吗?有时我要评估的发行版的PDF非常复杂,例如t分布或beta分布,因此从头开始编写这些代码(特别是如果它们是多变量的话)会很麻烦。

编辑:我知道我可以在不使用纯numpy的for循环的情况下评估PDF:

x = np.linspace(-2,2,10000) # 10000 evaluation points
pdf_eval = stats.norm().pdf(x)

我上面给出的quick_pdf函数只是一个简单的例子。对于我的实际用例,因为有if语句和其他依赖迭代的问题,所以需要for循环。

0 个答案:

没有答案