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.

Not startedJump to quiz

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.

-5-4-3-20.20.40.60.8plog-likelihood ℓ(p)maximum at p = 0.75, ℓ = −2.249

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

. Maximise ⇔ minimise cross-entropy.

Wald test

, reject if . OR CI: .

OLS vs MLE fitting

OLS: minimise RSS, closed form. Logistic MLE: maximise log-likelihood, numerical (concave, no bad local optima).

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptWhy can't logistic regression be fitted with a closed-form formula like OLS's ?
Q2calculateLabels ; predicted probabilities . What is the log-likelihood?
Q3conceptWhy do we maximise the log-likelihood rather than the likelihood itself?
Q4calculateA coefficient is with SE . Is it significant at 5% (Wald test)?
Q5calculateA logistic model with no predictors is fitted to labels . What is the maximum-likelihood estimate of ?