Lesson 4.4 · 9 min read · about 55 min to master at a very slow pace
Maximum Likelihood Estimation
How logistic regression is fitted: choose coefficients that make the observed labels as probable as possible. The likelihood, the log-likelihood, a worked example, and why there is no closed form.
The idea
Unlike OLS there's no formula for logistic regression's coefficients. Instead we use maximum likelihood estimation (MLE): choose to make the labels we actually observed as probable as possible under the model.
Likelihood for one observation
With predicted probability :
This is just a compact way to write " if , and if ".
All observations, and the log
For independent observations, the likelihood multiplies them. Products of many small numbers are awkward, so we take logs. The log is increasing, so the maximiser doesn't change:
Log-likelihood
Each term is the log of the probability the model gave to what actually happened. is always , and closer to 0 is better.
Worked example
Labels ; predicted probabilities .
Probabilities of what happened: , , .
.
Mean cross-entropy (lesson 4.5).
The simplest MLE: no predictors
Labels and a single probability for everyone. .
Derivative: . The MLE is the proportion of ones, which matches intuition.
Fitting in practice
- Maximise , which is the same as minimising , which is the same as minimising the cross-entropy loss. So logistic regression is ERM with cross-entropy.
- There's no closed form: setting the derivative to zero gives equations in which sits inside the sigmoid. They're solved numerically (gradient descent, Newton's method).
- The log-likelihood is concave, so there are no bad local optima: the numerical solver finds the global best.
Testing coefficients (Wald z-test)
As with OLS, test with , which is approximately standard normal for large samples. Reject at 5% if . A 95% CI for the odds ratio is .
Worked example
, SE : , , significant. CI for : ; for the odds ratio: . It excludes 1, consistent with the test.
Perfect separation
If a predictor perfectly splits the classes, the likelihood keeps increasing as the coefficient grows, so the MLE diverges (huge and SEs). Regularisation (5.x) fixes this.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
Going deeperWhy does logistic regression need gradient descent when OLS has a formula?
Both set the gradient (the vector of slopes of the objective, one per coefficient) to zero. For OLS the objective is quadratic, so the gradient is linear in and the equations can be solved directly: .
For logistic regression, sits inside the sigmoid, so the equations can't be rearranged into a formula. Instead you start somewhere and repeatedly step against the gradient of the cross-entropy, , until it stops improving. The objective is convex, so this finds the single best . The one exception is perfectly separable data, where the coefficients keep growing forever.
Cheatsheet
Logistic log-likelihood
Wald test
OLS vs MLE fitting
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.