I need logit and inverse logit functions so that logit(inv_logit(n)) == n
. I use numpy and here is what I have:
import numpy as np
def logit(p):
return np.log(p) - np.log(1 - p)
def inv_logit(p):
return np.exp(p) / (1 + np.exp(p))
And here are the values:
print logit(inv_logit(2))
2.0
print logit(inv_logit(10))
10.0
print logit(inv_logit(20))
20.000000018 #well, pretty close
print logit(inv_logit(50))
Warning: divide by zero encountered in log
inf
Now let's test negative numbers
print logit(inv_logit(-10))
-10.0
print logit(inv_logit(-20))
-20.0
print logit(inv_logit(-200))
-200.0
print logit(inv_logit(-500))
-500.0
print logit(inv_logit(-2000))
Warning: divide by zero encountered in log
-inf
So my questions are: what is the proper way to implement these functions so that the requirement logit(inv_logit(n)) == n
will hold for any n
in as wide a range as possible (at least [-1e4; 1e4)?
And also (and I'm sure this is connected to the first one), why are my function more stable with negative values, compared to the positive ones?