Logistic Regression: A Fundamental Classification Method
For a binary response, we want to model a probability between zero and one. A linear predictor does not enforce these bounds. Logistic regression instead models the log-odds as a linear function and transforms the result into a probability. We derive its likelihood, gradient, and Hessian, then examine Bayesian inference and coefficient interpretation.
Recap: The chain rule for a linear score
For a fixed predictor vector and differentiable scalar function , the vector-calculus rule used below is
Thus each observation contributes its predictor vector times a scalar derivative. The Bernoulli likelihood will determine that scalar.
Key Logistic Regression Framework
Let be a binary outcome with predictors . For observation , we use the column vector , whose first entry supplies the intercept. We write and model its log-odds as a linear function:
Component Form:
Vectorized Form:
The logit function maps probabilities strictly between zero and one to the real line. The inverse of this link is the sigmoid function, which satisfies and .
Component Form:
Vectorized Form:
For the likelihood, we keep the predictor matrix fixed and assume independent draws . In the frequentist calculation, the parameters stay fixed and denotes an observed outcome. Thus and .
The Bayesian section uses the same conditional model for the outcomes but adds a prior on . Posterior averages then vary while keeping the observed fixed.
1. Frequentist Approach
In the frequentist approach, we treat as fixed but unknown and estimate it by maximizing the likelihood of the observed outcomes, conditional on the predictors.
Likelihood and MLE
Given independent observations , with , the probability of is . The likelihood is:
Component Form:
Log-Likelihood:
Derivative of the Log-Likelihood
To maximize , we need the gradient . We derive it one observation at a time, then collect the terms in matrix form.
Express the likelihood through the coefficients.
Substituting the logistic probabilities into the log-likelihood gives:
Differentiate the probability map.
The derivative of is:
We use this identity when differentiating each logarithm below.
Apply the chain rule to one observation.
Write the contribution from observation as:
Let . The chain rule gives the two derivatives we need:
For ,
and for ,
Combining the two contributions gives:
After collecting terms, the gradient is the predictor vector multiplied by the observed outcome minus its predicted probability:
Combine the observation-level derivatives.
The gradient of the full log-likelihood is:
Let be the matrix with row equal to , and let be the outcome vector. Defining , with the sigmoid applied to each entry, gives:
Equivalently, we can write the derivative for one coefficient or for the full vector:
-
Component form for coefficient :
-
Vector form:
Hessian Matrix
Newton–Raphson also uses the Hessian , the matrix of second derivatives of the log-likelihood. Differentiating the gradient gives:
-
Component Form
For coefficients and , the second partial derivative is:where .
-
Vectorized Form
Define the diagonal matrix . Then the Hessian matrix is:
Why No Closed-Form Solution?
Setting gives:
Because depends nonlinearly on through , these equations do not generally have a closed-form solution. When a finite maximizer exists, we typically compute it numerically:
- Newton-Raphson: Iteratively update using:
- Gradient ascent: Update via with a suitable positive step size . The plus sign reflects maximization; minimizing the negative log-likelihood instead uses gradient descent.
2. Bayesian Approach
Bayesian logistic regression uses the same likelihood and describes uncertainty about through a prior . For example, we may use a Gaussian prior with precision :
Posterior Inference
The posterior is proportional to the likelihood times the prior:
Log-Posterior:
Why No Closed-Form Posterior?
Combining a Gaussian prior with the logistic likelihood does not produce a Gaussian posterior. In other words, this prior is not conjugate to the likelihood. We generally cannot compute the posterior's normalizing constant in closed form, so we use numerical methods such as:
- Markov Chain Monte Carlo (MCMC): Sample from the posterior using algorithms like Metropolis-Hastings or Hamiltonian Monte Carlo.
- Variational inference: Approximate the posterior within a chosen family, such as Gaussian distributions, by maximizing the evidence lower bound (ELBO).
Estimating Parameters Step by Step
Frequentist (MLE) Derivation
The score equation does not generally let us solve directly for the coefficients. Instead, we use the derivatives above in an iterative procedure: calculate the current probabilities, evaluate the gradient and Hessian, and update the coefficients. The expressions below collect the quantities needed for these updates.
-
Define the Likelihood
For each observation , where , the probability of the observed outcome is:where is the sigmoid function.
-
Log-Likelihood
Taking logs turns products into sums:This expression is more convenient for differentiation and for numerical optimization.
-
Gradient and Hessian
Let be the design matrix, the outcome vector, and (applied row-by-row). Then:- Gradient:
- Hessian:
where .
- Gradient:
-
Newton-Raphson Update
- Initialization: Choose a starting guess .
- Iterate until convergence:
We recompute the Hessian and gradient at each iteration. The inverse notation assumes a nonsingular Hessian; in computation, we solve the corresponding linear system. Damping or a line search may be needed to control the step.
-
Gradient Ascent
If computing the Hessian is costly, we can use gradient ascent, which requires only first derivatives:
where is the step size or learning rate.
Gradient ascent avoids solving the Newton system, although it may require more iterations. Stochastic and mini-batch versions replace the full-data gradient with an estimate from a subset of observations; their step sizes must be chosen accordingly.
Interpreting Logistic Regression Results
1. Statistical Interpretation
In the additive model specified above, a coefficient describes the change in log-odds associated with a one-unit increase in , holding the other predictors fixed:
-
Log-Odds Interpretation:
- is the change in (log-odds of the event) for a one-unit increase in , holding all other predictors constant.
-
Odds Ratio:
- is the odds ratio (OR) associated with a one-unit increase in .
- If , the odds of increase as increases.
- If , the odds of decrease as increases.
-
Confidence Intervals:
- We usually report an estimated odds ratio with a confidence interval. An interval containing 1 does not reject the null odds ratio of 1 at the corresponding two-sided significance level; it does not establish that there is no association.
2. Layman Interpretation
For a non-technical audience, predicted probabilities are often easier to interpret than log-odds. The important distinction is that an odds ratio multiplies odds, not probabilities. For example:
-
“Chance” vs. “Odds”:
- A one-unit increase in multiplies the odds of by , with the other predictors held fixed. If the original probability is and the odds ratio is , the new probability is . Doubling the odds therefore does not generally double the probability.
-
Example Phrase:
- If an additional study hour has an estimated odds ratio of 2, a baseline passing probability of 0.5 becomes about 0.67, not 1.0. This describes an association in the fitted model, not necessarily the causal effect of studying an extra hour.
-
Intuitive Summaries:
- Report predicted probabilities for clearly specified predictor values. This makes the size of the association visible without confusing a percentage change in odds with a percentage-point change in probability.
Model Assumptions & Diagnostics
Linearity in the Log-Odds
What It Means: Continuous predictors should relate linearly to the log-odds of the outcome, not directly to the outcome itself.
How to Check:
- Box–Tidwell Check: For a strictly positive continuous predictor, terms of the form can be used to assess departure from linearity on the log-odds scale. This is evidence about the chosen functional form, not proof that an unflagged model is correct.
- Residual or Partial Residual Plots: Plot deviance or Pearson residuals vs. the predictor. A systematic pattern suggests nonlinearity.
- Spline Terms: Including spline (e.g., cubic) expansions of predictors can reveal or capture nonlinearity.
Mathematical Note: If does not adequately describe the relationship, consider higher-order or interaction terms.
No Perfect Separation
What It Means: Complete or quasi-complete separation can prevent a finite maximum likelihood estimate from existing. Along a separating direction, some coefficient estimates diverge toward .
How to Check:
- Software Warnings: Common packages (R, Python, etc.) often warn about divergence or extremely large coefficient estimates.
- Cross-Tabulations: Inspect whether specific categories of predictors contain only 0’s or only 1’s for the outcome.
Remedies: Use Firth’s bias-reduced logistic regression or add a penalty (e.g., ridge or lasso).
Mathematical Note: For Bernoulli observations, . Under complete separation, the log-likelihood approaches its supremum, , along a separating direction, but does not attain it at finite coefficients. It is the coefficients—not the log-likelihood—that become unbounded. See the Stan discussion of separation.
Independence of Observations
What It Means: Each data point (observation) is assumed to be independent from the others.
How to Check:
- Study Design: Ensure the data-collection process didn’t violate independence (e.g., repeated measures, clustered data).
- Time or Cluster Structure: Examine residual patterns over time or within groups, using diagnostics appropriate to a binary-response model.
- Intraclass Correlation (ICC): For grouped data, a high ICC indicates dependence within groups.
Remedies: Use Generalized Estimating Equations (GEE) or Mixed-Effects Logistic Regression if observations are correlated.
Absence of High Multicollinearity
What It Means: Predictors should not be excessively correlated with each other, as it inflates variance of the parameter estimates.
How to Check:
- Variance Inflation Factor (VIF): (or 10) often indicates problematic collinearity.
- Correlation Matrix: Look for predictor pairs with correlations near 1 or -1.
- Eigenvalues & Condition Indices: A condition index above 30 is suspicious.
Remedies: Remove or combine redundant predictors, or apply regularization (ridge or lasso).
Correct Link Function
What It Means: Logistic regression assumes a logit link. If the true relationship is closer to a probit or complementary log-log, the model may be misspecified.
How to Check:
- Compare Fit: Fit alternative links (e.g., probit) and compare information criteria (AIC/BIC).
- Residual Plots: Look for systematic deviation that might indicate a different link is needed.
Mathematical Note: The logit link is . Alternative links just change the transformation of .
No Overdispersion (for Aggregated Binomial Data)
What It Means: When using grouped/binomial data, logistic regression assumes binomial variance. Overdispersion occurs if the variance exceeds what's expected under the binomial assumption.
How to Check:
- Deviance / Degrees of Freedom: If the ratio is noticeably greater than 1, consider overdispersion.
- Pearson Chi-Square: Compare to degrees of freedom for an overdispersion test.
Remedies: Use a quasi-binomial or beta-binomial approach if overdispersion is detected.
Adequate Sample Size
What It Means: Logistic regression requires enough data relative to the number of parameters. Small sample sizes can lead to instability or bias in estimates.
How to Check:
- Events and Non-Events: Compare the available outcomes with the number of fitted parameters, including nonlinear terms and interactions. A fixed events-per-variable threshold is not a guarantee of adequate estimation.
- Simulation or Power Analysis: Evaluate how sample size affects parameter estimation confidence.
Remedies: Collect more data, reduce the number of predictors, or apply penalized methods (ridge, lasso, or Firth).
Conclusion
The inverse logit turns the linear predictor into a valid probability. Differentiating the Bernoulli log-likelihood then gives observed outcomes minus predicted probabilities, weighted by the predictor vectors. The Hessian supplies the curvature for Newton–Raphson; adding a prior instead changes the objective to posterior inference without changing the likelihood.
Interpretation requires a separate step: coefficients describe conditional log-odds associations, and odds ratios are not probability ratios. Checking the functional form, separation, dependence, and the amount of available information helps us judge whether those fitted associations are useful for the intended question.
