I'm trying to use the mpmath
library, which provides arbitrary precision arithmetic, and the scipy.stats
library together:
from mpmath import mpf
from scipy.stats import norm
x = mpf(3) # arbitrary precision float
y = norm.cdf(x)
However, norm.cdf
internally checks if its input is a number by calling np.isnan(x)
. Therefore, I get the folloing error:
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''
Is there a way to force scipy.stats.cdf
to use mpmath.isnan
instead of np.isnan
? Or is there another way to solve this issue?
mpmath implements its own methods for the normal distribution: mpdf and ncdf.
from mpmath import ncdf
y = ncdf(x) # returns mpf('0.9986501019683699')
Other than downcasting mpf to regular floats, you can't make non-mpmath methods work with mpf objects. Their underlying computational routines are designed to work in fixed precision (often in Fortran) and have no idea what to do with mpf. This is why mpmath
re-implements the mathematical functions that already exist in SciPy.