Foundations to Frontier
Tests
D. Classical Machine Learning › D2 Linear Classifiers

Logistic Regression (Binary)

Logistic regression models the probability of a binary outcome as the logistic (sigmoid) function of a linear score. It is fitted by maximum likelihood, which means minimising the log loss, a convex problem solved by Newton's method (IRLS) or gradient descent. Its coefficients are log-odds ratios, its probabilities are usually well calibrated, and it is the default baseline classifier in science and industry. This page covers the model, odds and log-odds, the likelihood, its gradient and Hessian, Newton/IRLS, standard errors, the separation problem, decision thresholds, and how it relates to linear regression, the perceptron and GLMs. In the exercises you implement logistic regression by Newton's method and gradient descent, with standard errors and a demonstration of separation; then you predict thruster-valve failures and compare it with the linear probability model and a Platt-scaled perceptron on log loss, Brier score and calibration.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

From a score to a probability

The perceptron answers yes or no. Often you need how likely: “this valve has a 30% chance of failing by 20 000 cycles.” Logistic regression computes a linear score, as before, and then squashes it through an S-shaped curve, the sigmoid σ(t) = 1/(1 + e⁻ᵗ). This turns any number into a probability between 0 and 1. The weights are chosen to make the observed outcomes as likely as possible.

Logistic regression: an S-curve for probabilities
00.51
mean log loss 0.3427 · 50% point at 15.0 thousand cycles · odds multiply by 1.22 per thousand cycles
Orange dots (top): valves that failed; blue (bottom): survived. The curve is P(fail) = σ(β₀ + β₁x); the green dashed line marks where it crosses 50%, the decision boundary. Maximum likelihood chooses the curve with the lowest log loss.
Why engineers care

Risk assessment runs on probabilities: probability of failure in qualification tests, of a conjunction (collision), of a sensor fault. Logistic regression gives calibrated probabilities, interpretable effects (“each 1000 cycles multiplies the odds of failure by 1.28”) and confidence intervals. It is the first model to try for any binary outcome.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Logistic regression outputs…
  2. 2. σ(0) equals…
  3. 3. In the demo, separable data make the fitted slope…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Odds, log-odds and one Newton step

Odds are p/(1 − p): a 75% chance is odds of 3 (3 to 1). Log-odds (the logit) are log(p/(1 − p)). Logistic regression says that the log-odds are linear in x:

log(p/(1 − p)) = β₀ + β₁x.

With β₁ = 0.25 per thousand cycles, each extra 1000 cycles multiplies the odds by e^0.25 = 1.28.

Fitting by hand (intercept only). Outcomes y = 1, 0, 0, 1, 1. Start at β = 0, so p = 0.5 for everyone.

  • The gradient of the log-likelihood is Σ(yᵢ − p) = 3 − 2.5 = 0.5.
  • The curvature is Σp(1 − p) = 5 × 0.25 = 1.25.
  • The Newton step is 0.5/1.25 = 0.4, so β = 0.4.
  • The exact answer is logit(3/5) = log 1.5 = 0.405. One more step gets there.

The steps in plain English:

  1. Compute a score for each example: β₀ + β₁x₁ + ….
  2. Turn it into a probability with the sigmoid.
  3. Measure the fit by the log loss: −log p for actual failures, −log(1 − p) for survivors, averaged.
  4. Adjust β to reduce the log loss (Newton’s method converges in a handful of steps).
  5. Predict “fail” when p exceeds a threshold: 0.5 by default, lower when misses are costly.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Odds for p = 0.8?
  2. 2. Odds ratio for a coefficient of ln 2?
  3. 3. The intercept-only MLE for 3 successes out of 5 is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Maximum likelihood

Model: yᵢ ~ Bernoulli(pᵢ) with pᵢ = σ(xᵢᵀβ). This is a GLM (D1.15) with logit link and V(μ) = μ(1 − μ).

Log-likelihood: ℓ(β) = Σ [yᵢ log pᵢ + (1 − yᵢ) log(1 − pᵢ)]. Its negative mean is the log loss (cross-entropy).

Gradient and Hessian: ∇ℓ = Xᵀ(y − p) and ∇²ℓ = −XᵀWX with W = diag(pᵢ(1 − pᵢ)). The Hessian is negative definite, so ℓ is concave and any stationary point is the global maximum.

Newton / IRLS: β ← β + (XᵀWX)⁻¹Xᵀ(y − p), usually 5–10 steps. Gradient descent (or SGD for large data) uses only Xᵀ(p − y)/n.

At the optimum the score equations Xᵀ(y − p̂) = 0 hold. With an intercept, the mean predicted probability equals the observed rate.

Inference. Cov(β̂) ≈ (XᵀŴX)⁻¹, giving Wald z-tests and confidence intervals. Likelihood-ratio tests compare nested models.

Decision threshold. If a false positive costs c_FP and a false negative c_FN, predict 1 when p > c_FP/(c_FP + c_FN) (D0.9).

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(penalty=None, solver="lbfgs").fit(X, y)       # unpenalised MLE
clf.predict_proba(X_new)[:, 1], clf.coef_, clf.intercept_
import statsmodels.api as sm; sm.Logit(y, sm.add_constant(X)).fit().summary()   # SEs, z-tests

Note that scikit-learn’s default adds an L2 penalty (C = 1). Use penalty=None for the textbook MLE.

Use it when you need probabilities, interpretable effects and a strong linear baseline. Watch out for separation (no finite MLE), strong collinearity, and non-linear effects (add features or splines).

🐍 Exercise 1: logistic regression by Newton and gradient descent
Implement a stable sigmoid, the log loss, logistic regression by Newton's method (IRLS) and by gradient descent, and standard errors; recover known coefficients, check the score equations and that the mean prediction equals the observed rate, and show that on perfectly separated data the coefficients grow without bound.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The gradient of the log-likelihood is…
  2. 2. The log-likelihood of logistic regression is…
  3. 3. scikit-learn's LogisticRegression by default…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Theory and pitfalls

Why the sigmoid? Three views give the same model:

  1. GLM: the logit is the canonical link of the Bernoulli distribution.
  2. Latent variable: y = 1[xᵀβ + ε > 0] with ε logistic-distributed. Gaussian ε gives probit (D2.30).
  3. Generative: if x | y is Gaussian with a shared covariance (LDA, D2.28) or exponential-family with shared dispersion, then P(y | x) is exactly logistic in x.

Separation. If some hyperplane separates the classes perfectly, ℓ(β) keeps increasing as ‖β‖ → ∞ and no finite MLE exists (Albert and Anderson, 1984). Symptoms are huge coefficients and standard errors, and warnings about non-convergence. Fixes:

  • regularisation (D2.24);
  • Firth’s bias-reduced likelihood;
  • Bayesian priors.

Quasi-separation (only some points perfectly predicted) causes the same problem for some coefficients.

Calibration. The MLE is calibrated in the sense that p̂ matches the outcome rate on average (score equations). If the model is correctly specified, it is calibrated conditionally too. Under misspecification it can still be miscalibrated; check with reliability diagrams (D0.10).

Compared with neighbours:

  • Linear probability model (OLS on 0/1): same linear score, but probabilities can leave [0, 1] and errors are heteroscedastic. Its coefficients are often similar near p = 0.5.
  • Perceptron: minimises a non-smooth surrogate and gives no probabilities.
  • SVM: hinge loss, margin and sparsity in the support vectors, but no probabilities without Platt scaling.

Interview questions.

  • Why not use squared error with a sigmoid? The objective becomes non-convex, with flat regions and weak gradients when the model is confidently wrong. Log loss gives the convex likelihood.
  • What does a coefficient of 0.7 mean? A unit increase multiplies the odds by e^0.7 ≈ 2.0, other features held fixed.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Complete separation causes…
  2. 2. Gaussian classes with shared covariance give P(y | x) that is…
  3. 3. Firth's method addresses…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Predicting valve failures

History.

  • Verhulst (1838) introduced the logistic curve for population growth.
  • Berkson (1944) proposed the “logit” model for bioassay.
  • Cox (1958) developed logistic regression for binary sequences.
  • Nelder and Wedderburn (1972) placed it within GLMs.
  • Logistic regression became the workhorse of epidemiology, credit scoring and click-through prediction. It is also the output layer (sigmoid plus cross-entropy) of every binary neural classifier.

Exercise 1. n = 2000, true β = (−0.5, 1.5, −1.0). Newton’s method reached β̂ = (−0.441, 1.457, −1.018) in 7 steps, with standard errors of about 0.06–0.08. Gradient descent agreed to within 10⁻³. The odds ratio for x₁ is 4.29. On four perfectly separated points, ‖β‖ grew from 4.8 to 19.8 as the iterations increased: there is no finite MLE.

Exercise 2: thruster-valve failures. Setup: the true model is logit P = −6 + 0.25·cycles + 0.04·(T − 20), with a failure rate of about 33%. Train on 400 valves; average over 30 campaigns, scored on 4000 test valves.

Method Log loss Brier ECE Accuracy
Logistic regression 0.339 0.106 0.026 0.850
Linear probability model (OLS on 0/1) 0.370 0.122 0.081 0.849
Averaged perceptron + Platt scaling 0.339 0.107 0.026 0.850
  • The ranking of valves is the same for all three methods (identical accuracy). The probabilities are not.
  • The linear probability model puts 18% of its predictions outside [0, 1] and is 3 times worse calibrated.
  • A linear score plus a fitted sigmoid equals logistic regression here. The perceptron found nearly the same direction, and Platt scaling is itself a one-feature logistic regression.
🐍 Exercise 2 (research): probabilities of valve failure
Simulate thruster-valve qualification tests with failure probability logistic in cycles and temperature; compare logistic regression, the linear probability model and a Platt-scaled averaged perceptron by test log loss, Brier score, expected calibration error and accuracy over 30 campaigns.

Logistic regression in AI, robotics and aerospace

  • Reliability and qualification: probability of failure versus cycles, temperature and vibration.
  • Risk models: conjunction (collision) alerts, anomaly flags and go/no-go criteria.
  • Neural networks: the sigmoid output with binary cross-entropy in every binary classifier.
  • Calibration layer: Platt scaling is logistic regression on a model’s scores.

References

  1. D. R. Cox (1958). “The regression analysis of binary sequences.” JRSS B 20(2).
  2. J. Berkson (1944). “Application of the logistic function to bio-assay.” JASA 39(227).
  3. A. Albert, J. A. Anderson (1984). “On the existence of maximum likelihood estimates in logistic regression models.” Biometrika 71(1).
  4. D. Firth (1993). “Bias reduction of maximum likelihood estimates.” Biometrika 80(1).
  5. D. W. Hosmer, S. Lemeshow, R. X. Sturdivant (2013). Applied Logistic Regression, 3rd ed. Wiley.