我正在尝试使用提供任意精度算术的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
?或者有其他方法可以解决这个问题吗?
答案 0 :(得分:1)
mpmath为正态分发实现了自己的方法:mpdf and ncdf。
from mpmath import ncdf
y = ncdf(x) # returns mpf('0.9986501019683699')
除了将mpf向下转换为常规浮点数之外,您不能使非mpmath方法与mpf对象一起使用。它们的基础计算程序设计为以固定精度(通常在Fortran中)工作,并且不知道如何处理mpf。这就是mpmath
重新实现SciPy中已存在的数学函数的原因。