Logistic Regression for Yes/No Outcomes
Model yes/no outcomes with the logistic curve, convert between probabilities, odds and log-odds, read odds ratios and judge a confusion matrix.
Welcome back!
Step 1 of 13Learn
Predicting who will cancel
A streaming service wants to predict which subscribers will cancel next month. The outcome is yes or no, coded as 1 for cancel and 0 for stay. An analyst tries ordinary linear regression of the 0/1 outcome on the number of days since the subscriber last watched something.
The line fits the general trend, with more inactive days and more cancellations, but its predictions are nonsense at the edges. For subscribers who watched yesterday, it predicts a probability of −0.08. For those inactive for 90 days, it predicts 1.3. Probabilities must stay between 0 and 1, and the true relationship is not a straight line: going from 0 to 10 inactive days matters much more than going from 80 to 90.
Logistic regression is the standard tool for binary outcomes: churn or stay, buy or not, default or repay, pass or fail, spam or not spam. Instead of a straight line, it fits an S-shaped curve that rises from near 0 to near 1 and never goes past either.
Logistic regression is one of the most widely used models in practice, in credit scoring, medicine, marketing and as the basic building block of neural networks. It uses ideas you already know, a linear combination b₀ + b₁x of predictors, but connects them to probabilities through odds and log-odds. Those two ideas are the key to interpreting it.
Step 2 of 13Learn
Probabilities, odds and log-odds
The odds of an event are the probability it happens divided by the probability it doesn't:
odds = p / (1 − p)
A probability of 0.8 gives odds of 0.8 / 0.2 = 4, often said "4 to 1". A probability of 0.5 gives odds of 1. A probability of 0.2 gives odds of 0.25. Odds run from 0 to infinity.
Take the natural log of the odds and you get the log-odds or logit, which runs from minus infinity to plus infinity: negative when p < 0.5, zero at p = 0.5, positive when p > 0.5.
Logistic regression models the log-odds as a linear function of the predictors:
ln( p / (1 − p) ) = b₀ + b₁x
Solving for p gives the logistic function:
p = 1 / (1 + e^−(b₀ + b₁x))
which produces the S-curve. However large or small b₀ + b₁x gets, p stays between 0 and 1.
Example: a churn model has b₀ = −4 and b₁ = 0.8, with x in weeks of inactivity. At x = 5, the log-odds are −4 + 4 = 0, so p = 0.5. At x = 7, the log-odds are 1.6, and p = 1 / (1 + e^−1.6) ≈ 0.83. The curve is steepest around p = 0.5, where each extra week changes the probability the most.
The coefficients are estimated by maximum likelihood rather than least squares: they are the values that make the observed outcomes most probable.
Step 3 of 13Practice · Chart
Odds for a few probabilities
Tap the probability where the odds are exactly 1 (even).
Tap the bar that answers the question, then press Check.
Step 4 of 13Explore
The logistic curve
Change the intercept and slope to see how the S-curve shifts and steepens. The readouts show where the predicted probability crosses 50% and the odds ratio for a one-unit increase in x.
- x where probability = 50%
- 5
- Odds ratio per one-unit increase (eb₁)
- 2.23
- Probability at x = 0
- 0.018
Step 5 of 13Practice · Work it out
A customer has a 0.8 probability of renewing a subscription. What are the odds of renewal?
Step 6 of 13Learn
Interpreting coefficients as odds ratios
Because the model is linear in the log-odds, b₁ is the change in log-odds for a one-unit increase in x. That isn't intuitive, so analysts exponentiate it. The quantity eb₁ is the odds ratio: the factor by which the odds are multiplied for each one-unit increase in x, holding other predictors constant.
In the churn model, b₁ = 0.8, so e0.8 ≈ 2.23. Each extra week of inactivity multiplies the odds of cancelling by about 2.23, a 123% increase in the odds.
Read odds ratios this way:
- Odds ratio above 1: higher x, higher odds of the outcome.
- Odds ratio below 1: higher x, lower odds. An odds ratio of 0.7 means 30% lower odds per unit.
- Odds ratio of exactly 1: no association.
The odds ratio is constant, but the change in probability is not. Multiplying odds by 2.23 moves a probability from 0.02 to about 0.04, but from 0.50 to about 0.69. That is why you shouldn't say "each week increases the probability of churn by 123%". To communicate effects on probabilities, compute predicted probabilities for meaningful values, such as "a subscriber inactive for 2 weeks has an 8% predicted chance of cancelling; one inactive for 6 weeks, 69%".
For a 0/1 predictor such as "on the annual plan", eb compares the odds for the two groups directly.
Step 7 of 13Practice · Work it out
Using the churn model with log-odds = −4 + 0.8 × (weeks inactive), what is the predicted probability of cancelling for a subscriber inactive for 7 weeks? Give a percentage rounded to the nearest whole number.
Step 8 of 13Practice · Choose one
In a logistic regression predicting loan default, the coefficient for "number of late payments in the past year" is 0.4, so the odds ratio is e0.4 ≈ 1.49. What does this mean?
Step 9 of 13Learn
From probabilities to decisions
A logistic model outputs probabilities. To act, such as sending a retention offer, you usually pick a threshold: flag customers whose predicted probability exceeds, say, 0.5. Comparing flags with what actually happened gives a confusion matrix:
| Actually cancelled | Actually stayed | |
|---|---|---|
| Flagged | True positives | False positives |
| Not flagged | False negatives | True negatives |
From it come the key measures:
- Accuracy: share of all predictions that are correct.
- Precision: share of flagged customers who actually cancelled.
- Recall (sensitivity): share of actual cancellers who were flagged.
Accuracy is misleading when the outcome is rare. If only 3% of subscribers cancel, a model that flags nobody is 97% accurate and completely useless. Precision and recall matter more.
The threshold is a business choice, not a statistical one. Lowering it catches more cancellers (higher recall) but flags more people who would have stayed (lower precision). If a retention offer is cheap and losing a customer is expensive, a low threshold makes sense. If each intervention is costly, raise it.
The ROC curve shows the trade-off across all thresholds, and the area under it (AUC) summarizes how well the model ranks positives above negatives: 0.5 is random guessing, 1.0 is perfect ranking.
Step 10 of 13Practice · Chart
Moving the threshold trades recall for precision
Retention offers are cheap and losing a customer is costly. Tap the best threshold of these.
Tap the point that answers the question, then press Check.
| Threshold | Recall | Precision |
|---|---|---|
| 0.1 | 95% | 20% |
| 0.2 | 88% | 28% |
| 0.3 | 80% | 36% |
| 0.4 | 70% | 45% |
| 0.5 | 58% | 54% |
| 0.6 | 45% | 62% |
| 0.7 | 32% | 70% |
| 0.8 | 20% | 78% |
| 0.9 | 10% | 85% |
Step 11 of 13Note
Step 12 of 13Practice · What would you do?
What would you do?
An analyst must model whether each online visitor makes a purchase (yes or no) from time on site, pages viewed and traffic source. A colleague suggests ordinary linear regression on the 0/1 purchase variable because it is simpler. What do you recommend?
Step 13 of 13Practice · True or false
If only 2% of transactions are fraudulent, a model that labels every transaction as legitimate achieves 98% accuracy.
Lesson complete!