Logistic Regression
Volume II, Chapter 4 — Part II. Binary classification via the logistic link function: Bernoulli likelihood, cross-entropy loss derivation, gradient structure, multiclass softmax extension, and the connection to neural network output layers.
Prerequisites
Table of Contents
- Learning Objectives
- Prerequisites
- Notation
- Core Intuition
- The Classification Problem
- The Logistic Function and Link
- Bernoulli Likelihood and Cross-Entropy
- Gradient Derivation
- The Hessian and Convexity
- Decision Boundaries and Geometry
- Multiclass Extension: Softmax Regression
- Regularized Logistic Regression
- Connection to Generalized Linear Models
- Worked Examples
- Connection to the Broader Curriculum
- Common Pitfalls and Misconceptions
- Research Perspective
- Summary of Takeaways
- Exercises
Learning Objectives
After reading this chapter, you should be able to:
- Formulate binary classification as estimating via a linear predictor composed with the logistic sigmoid.
- Derive the cross-entropy (logistic) loss from the Bernoulli negative log-likelihood.
- Compute the gradient and explain its resemblance to the linear regression gradient.
- Prove that the logistic loss is strictly convex in , guaranteeing a unique global minimum.
- Characterize the decision boundary as a hyperplane in feature space.
- Extend the framework to -class classification via the softmax function.
- Explain why linear classification remains linear in feature space even though the link function is nonlinear.
Prerequisites
This chapter builds on:
- Linear Regression — design matrix, parameter vector, loss minimization
- Gradient Descent — iterative optimization, convexity
- Bayes' Theorem — conditional probability, odds
Notation
- or — Binary or multiclass labels
- — Logistic sigmoid
- — Multiclass weight matrix
- — Predicted class probability
- — Cross-entropy loss
Core Intuition
Linear regression predicts continuous values; logistic regression predicts probabilities for binary (and, by extension, categorical) outcomes. The key insight is separation of concerns:
- A linear score captures how strongly the features favor class 1.
- A link function maps the score to a valid probability in .
This template — linear predictor plus link function — defines the class of Generalized Linear Models (GLMs). Logistic regression is the GLM for Bernoulli-distributed responses. Every binary classifier in deep learning (the final sigmoid or softmax layer) is a direct descendant.
The loss function changes from squared error to cross-entropy, which penalizes confident wrong predictions harshly. The gradient takes a remarkably clean form — structurally identical to linear regression but with "residuals" replaced by prediction errors in probability space.
Series context. This is Part II of Chapter 4 (Supervised Learning) in Volume II. It follows Linear Regression and precedes Bayesian Linear Regression.
Logistic Regression
75% accThe Classification Problem
Definition 1 (Binary Classification). Given training data with and , learn a function that predicts the label of unseen examples.
We model the conditional probability:
The prediction task reduces to estimating and thresholding (typically at ).
Definition 2 (Linear Score). The logit input (linear predictor) is
We require a function such that .
The Logistic Function and Link
Definition 3 (Logistic Sigmoid). The logistic function (sigmoid) is
Proposition 1 (Properties of ). The logistic function satisfies:
- for all
- (symmetry)
- (derivative in terms of itself)
- ,
Proof of (3). By the quotient rule:
Definition 4 (Log-Odds / Logit). The logit is the inverse of the logistic function:
Logistic regression models log-odds as linear in features:
Important equation. Equation (5) is the defining property of logistic regression: the log-odds are an affine function of features. This is why the model is "linear" despite predicting nonlinear probabilities.
Bernoulli Likelihood and Cross-Entropy
Definition 5 (Bernoulli Distribution). For , the Bernoulli PMF can be written compactly as
Proposition 2 (Negative Log-Likelihood). The negative log-likelihood for a single observation is
Substituting , this is the binary cross-entropy between the true label and predicted probability .
Definition 6 (Empirical Cross-Entropy Loss). Over i.i.d. observations:
where .
Theorem 1 (Cross-Entropy vs. Squared Error). For probability estimation, cross-entropy is the proper scoring rule derived from maximum likelihood. Using squared error on binary labels yields a non-convex loss in and is theoretically unjustified.
Proof sketch. Squared error on gives which can be negative for certain pairs, violating convexity. Cross-entropy's Hessian is positive semidefinite (Theorem 3 below).
Gradient Derivation
We derive the gradient of (8) in complete detail.
Theorem 2 (Logistic Regression Gradient). The gradient of the average cross-entropy loss is
where with , and is the design matrix.
Proof. For a single sample, the loss is . By the chain rule:
where .
Step 1: .
Step 2: by Proposition 1.
Step 3: .
Combining:
The simplification in (11) is the hallmark of exponential-family GLMs: the gradient equals the prediction error. Averaging over samples and stacking into matrix form gives (9).
Important equation. Equation (9) mirrors linear regression's gradient , with predictions replacing linear predictions. This structural unity is why both models are solved by the same gradient descent machinery.
Corollary 1 (Gradient Descent Update).
Unlike linear regression, no closed-form solution exists — appears nonlinearly inside .
The Hessian and Convexity
Theorem 3 (Strict Convexity). The logistic loss is strictly convex in when has full column rank. The Hessian is
where is a positive diagonal matrix.
Proof. From (11), . Differentiating with respect to :
Since for , each is positive semidefinite. Summing: . Strict positive definiteness holds when has full column rank and at least one .
Consequence. Gradient descent (with appropriate step size) converges to the unique global minimum. Newton's method, using , converges quadratically near the optimum — the basis of Iteratively Reweighted Least Squares (IRLS).
Proposition 3 (IRLS). Newton's update for logistic regression is equivalent to solving a weighted least-squares problem at each iteration:
where depends on current predictions and is a "working response."
Decision Boundaries and Geometry
Definition 7 (Decision Boundary). The decision boundary is the set of points where , equivalently where (since ).
Proposition 4. The decision boundary of logistic regression is a hyperplane in (or an affine hyperplane if the bias is included in ).
Proof. defines a hyperplane with normal vector .
Interpretation. Logistic regression is a linear classifier — it can only separate classes with a linear boundary in the original feature space. Nonlinear boundaries require feature transformations (polynomial features, kernels) or deeper models.
Definition 8 (Margin and Confidence). The magnitude measures distance from the boundary in log-odds space. Large positive implies high confidence in class 1; large negative implies high confidence in class 0.
Multiclass Extension: Softmax Regression
Definition 9 (Multinomial Logistic / Softmax Regression). For classes, assign a parameter vector to each class. The softmax function gives class probabilities:
where .
Proposition 5 (Softmax Properties). (i) and ; (ii) Softmax is invariant to adding a constant to all logits; (iii) Binary logistic regression is softmax with .
Definition 10 (Categorical Cross-Entropy). With one-hot labels :
Theorem 4 (Softmax Gradient). The gradient with respect to class- parameters is
The same "prediction minus target" structure persists. This is the loss function used in the output layer of every multi-class neural network.
Regularized Logistic Regression
Definition 11 (L2-Regularized Logistic Regression).
The regularized problem remains convex. The gradient becomes:
where excludes the bias.
Proposition 6 (L1 Regularization). L1 penalty produces sparse models — some feature coefficients are exactly zero, performing feature selection. The loss remains convex but is non-differentiable at zero.
Connection to Generalized Linear Models
Definition 12 (GLM). A Generalized Linear Model specifies:
- Linear predictor:
- Link function: , where
- Exponential family distribution for
For logistic regression: , , .
For linear regression: , , .
The GLM framework unifies the gradient structure: for all exponential-family GLMs with canonical link.
Worked Examples
Example 1: Single-Feature Classification
Suppose (one feature) and we fit . The decision boundary is at . If , larger increases .
Example 2: Gradient at Perfect Separation
When data are linearly separable, the MLE does not exist — drives or for all . Regularization is essential to obtain finite parameter estimates.
Example 3: Softmax with Three Classes
For , logits , , :
The softmax amplifies differences — class 1 dominates despite modest logit separation.
Connection to the Broader Curriculum
- Sigmoid link — Output activation of binary classifiers
- Cross-entropy loss — Standard classification loss
- Softmax — Multi-class output layer
- Linear decision boundary — Last layer before activation; deeper layers learn features
- IRLS / Newton — Second-order optimization methods
- Regularization — Weight decay in all neural networks
Backpropagation extends the gradient computation in (18) through hidden layers. Self-Attention uses softmax over a different axis — attention weights over tokens rather than class probabilities.
Common Pitfalls and Misconceptions
Pitfall 1: Using squared error for classification. MSE on binary labels is theoretically improper and practically non-convex in . Always use cross-entropy for probabilistic classification.
Pitfall 2: Interpreting coefficients as causal effects. is the change in log-odds per unit change in , holding other features fixed. This is an associational, not causal, statement unless additional assumptions hold.
Pitfall 3: Ignoring class imbalance. When , the classifier can achieve high accuracy by always predicting 0. Use balanced metrics (F1, AUC-ROC) and consider class-weighted loss.
Pitfall 4: Assuming nonlinear boundaries. Logistic regression with raw features produces only linear boundaries. Nonlinear problems require feature engineering or deeper architectures.
Pitfall 5: Perfect separation without regularization. Linearly separable data causes the MLE to diverge. Always apply regularization or use a Bayesian treatment (Bayesian Linear Regression extends naturally to classification).
Pitfall 6: Confusing with the gradient. The derivative of the sigmoid appears in backpropagation through hidden layers, but the gradient of the loss with respect to simplifies to at the output layer.
Research Perspective
Logistic regression originated with the work of Barnard (1949) and Cox (1958) on binary response models. The GLM framework (Nelder & Wedderburn, 1972) placed logistic regression within a unified statistical theory.
In machine learning, logistic regression remains the baseline for:
- Calibration studies: assessing whether predicted probabilities match observed frequencies
- Interpretable models in medicine, finance, and policy
- Neural network output layers: the final softmax-with-cross-entropy layer is softmax regression over learned features
Modern research on calibration in deep learning (Guo et al., 2017) shows that large neural networks are often miscalibrated despite high accuracy — temperature scaling, a single-parameter adjustment of the softmax logits, connects directly to the logistic framework.
The logistic loss also appears in contrastive learning and recommendation systems as a pairwise ranking objective, demonstrating the breadth of this simple model.
Summary of Takeaways
- Logistic sigmoid — — Maps scores to probabilities
- Log-odds linearity — — Defining GLM property
- Cross-entropy — — Proper Bernoulli loss
- Gradient — — Same structure as linear regression
- Hessian — — Strict convexity
- Decision boundary — — Linear hyperplane
- Softmax — — Multiclass extension
Next article: Bayesian Linear Regression → — where we place a prior on and derive the posterior predictive distribution.
Exercises
Exercise 1 (Sigmoid properties). Prove all four properties in Proposition 1. In particular, verify by direct differentiation.
Exercise 2 (Gradient). Derive (11) step by step without skipping algebraic simplification. Show each intermediate expression.
Exercise 3 (Hessian). Verify (14) by differentiating (11). Confirm that is positive semidefinite for any .
Exercise 4 (Decision boundary). For and features , find the equation of the decision boundary in the plane. Sketch the regions where .
Exercise 5 (Softmax). Prove that for any scalar . Why does this imply identifiability issues for ?
Exercise 6 (Separation). Construct a 2D dataset with two points labeled 0 and two labeled 1 that is linearly separable. Show that the cross-entropy loss can be driven arbitrarily close to zero by scaling .
Exercise 7 (Multiclass gradient). Derive (18) by applying the chain rule to (17). Show that the gradient for each class has the same "prediction minus target" form.
Exercise 8 (Connection). Show that binary logistic regression with and constraint reduces to standard binary logistic regression. Explain the redundancy in the -class parameterization.