我正在尝试在 PyPy 上评估大量高斯函数的 CDF,但使用 math.erf
执行此操作的速度很慢。
我在 PyPy 上没有 SciPy,所以我无法使用它。不过,我有 NumPy。
除了编写自己的 C 扩展之外,在 PyPy 上执行此操作的最快方法是什么?
最佳答案
如果您有 numpy,您可以将误差函数制成表格,进行插值(np.interp
或自行推出),然后使用 numpy 向量化运算一次评估多个值。
大致(这是在 CPython 上,我不确定 np.interp
在 numpypy 中是否有效),
>>> import math
>>> x = np.linspace(-3, 3, 101) # example only, better use e.g. Chebyshev nodes
>>> y = [math.erf(_) for _ in x ]
>>> xval = [-0.1, 0.4, 0.9]
>>> np.interp(xval, x, y)
array([-0.11237943, 0.42808831, 0.79690821])
关于python - 在 PyPy 上矢量化高斯 CDF(或 erf/erfc/等)的最快方法(即没有 SciPy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639492/