Sub topic 3
From probability to odds to log-odds
This chapter assumes you know basic probability (events and complements), logarithms and exponentials, and the idea of a linear model such as w·x + b. Here we build the quantity that logistic regression models as a linear function. The next chapter turns it into the sigmoid.
The problem this chapter solves
A linear score z = w·x + b can be any real number, from very negative to very positive. A probability must stay between 0 and 1. We need a transformation that maps the interval (0, 1) onto the whole real line. Then a linear score can sit on that line without ever producing an impossible probability.
We get there in two steps: probability to odds, then odds to log-odds.
Step 1: probability to odds
The odds of an event with probability p are the probability that it happens divided by the probability that it does not:
odds = p / (1 - p)If p = 0.75, the odds are 0.75 / 0.25 = 3. People say "3 to 1 in favor". If p = 0.5, the odds are 1 ("even odds"). If p = 0.2, the odds are 0.25, or 1 to 4.
Odds fix one half of the problem. As p goes from 0 to 1, odds go from 0 to infinity. The upper bound is gone, but odds are still never negative.
Odds and probability carry the same information. You can recover p with p = odds / (1 + odds).
Step 2: odds to log-odds
Taking the natural logarithm of the odds removes the lower bound:
logit(p) = ln(p / (1 - p))This function is called the logit. Odds below 1 (event less likely than not) give negative log-odds. Odds of exactly 1 give 0. Odds above 1 give positive log-odds.
Values worth memorizing
| Probability p | Odds | Log-odds |
|---|---|---|
| 0.10 | 0.111 | -2.197 |
| 0.25 | 0.333 | -1.099 |
| 0.50 | 1.000 | 0.000 |
| 0.75 | 3.000 | 1.099 |
| 0.90 | 9.000 | 2.197 |
| 0.99 | 99.000 | 4.595 |
Two patterns matter later.
- The logit is antisymmetric around 0.5. Swapping an event with its complement flips the sign:
logit(1 - p) = -logit(p). The rows for 0.10 and 0.90 show this. - The logit stretches the ends. Moving from 0.90 to 0.99 adds only 0.09 of probability but adds about 2.4 units of log-odds. Near 0 and 1, small changes in probability correspond to large changes in the score.
Checking it in code
import numpy as np
def odds(p):
return p / (1 - p)
def logit(p):
return np.log(odds(p))
def inverse_logit(z):
return 1.0 / (1.0 + np.exp(-z))
p = np.array([0.10, 0.25, 0.50, 0.75, 0.90, 0.99])
print(np.round(odds(p), 3))
print(np.round(logit(p), 3))
print(np.allclose(logit(1 - p), -logit(p)))
print(np.allclose(inverse_logit(logit(p)), p))The first two lines reproduce the table. Both checks print True. The last one shows that the logit can be undone, which is what lets a model go from a score back to a probability. The function inverse_logit is the sigmoid, which the next chapter derives properly.
Why log-odds is the right scale to model
Suppose we assume the log-odds of the positive class is a linear function of the features:
ln(p / (1 - p)) = w·x + bThis assumption has three useful consequences.
- Any score is valid. Whatever value
w·x + btakes, solving forpgives a number strictly between 0 and 1. - The effect of a feature is additive on the log-odds scale. Increasing feature
x_jby one unit addsw_jto the log-odds, regardless of the other features' values. - The same effect is multiplicative on the odds scale. Adding
w_jto the log-odds multiplies the odds bye^(w_j). This factor is the odds ratio, which the coefficient chapter uses.
A score of 0 means the odds are 1, so p = 0.5. That is why the decision boundary at the default threshold lies where w·x + b = 0.
Edge cases and common errors
- p = 0 or p = 1. The odds become 0 or infinity, and the logit becomes minus or plus infinity. NumPy raises a divide-by-zero warning and returns
-inforinf. When you compute empirical log-odds from data, clip probabilities (for example to[1e-6, 1 - 1e-6]) or add a small count to both outcomes. - Confusing odds with probability. Odds of 3 do not mean a 3% or 30% chance. They mean
p = 0.75. Odds of 0.25 meanp = 0.2, notp = 0.25. - Confusing odds ratio with risk ratio. Doubling the odds does not double the probability. From
p = 0.5(odds 1) to odds 2 givesp = 0.667. The two only agree whenpis small, so the event is rare. - Log base. Logistic regression uses the natural logarithm. Using base 10 or base 2 only rescales the coefficients, but it breaks the
e^(w_j)shortcut for odds ratios. - Reading log-odds as a probability. A log-odds of 2 is a probability of about 0.88, not 2 or 20%. Always convert before reporting.
Exercise
A model outputs log-odds of -1.386 for a customer who may cancel a subscription. What are the odds of cancelling, and what is the probability? Then a feature raises the log-odds by ln(2) ≈ 0.693. What is the new probability?
Show answer
The odds are e^(-1.386) ≈ 0.25, so the probability is 0.25 / 1.25 = 0.20. Adding 0.693 to the log-odds gives -0.693, which multiplies the odds by 2: the new odds are 0.5 and the new probability is 0.5 / 1.5 ≈ 0.333. The probability rose by about 0.13, not by a factor of 2.
Exercise
Why is it a problem to model the probability directly as a linear function of the features, but not to model the log-odds that way?
Show answer
A linear function of probability is unbounded, so for some inputs it will produce values below 0 or above 1, which are not valid probabilities. Log-odds covers the whole real line, so any linear score maps back to a valid probability strictly between 0 and 1.