麻木
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分布,所以我自己从头开始编写它们是很乏味的(特别是当它们是多变量的时候)。
编辑:
x = np.linspace(-2,2,10000) # 10000 evaluation points
pdf_eval = stats.norm().pdf(x)
这个
quick_pdf