Noticed some nan's were appearing unexpectedly, in my data. (and expanding out and naning everything they touched) Did some careful investigation and produced a minimal working example:
>>> import numpy
>>> from scipy.special import expit
>>> expit(709)
1.0
>>> expit(710)
nan
Expit is the inverse logit. Scipy documentation here.
Which tells us:
expit(x) = 1/(1+exp(-x))
So 1+exp(-709)==1.0
so that expit(709)=1.0
Seems fairly reasonable, rounding exp(-709)==0
.
However, what is going on with expit(710)
?
expit(710)==nan
implies that 1+exp(-710)==0
, which implies: exp(-710)=-1
which is not right at all.
What is going on?
I am fixing it with:
def sane_expit(x):
x = np.minimum(x,700*np.ones_like(x)) #Cap it at 700 to avoid overflow
return expit(x)
But this is going to be a bit slower, because extra op, and the python overhead.
I am using numpy 1.8.-0, and scipy 0.13.2