After we discussed logarithm (‘log’) last week, we explored a bit on some commonly used methods that have log embedded in them. For example, the logit function, or logit transform (using the “natural” logarithm). We explained its definition by the following Python code.
>>> def logit(c):
>>> d = np.log((c+epsilon)/(1+ epsilon-c))
>>> return d
The following is the inverse, which is to bring what was transformed back to what it was before.
>>> def inverse_logit(a):
>>> b = ((1+ epsilon)*np.exp(a) – epsilon)/(np.exp(a)+1)
>>> return b
>> print(logit(0.1)) #-2.1883847407670785
>>> print(inverse_logit(logit(0.1))) #0.09999999999999999
It is much more revealing on what the logit transform is doing by looking at some pictures of how this works. See how fast when it is transformed! Why it is stretched instead of being shrunk? We know that taking log is to do division multiple times (recall log10 of a number is how many times it needs to divide by 10 in order to become 1). But when it applies to numbers between 0 and 1, it gives us the opposite effect. A positive small number less than one has to divide by 10 negative times to become 1. For example, 0.01 needs to be divided by 10 negative two times to be restored to 1. That’s why you see that y axis we have negatives.
On the other hand, we also have positives in the y-axis. That’s because about half of the numbers
(c+epsilon)/(1+ epsilon-c) (the odds) are large positive numbers. Play around with it and you will surely get it.
>> x = np.linspace(0,1,1000)
>>> y = logit(x)
>>> plt.scatter(x=x, y=y, alpha=0.3)
>>> title =”logit transform”
>>> plt.xlabel(“numbers between 0 and 1 (inclusive”)
>>> plt.ylabel(“after logit transform”)
>>> plt.xlim(-6, 6)
>>> plt.ylim(-6, 6)
>>> plt.gca().set_aspect(‘equal’, adjustable=’box’)
Look at the same plot with the axis scaled differently: