Find a course

Module 5: Beyond the Straight LineLesson 12 of 1215 min

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.

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.

Odds for a few probabilitiesBar chart with 5 categories: p = 0.1: 0.11; p = 0.25: 0.33; p = 0.5: 1; p = 0.75: 3; p = 0.9: 9.0246810p = 0.1p = 0.25p = 0.5p = 0.75p = 0.90.110.33139
Odds run from 0 to infinity; log-odds run from minus to plus infinity, which is what lets a straight line model them.
Odds for a few probabilities
ItemOdds p / (1 − p)
p = 0.10.11
p = 0.250.33
p = 0.51
p = 0.753
p = 0.99

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.

00.20.40.60.81 051015
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 cancelledActually stayed
FlaggedTrue positivesFalse positives
Not flaggedFalse negativesTrue 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.

Moving the threshold trades recall for precisionLine chart with 9 categories: 0.1: Recall 95%, Precision 20%; 0.2: Recall 88%, Precision 28%; 0.3: Recall 80%, Precision 36%; 0.4: Recall 70%, Precision 45%; 0.5: Recall 58%, Precision 54%; 0.6: Recall 45%, Precision 62%; 0.7: Recall 32%, Precision 70%; 0.8: Recall 20%, Precision 78%; 0.9: Recall 10%, Precision 85%.0%20%40%60%80%100%0.10.20.30.40.50.60.70.80.9Threshold
Illustrative churn model.
Moving the threshold trades recall for precision
ThresholdRecallPrecision
0.195%20%
0.288%28%
0.380%36%
0.470%45%
0.558%54%
0.645%62%
0.732%70%
0.820%78%
0.910%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.