一起使用 mpmath 和 scipy.stat

Using mpmath and scipy.stat together

我正在尝试同时使用 mpmath 库和 scipy.stats 库:

from mpmath import mpf
from scipy.stats import norm 

x = mpf(3) # arbitrary precision float
y = norm.cdf(x)

但是,norm.cdf 通过调用 np.isnan(x) 在内部检查其输入是否为数字。因此,我得到以下错误:

Traceback (most recent call last):

File "name of my file", line 5, in <module>
y = norm.cdf(x)

File "C:\Program Files\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 1734, in cdf
place(output, (1-cond0)+np.isnan(x), self.badvalue)

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

有没有办法强制 scipy.stats.cdf 使用 mpmath.isnan 而不是 np.isnan?或者有其他方法可以解决这个问题吗?

mpmath 实现了自己的正态分布方法:mpdf and ncdf

from mpmath import ncdf
y = ncdf(x)         # returns mpf('0.9986501019683699')

除了将 mpf 向下转换为常规浮点数之外,您无法使 non-mpmath 方法与 mpf 对象一起使用。他们的底层计算例程设计为以固定精度(通常在 Fortran 中)工作,并且不知道如何处理 mpf。这就是为什么 mpmath re-implements 中已经存在的数学函数 SciPy.