Skip to content
mlmentorship

Logistic regression

Compute a Bernoulli likelihood and gradient, separate probability fitting from decision costs, and test interactions, separation, and odds interpretation.

Published · 6 min read ·Core ·Foundation
Reading depth

Summary

Logistic regression models binary outcomes with affine log-odds, not an affine probability. Fit probabilities using a Bernoulli likelihood; choose actions separately using deployment costs.

Task: independent observations have , . Evaluate candidate intercept and slope using summed negative log-likelihood. Find both probabilities, the likelihood, and the intercept/slope gradient. These coefficients are a checked candidate, not an asserted finite optimum for separable data.

The maximum-likelihood lesson supplies the fitting principle; here the conditional observation model is Bernoulli.

The model

For binary :

The log-odds (logit) is linear in :

Odds are , with . Equal score increments multiply odds equally, but do not add equal probability increments.

Learning objective

Equal logit steps multiply odds rather than add fixed probability

Equal logit steps multiply oddsMoving z from 0 to 1 multiplies odds by e. The probability change is nonlinear and becomes smaller near the extremes.Follow the same three candidatesLogit: equal steps012Odds: multiply by e12.7187.389Probability: compress toward one0.50.73110.8808Equal logit gaps become unequalprobability gaps.
Derivation
Linear logits mapped to odds and sigmoid probabilities A sigmoid curve maps the linear score z to probability. Five equally spaced scores, minus two through two, have probabilities 0.12, 0.27, 0.50, 0.73, and 0.88. Unit score increments multiply odds by e. The dashed zero-score line is the probability-one-half decision boundary, a special case rather than a universal cost-sensitive threshold. 0 0.5 1 probability p p=.12 p=.27 p=.50 p=.73 p=.88 p=.5 boundary z=-2 -1 0 1 2 odds .14 .37 1 2.72 7.39
Read it this way: each unit score step multiplies odds by e. The five probability markers compress toward zero and one. The zero-score line marks p=0.5, appropriate for equal error costs under the stated probability model. Unequal costs can move the action threshold without refitting this sigmoid. Sources: An Introduction to Statistical Learning and Stanford CS229.
Check: Why does z increasing from 0 to 1 multiply odds by 2.72 rather than add fixed probability?
Read it this way: Moving z from 0 to 1 multiplies odds by e. The probability change is nonlinear and becomes smaller near the extremes.
Does another unit logit step add the same probability near the upper end?

Training

Negative log-likelihood (binary cross-entropy):

Our scores give probabilities . The observed-label likelihood is , so . Residuals give

The negative gradient locally decreases the intercept and increases the slope. This is Bernoulli cross-entropy, not a Gaussian squared-error objective.

With augmented design matrix containing the intercept column, . Its Hessian is , positive semidefinite for fixed features. Convexity does not ensure a finite, identifiable minimizer. Evaluate each term stably as , with .

Change decision costs

New task: false positives cost ; false negatives cost ; correct actions cost zero. Keep the probabilities and compare positive-action cost with negative-action cost . Check: choose positive for ; at , costs are versus . Use when: these probabilities describe the deployment population and errors have unequal costs.

Change the feature basis

New task: represent XOR at . Add interaction ; candidate score gives . Check: threshold recovers all four classes. Use when: additive log-odds in original features misses a known interaction. The model remains linear in expanded coefficients; these are fixture coefficients, not an MLE claim.

Common pitfalls

Failure: for , , and , increasing drives loss toward zero. No finite attains it: complete separation defeats the implied finite optimum. A sigmoid does not establish calibration under finite samples, misspecification, regularization, or deployment shift.

Reconstruct: a coefficient doubles odds. Starting at , what follows one feature unit, holding others fixed?

Answer

Odds move from to , giving , not .

Multinomial / softmax regression

For mutually exclusive classes, normalize affine class scores with softmax and fit categorical cross-entropy. Multilabel outcomes instead need an appropriate joint or separate-output model; not every classifier uses softmax.

Properties

For an additive coefficient, is a conditional log-odds change holding other features fixed; is its odds ratio. Interactions alter that interpretation, and it is not automatically a causal effect. At probability threshold , the boundary is .

When to use vs. alternatives

Compare held-out probability quality, operating costs, latency, feature construction, and maintainability against relevant alternatives. Validate calibration separately; a model family is not a calibration certificate.

L-BFGS uses quasi-Newton updates; SGD/Adam use gradient updates; Newton-Raphson/IRLS uses curvature through weighted least squares. Solver choice depends on sparsity, dimensions, penalties, and resource limits.

An added is one declared L2 convention, not an automatic implementation default. State whether the intercept is penalized and scale features using training-only statistics. Control feature engineering and leakage when comparing trees or nonlinear models. Use the separate derivation for the Bernoulli gradient cancellation.