1

我正在尝试使用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?或者有其他方法可以解决这个问题吗?

4

1 回答 1

1

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

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

除了将 mpf 向下转换为常规浮点数之外,您不能使非 mpmath 方法与 mpf 对象一起使用。它们的底层计算例程设计为以固定精度(通常在 Fortran 中)工作,并且不知道如何处理 mpf。这就是mpmath 重新实现 SciPy 中已经存在的数学函数的原因。

于 2017-01-18T03:55:20.780 回答