Classical MLLogistic Regression

Logistic Regression

Background
  • Developed in the 1950s-1960s as a way to model binary outcomes in epidemiology and social science
  • One of the most-used algorithms in industry despite being 70+ years old
  • Maximum likelihood estimation connects it directly to information theory
The Concept

Logistic regression applies a sigmoid function to a linear combination of features, squashing the output to a probability between 0 and 1. Training maximizes the likelihood of the observed labels, equivalent to minimizing cross-entropy loss.

How it Links
  • The sigmoid activation in logistic regression is the same one in neural network nodes
  • Multinomial logistic regression with a softmax output is the final layer of most classifiers
  • Connects to GLMs, Bayesian inference, and the cross-entropy loss ubiquitous in deep learning
Concrete Example
A bank model predicting loan default: features like income, debt, and payment history get weights; the sigmoid maps their sum to a default probability between 0 and 1. A probability > 0.5 triggers rejection.
Learning outcomes
  • State the sigmoid model and derive the log-likelihood and its gradient.
  • Recognize logistic regression as a generalized linear model with the canonical logit link.
  • Derive Newton-Raphson and IRLS as second-order optimization on the log-likelihood.
  • Explain why logistic regression is well-calibrated while decision-tree leaf proportions are not.
  • Implement logistic regression in NumPy (IRLS), sklearn (l1 and l2), and PyTorch (BCEWithLogitsLoss).

TL;DR

TL;DR

Logistic regression models P(y=1∣x)=σ(w⊤x+b)P(y = 1 \mid x) = \sigma(w^{\top}x + b), where σ\sigma is the logistic sigmoid. It is the GLM with the canonical logit link and Bernoulli likelihood, and its parameters are fit by maximum likelihood. The negative log-likelihood is convex with gradient X⊤(p^−y)X^{\top}(\hat{p} - y), so it is solved by gradient descent or, more commonly, by Newton-Raphson which becomes iteratively reweighted least squares (IRLS) [1][3]. The decision boundary is still a hyperplane in feature space, but the output is a calibrated probability rather than a hard label as with the perceptron [4].

Intuition

If you want a probability that an example is class 1, logistic regression is the simplest model that gives you one. It has the same affine score z=w⊤x+bz = w^{\top}x + b as the perceptron, but instead of thresholding at zero with the sign function it squashes through the logistic sigmoid

σ(z)=11+e−z,\sigma(z) = \frac{1}{1 + e^{-z}},

which maps R→(0,1)\mathbb{R} \to (0, 1) and is monotonic. Two facts make this model well behaved:

  • The log-odds is linear. The function σ\sigma inverts to σ−1(p)=log⁡p1−p\sigma^{-1}(p) = \log\frac{p}{1-p}, the logit. So logistic regression is exactly the model “the log-odds of class 1 is a linear function of xx” - a small, falsifiable assumption.
  • The boundary is still a hyperplane. The decision rule σ(z)>0.5\sigma(z) > 0.5 is the same as z>0z > 0. The sigmoid does not curve the boundary; it curves the probability surface that sits above it.
Calibration is free here

Logistic regression is trained on the log-likelihood of the observed labels, so its outputs are designed to match the empirical conditional rate P(y∣x)P(y \mid x) when the linear-in-log-odds assumption holds. Decision-tree leaf proportions are noisy because each leaf has a small sample, and they are systematically over-confident on the training set. That is why a logistic head on top of a calibrated representation is often the right last layer for production classifiers [1].

Math

Math: log-likelihood, gradient, and Newton step

Model

For binary labels y∈{0,1}y \in \{0, 1\}, the model is

p(x;w,b)=σ(w⊤x+b),σ(z)=11+e−z.p(x; w, b) = \sigma(w^{\top}x + b), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}.

A useful identity: σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)).

Likelihood

Assume the labels are conditionally Bernoulli, yi∣xi∼Bern(p(xi))y_i \mid x_i \sim \mathrm{Bern}(p(x_i)). The likelihood of the whole training set is

L(w,b)=∏i=1np(xi)yi (1−p(xi))1−yi,\mathcal{L}(w, b) = \prod_{i=1}^{n} p(x_i)^{y_i}\,(1 - p(x_i))^{1 - y_i},

so the log-likelihood is

ℓ(w,b)=∑i=1n[ yilog⁡p(xi)+(1−yi)log⁡(1−p(xi)) ].\ell(w, b) = \sum_{i=1}^{n}\Bigl[\,y_i \log p(x_i) + (1 - y_i)\log(1 - p(x_i))\,\Bigr].

The negative log-likelihood is the binary cross-entropy loss; minimizing it equals maximum likelihood.

Gradient

Let zi=w⊤xi+bz_i = w^{\top}x_i + b and pi=σ(zi)p_i = \sigma(z_i). Differentiating term by term, one shows

∂ℓ∂zi=yi−pi.\frac{\partial \ell}{\partial z_i} = y_i - p_i.

By the chain rule,

∇w ℓ(w,b)=∑i=1n(yi−pi) xi=X⊤(y−p^),∂ℓ∂b=∑i=1n(yi−pi).\nabla_{w}\,\ell(w, b) = \sum_{i=1}^{n} (y_i - p_i)\, x_i = X^{\top}(y - \hat{p}), \qquad \frac{\partial \ell}{\partial b} = \sum_{i=1}^{n}(y_i - p_i).

The negative log-likelihood gradient is therefore X⊤(p^−y)X^{\top}(\hat{p} - y) - the same shape as the OLS gradient, but with the sigmoid squashed predictions in place of the linear ones. There is no closed form, because p^\hat{p} depends nonlinearly on ww.

Hessian

Let W=diag⁡(pi(1−pi))W = \operatorname{diag}\bigl(p_i(1 - p_i)\bigr). Then

∇w2 [−ℓ(w,b)]=X⊤WX⪰0,\nabla^{2}_{w}\,\bigl[-\ell(w, b)\bigr] = X^{\top} W X \succeq 0,

so the negative log-likelihood is convex (and strictly convex if XX has full column rank), guaranteeing a unique minimum and well-behaved optimization.

Newton’s update and IRLS

Newton-Raphson on the negative log-likelihood gives the update

wt+1=wt+(X⊤WtX)−1X⊤(y−p^t),w_{t+1} = w_{t} + (X^{\top} W_t X)^{-1} X^{\top}(y - \hat{p}_t),

which can be rewritten as a weighted least-squares problem:

wt+1=(X⊤WtX)−1X⊤Wtzt,zt=Xwt+Wt−1(y−p^t).w_{t+1} = (X^{\top} W_t X)^{-1} X^{\top} W_t z_t, \qquad z_t = X w_t + W_t^{-1}(y - \hat{p}_t).

This is exactly the iteratively reweighted least squares (IRLS) algorithm: each step is a weighted OLS regression of the working response ztz_t on XX with weights WtW_t. IRLS is the standard fitting procedure for any GLM, not just logistic regression [3].

GLM view

A generalized linear model has three components:

  1. Random component: y∣xy \mid x comes from an exponential-family distribution. Here, Bernoulli.
  2. Systematic component: a linear predictor η=w⊤x+b\eta = w^{\top}x + b.
  3. Link function: gg relates the mean to the linear predictor via g(μ)=ηg(\mu) = \eta.

For Bernoulli, the canonical link is the logit, g(μ)=log⁡μ1−μg(\mu) = \log\frac{\mu}{1 - \mu}. Plugging in, log⁡p1−p=w⊤x+b\log\frac{p}{1-p} = w^{\top}x + b, which inverts to p=σ(w⊤x+b)p = \sigma(w^{\top}x + b). So logistic regression is the GLM with Bernoulli random component and canonical (logit) link. With the canonical link, the score equation simplifies to X⊤(y−p^)=0X^{\top}(y - \hat{p}) = 0, exactly what we derived [3][2].

Multinomial / softmax extension

For KK classes, replace the sigmoid with the softmax

P(y=k∣x)=ewk⊤x+bk∑j=1Kewj⊤x+bj.P(y = k \mid x) = \frac{e^{w_{k}^{\top}x + b_{k}}}{\sum_{j=1}^{K} e^{w_{j}^{\top}x + b_{j}}}.

The negative log-likelihood becomes the categorical cross-entropy, and the gradient w.r.t. wkw_k is ∑i(1[yi=k]−P(y=k∣xi)) xi\sum_{i}(\mathbb{1}[y_i = k] - P(y = k \mid x_i))\,x_i - the multi-class analogue of X⊤(p^−y)X^{\top}(\hat{p} - y). It is still a convex problem.

Regularized variants

Add an L2 penalty λ2∥w∥22\frac{\lambda}{2}\lVert w\rVert_{2}^{2} for ridge-style logistic regression (MAP under a Gaussian prior on ww); add an L1 penalty λ∥w∥1\lambda \lVert w \rVert_{1} for sparse logistic regression (MAP under a Laplace prior). Both keep convexity. L2 stabilizes IRLS when X⊤WXX^{\top}WX is ill-conditioned; L1 yields feature selection.

Geometry of the decision boundary

The model predicts class 1 iff σ(w⊤x+b)>0.5\sigma(w^{\top}x + b) > 0.5, which is iff w⊤x+b>0w^{\top}x + b > 0. So the decision boundary is the hyperplane {x:w⊤x+b=0}\{x : w^{\top}x + b = 0\} - identical to the perceptron and the linear SVM. The sigmoid is a monotonic post-processing of the score that produces calibrated probabilities; it does not curve the boundary.

What changes between linear classifiers is the loss and therefore which hyperplane is chosen. Perceptron stops at any separating hyperplane; linear SVM picks the max-margin one; logistic regression picks the one that maximises Bernoulli likelihood, which uses all training points, not just the support vectors.

How it compares to nearby linear classifiers

MethodLossOutputCalibrated?Multi-class strategyClosed form?Cost per epoch (n examples, d features)
Perceptronmax(0, -y (w·x + b))class label in {-1, +1}noone-vs-rest by handnoO(n d)
Logistic regressionbinary cross-entropy / negative Bernoulli log-likelihoodP(y = 1 | x) in (0, 1)yes (under linear-in-logit assumption)softmax (multinomial)no - convex; use IRLS / GDO(n d) per IRLS iter, O(n d^2 + d^3) for the WLS step
Linear SVMhinge: max(0, 1 - y (w·x + b))class label; Platt-scaled probabilityno by default; yes after Platt scalingone-vs-rest or one-vs-oneno - convex QPO(n d) per SGD epoch, full QP otherwise

Algorithm

# Gradient descent on the negative log-likelihood (binary cross-entropy)
w = zeros(d)
b = 0.0
for step in range(max_steps):
    z = X @ w + b                      # affine score, shape (n,)
    p = 1.0 / (1.0 + exp(-z))          # sigmoid -> probabilities
    grad_w = X.T @ (p - y)             # canonical-link gradient
    grad_b = (p - y).sum()
    w = w - eta * grad_w
    b = b - eta * grad_b
 
# Newton / IRLS: solve a weighted least-squares problem each step
for step in range(max_steps):
    z = X @ w + b
    p = 1.0 / (1.0 + exp(-z))
    W = diag(p * (1 - p))              # working weights
    H = X.T @ W @ X                    # Hessian (positive semidefinite)
    grad = X.T @ (p - y)
    w = w - solve(H, grad)             # Newton step

Implementations

python
import numpy as np

def sigmoid(z):
  # numerically stable sigmoid
  return np.where(z >= 0, 1.0 / (1.0 + np.exp(-z)), np.exp(z) / (1.0 + np.exp(z)))

def logreg_irls(X, y, max_iter=25, tol=1e-8):
  """Fit binary logistic regression by IRLS (Newton on the log-likelihood)."""
  n = X.shape[0]
  X1 = np.hstack([np.ones((n, 1)), X])
  d = X1.shape[1]
  w = np.zeros(d)
  for it in range(max_iter):
      z = X1 @ w
      p = sigmoid(z)
      W = p * (1.0 - p)                                     # diag of W
      # Newton step: w <- w + (X^T W X)^{-1} X^T (y - p)
      H = (X1.T * W) @ X1
      g = X1.T @ (y - p)
      delta = np.linalg.solve(H + 1e-8 * np.eye(d), g)      # tiny ridge for stability
      w_new = w + delta
      if np.linalg.norm(delta) < tol:
          return w_new[0], w_new[1:], it + 1
      w = w_new
  return w[0], w[1:], max_iter

# tiny synthetic dataset
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 2))
y = (X[:, 0] - X[:, 1] + 0.3 * rng.normal(size=200) > 0).astype(float)

b, w, iters = logreg_irls(X, y)
print(f"converged in {iters} Newton steps")
print("intercept :", round(float(b), 3))
print("weights   :", np.round(w, 3).tolist())
IRLS = Newton on the log-likelihood. Note the canonical-link gradient X^T (y - p).

Run the from-scratch NumPy IRLS solver live in the browser:

Idle (packages: numpy)
(no output yet)

Real-world uses

DomainSystemWhy logistic regression
AdTech / search rankingClick-through-rate (CTR) baselinesCalibrated probabilities feed downstream auction logic; one-feature-per-coefficient interpretation supports auditability.
Credit / riskCredit scoring (FICO-style scorecards)Per-feature coefficients give clear adverse-action explanations required by regulation; closed-form WLS fits at scale.
HealthcareClinical prediction models (e.g. Framingham)Sigmoid output is a probability, not a hand-tuned score; coefficients are interpretable as log-odds-ratios per unit.
NLPDocument classification with bag-of-words / TF-IDF featuresTrains in seconds on millions of features; L1-regularized variants double as feature selection.
Calibration headLast layer of deep classifiersWhen the network output is already a logit, BCEWithLogitsLoss is just logistic regression in disguise.

Pitfalls

Perfect separation makes weights blow up

If a feature perfectly separates the two classes, the MLE has ∥w∥→∞\lVert w \rVert \to \infty - the log-likelihood keeps improving without bound. IRLS will fail to converge (the Hessian goes singular). Fix with L2 regularization, Firth’s bias correction, or by replacing logistic with a max-margin loss.

Class imbalance distorts the intercept

The intercept absorbs the marginal class prior. With heavy imbalance the learned threshold of 0.5 is wrong; either use class weights, calibrate the threshold on a validation set, or rescale the intercept by the log prior ratio.

Always standardize before regularizing

L1 and L2 penalties depend on the scale of the features. A feature in cents gets penalized far less than the same feature in dollars. Standardize XX before fitting any penalized logistic regression, and never penalize the intercept.

Log-loss is a proper scoring rule

Calibration matters for risk-sensitive applications. Train on log-loss (the binary cross-entropy), then evaluate on log-loss, Brier score, and reliability diagrams - these are all proper scoring rules that reward true probabilities and punish over-confidence.

Interview Q&A

Derive the gradient of the logistic-regression log-likelihood.research

For binary labels yi∈{0,1}y_i \in \{0, 1\} and the model pi=σ(zi)p_i = \sigma(z_i) with zi=w⊤xi+bz_i = w^{\top}x_i + b, the log-likelihood is

ℓ(w,b)=∑i=1n[ yilog⁡pi+(1−yi)log⁡(1−pi) ].\ell(w, b) = \sum_{i=1}^{n}\bigl[\,y_i \log p_i + (1 - y_i) \log(1 - p_i)\,\bigr].

Use the chain rule. Define ui=yilog⁡pi+(1−yi)log⁡(1−pi)u_i = y_i \log p_i + (1 - y_i)\log(1 - p_i). Then

∂ui∂pi=yipi−1−yi1−pi=yi−pipi(1−pi),\frac{\partial u_i}{\partial p_i} = \frac{y_i}{p_i} - \frac{1 - y_i}{1 - p_i} = \frac{y_i - p_i}{p_i (1 - p_i)},

and using σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)),

∂pi∂zi=pi(1−pi).\frac{\partial p_i}{\partial z_i} = p_i (1 - p_i).

Multiplying,

∂ui∂zi=yi−pi,\frac{\partial u_i}{\partial z_i} = y_i - p_i,

and finally ∂zi/∂w=xi\partial z_i / \partial w = x_i. Summing,

∇w ℓ(w,b)=∑i=1n(yi−pi) xi=X⊤(y−p^).\nabla_{w}\,\ell(w, b) = \sum_{i=1}^{n} (y_i - p_i)\, x_i = X^{\top}(y - \hat{p}).

The cancellation between σ′(z)=p(1−p)\sigma'(z) = p(1-p) in the chain rule and the 1/(p(1−p))1/(p(1-p)) from ∂u/∂p\partial u / \partial p is why the gradient is so clean - this is a special property of the canonical-link GLM [3][2].

Why is logistic regression a generalized linear model?onsite

A GLM has three pieces:

  1. Random component: the response distribution is in the exponential family. The Bernoulli is, with natural parameter θ=log⁡p1−p\theta = \log\frac{p}{1-p}.
  2. Systematic component: a linear predictor η=w⊤x+b\eta = w^{\top}x + b.
  3. Link function gg: relates the mean μ\mu to the linear predictor via g(μ)=ηg(\mu) = \eta.

For logistic regression, the link is the canonical one for Bernoulli, the logit: g(μ)=log⁡μ1−μg(\mu) = \log \frac{\mu}{1-\mu}. So the model says log⁡p1−p=w⊤x+b\log \frac{p}{1-p} = w^{\top}x + b, which inverts to p=σ(w⊤x+b)p = \sigma(w^{\top}x + b). With the canonical link, the score equation is the simple form X⊤(y−p^)=0X^{\top}(y - \hat{p}) = 0, the Hessian is X⊤WXX^{\top} W X with WW diagonal, and Newton’s method becomes IRLS - a weighted least-squares problem each step. This is exactly the GLM machinery from McCullagh and Nelder [3].

Why is log-loss a proper scoring rule and what does that mean for training?research

A scoring rule S(p,y)S(p, y) is strictly proper if its expected value over y∼qy \sim q is uniquely minimized by p=qp = q. For log-loss,

S(p,y)=−[ylog⁡p+(1−y)log⁡(1−p)],S(p, y) = -\bigl[y \log p + (1 - y) \log (1 - p)\bigr],

we compute the expected score under the true distribution qq:

Ey∼q S(p,y)=−[qlog⁡p+(1−q)log⁡(1−p)]=H(q)+KL(q ∥ p),\mathbb{E}_{y \sim q}\,S(p, y) = -\bigl[q \log p + (1 - q)\log(1 - p)\bigr] = H(q) + \mathrm{KL}(q\,\Vert\,p),

which is minimized at p=qp = q because KL(q ∥ p)≥0\mathrm{KL}(q\,\Vert\,p) \ge 0 with equality iff p=qp = q. So if the model class is rich enough to represent the true conditional P(y∣x)P(y \mid x), fitting on log-loss recovers it - log-loss penalizes mis-calibrated probabilities, not just mis-classifications. This is the foundation of why logistic regression outputs are calibrated probabilities. It is also why people use log-loss to compare classifiers when calibration matters: a 0/1-accuracy comparison can rank a worse-calibrated model higher [1].

Derive Newton's update on the negative log-likelihood and show it equals IRLS.research

The negative log-likelihood is NLL(w)=−ℓ(w)\mathrm{NLL}(w) = -\ell(w). We computed

∇w NLL(w)=−X⊤(y−p^)=X⊤(p^−y),\nabla_{w}\,\mathrm{NLL}(w) = -X^{\top}(y - \hat{p}) = X^{\top}(\hat{p} - y),∇w2 NLL(w)=X⊤WX,W=diag⁡(pi(1−pi)).\nabla^{2}_{w}\,\mathrm{NLL}(w) = X^{\top} W X, \qquad W = \operatorname{diag}\bigl(p_i(1 - p_i)\bigr).

Newton’s step is

wt+1=wt−(X⊤WtX)−1X⊤(p^t−y)=wt+(X⊤WtX)−1X⊤(y−p^t).w_{t+1} = w_{t} - \bigl(X^{\top} W_t X\bigr)^{-1} X^{\top}(\hat{p}_t - y) = w_{t} + \bigl(X^{\top} W_t X\bigr)^{-1} X^{\top}(y - \hat{p}_t).

Define the working response zt=Xwt+Wt−1(y−p^t)z_t = X w_t + W_t^{-1}(y - \hat{p}_t) (the linearization of the link). Plugging in,

wt+1=(X⊤WtX)−1X⊤Wtzt,w_{t+1} = \bigl(X^{\top} W_t X\bigr)^{-1} X^{\top} W_t z_t,

which is the closed-form solution to the weighted least-squares problem min⁡w(zt−Xw)⊤Wt(zt−Xw)\min_{w} (z_t - X w)^{\top} W_t (z_t - X w). Each Newton step is therefore a weighted OLS regression of the working response on the features, with weights WtW_t updated each iteration. That is iteratively reweighted least squares, which is the fitting algorithm for GLMs and is what glm() in R and statsmodels.GLM actually run under the hood [3].

Check yourself
Score: 0 / 5(0 answered)
  1. 1. What is the gradient of the negative log-likelihood for binary logistic regression?
  2. 2. Why does the negative log-likelihood for logistic regression have NO closed form?
  3. 3. In the GLM framework, logistic regression uses which canonical link?
  4. 4. What is the geometry of the decision boundary for logistic regression?
  5. 5. Why are logistic-regression probabilities considered calibrated, while decision-tree leaf proportions are not?
Build it yourself

A short evening project:

  1. Generate a 2-class synthetic dataset with overlap (e.g. two Gaussians with overlapping means).
  2. Fit logistic regression by IRLS, by gradient descent, and by sklearn; confirm all three give the same coefficients to within 1e-6.
  3. Plot the decision boundary and the probability heat map.
  4. Add 50% L2 regularization and watch the boundary tilt back towards the origin.
  5. Stretch: replicate Platt scaling - take a hard-margin linear SVM, then fit a logistic regression on its scores to recover calibrated probabilities, and compare to native logistic regression on log-loss.

Interactive Demo

Logistic Regression Boundary Demo
P(y=1) = σ(w₁x + w₂y + b)
Cross-entropy loss: 0.0528
Class A (red)
Class B (blue)
Decision boundary
What to try: Adjust the weight sliders to see how the decision boundary and probability heatmap shift.

References

  1. Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning, 2nd ed.. Springer. https://hastie.su.domains/ElemStatLearn/
  2. Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. ISBN 978-0-387-31073-2
  3. McCullagh, P. and Nelder, J. A. (1989). Generalized Linear Models, 2nd ed.. Chapman & Hall / CRC. ISBN 978-0-412-31760-6
  4. Rosenblatt, F. (1958). The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain. Psychological Review 65(6), 386-408. doi:10.1037/h0042519