Sub topic 3
From probability to odds to log-odds
This chapter assumes you know basic probability and have seen linear regression. The previous chapter showed that a straight line can predict values outside 0 to 1, so it cannot output a probability directly. Here you will change the quantity being modeled so that a linear score does make sense.
The range problem
A linear model computes a score w·x + b that can be any real number. A probability must stay between 0 and 1. Two transformations remove this mismatch, one step at a time.
Odds
The odds of an event compare the chance it happens with the chance it does not:
odds = p / (1 − p)
If p = 0.75, the odds are 0.75 / 0.25 = 3. People say "3 to 1". Odds are not probabilities. They start at 0 when p = 0 and grow without limit as p approaches 1. That removes the upper cap, but the lower bound of 0 remains.
Log-odds
Taking the natural logarithm of the odds removes the lower bound too:
logit(p) = ln(p / (1 − p))
The result can be any real number. It is also symmetric around 0: events with probability p and 1 − p have log-odds of equal size and opposite sign.
| Probability | 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 |
You can reproduce the table with NumPy:
import numpy as np
p = np.array([0.10, 0.25, 0.50, 0.75, 0.90])
odds = p / (1 - p)
log_odds = np.log(odds)
for pi, oi, li in zip(p, odds, log_odds):
print(f"p={pi:.2f} odds={oi:.3f} log-odds={li:+.3f}")The modeling idea
Logistic regression assumes the log-odds of the positive class are a linear function of the features:
ln(p / (1 − p)) = w·x + b
The left side can be any real number, so the right side may be too. Solving for p gives the sigmoid, which the next chapter derives.
Common errors
- Reading odds as probability. Odds of 3 mean a probability of 0.75, not 3.
- Using p = 0 or p = 1. The odds become 0 or infinite, and the log-odds are undefined. Real models never output these exactly. Data that forces them is the "perfect separation" problem covered later.
- Mixing logarithm bases. Logistic regression uses the natural log. Using base 10 changes every number in the table.
Exercise
A model outputs log-odds of -1.386 for an example. What are the odds, and what is the probability of the positive class?
Show answer
The odds are e^(-1.386) ≈ 0.25, which is 1 to 4. The probability is odds / (1 + odds) = 0.25 / 1.25 = 0.2.