Logistic regression is trained by minimizing an average of negative logarithms, and it is fair to ask why that loss was chosen rather than the number of wrong labels or the squared error. The answer starts from a probability model for the labels. This lesson writes the likelihood of the observed labels, turns its product into a sum and then into the binary cross-entropy, finds a maximum likelihood estimate in closed form for a simple model, and shows how a prior distribution on the weights produces the ridge and lasso penalties.
Key terms
Likelihood
Definition Fix observed training data and a model that gives, for each parameter value in an allowed set , the probability that the label equals when the features are . The likelihood is the probability that the model gives to the observed labels, viewed as a function of in with the data held fixed. If the labels are modeled as conditionally independent given their features and , it is the product
Without the independence assumption the joint probability of the labels need not factor this way. For a continuous target the probabilities are replaced by densities.
In plain words A probability fixes the parameters and asks which labels might occur. Likelihood runs the other way. It fixes the labels that did occur and asks how probable they were under each parameter value. Its values are not probabilities of parameter values and need not add or integrate to over . Turning them into probabilities about requires a prior distribution and Bayes’ rule.
Example Suppose the labels are modeled as independent, each equal to with the same probability , where . Then , with , and . The area under for is , so the likelihood curve is not a probability density for .
Bernoulli likelihood
Definition A label in has a Bernoulli distribution with parameter , where , when and . Both cases fit in one formula,
for in , with read as . If a classifier gives example the probability of class , and the labels are conditionally independent given their features and , the Bernoulli likelihood is
For logistic regression and .
In plain words The exponents pick the right factor. An example with contributes and an example with contributes , and in both cases that is the probability the model gave to the label that actually occurred. One example whose actual label received a probability near pulls the whole product toward .
Example The labels are . Candidate A predicts class-one probabilities , so every observed label receives probability and the likelihood is . Candidate B predicts , so every observed label receives and the likelihood is . The observed labels are times as probable under A as under B. This ranks two candidates and does not show that A is the best possible one.
Maximum likelihood estimation
Definition A maximum likelihood estimate (MLE) is a parameter value in with for every in . An MLE need not exist, and when one exists it need not be unique. Take labels with ones, modeled as independent Bernoulli labels with one shared parameter in . For the logarithm of the likelihood is , and setting its derivative to zero gives
Multiplying by gives , so and . When the function is strictly concave on , so the sample proportion is the unique MLE. When the likelihood is largest at the endpoint , and when at . For logistic regression the MLE of has no closed form and is computed numerically, and it does not exist as a finite vector when a hyperplane separates the two training classes.
In plain words Maximum likelihood picks the allowed parameter value under which the observed labels were most probable. With shared parameters, as in logistic regression, raising the probability of one example’s label often lowers another’s, and the MLE balances these pulls. A high training likelihood is a fitting criterion, not a guarantee. A model flexible enough to give every training label a probability near can fit the training data almost perfectly and still predict new cases badly.
Example For the labels we have and , so the MLE of the shared probability is , with likelihood . That is larger than the values at and at found above. The estimate is the sample proportion, derived here from the shared independent Bernoulli model, and in a finite sample it need not equal the true probability.
Log likelihood and negative log likelihood
Definition The log likelihood is , defined where . With conditionally independent labels it is a sum,
and for the Bernoulli model the term of example is . The negative log likelihood (NLL) is . The natural logarithm is strictly increasing, so maximizing , maximizing and minimizing give the same parameter values. Dividing by the positive number changes its values but not its minimizers.
In plain words The logarithm turns a product into a sum without changing which parameters are best. Each term is at most , so each NLL term is a nonnegative penalty, small when the observed label got a high probability and large when it got a probability near . A sum of moderate numbers is also far safer in floating point than a product of many numbers below .
Example For candidates A and B above, and , so the NLL values are and . Their difference is , the logarithm of the likelihood ratio. A model that gives each of labels probability has likelihood , far below the smallest positive double-precision number, about , so the computed product is exactly . Its log likelihood is , an ordinary number.
Binary cross-entropy
Definition For a label in and a predicted probability of class with , the log loss is
which equals when and when . For labels and predicted probabilities , the binary cross-entropy, or mean log loss, is the Bernoulli NLL divided by ,
The name comes from the cross-entropy between two distributions on with probabilities and of class . Each loss equals , with the label acting as the distribution that puts all its probability on the observed class. For a logistic model with score and , the loss of one example can be written from the score as , its derivative with respect to the score is , and the first term can be computed as
In plain words Each example pays minus the log of the probability its actual label received, so a confident mistake is expensive and the cost grows without bound as that probability goes to . On average the loss favors the true probability. If a case is positive with probability , where , the expected loss has derivative in , so it falls until and rises after. A fitted model can still be miscalibrated because of finite data or a wrong model form. In code the right side of the last display is the safe way to compute , since its exponent is never positive, and working from the score avoids taking the logarithm of a rounded probability. At with , the computed rounds to exactly in double precision and fails, while the score form gives the correct loss of about .
Example Candidate A has mean loss and candidate B has . A single positive example given probability costs , and given it costs about . For one gradient step, take features with labels and a logistic model started at . Every probability is , so the values are . Their mean is the derivative for , and the mean of , which is , is the derivative for . A step of size gives and , the probabilities become about , and , and the mean loss falls from to about . One step moves the fit in the right direction on these training examples and does not finish the fitting.
Maximum a posteriori estimation
Definition A prior distribution is a probability distribution on the parameters, chosen before the data at hand are used, that describes which values are considered plausible. By Bayes’ rule the posterior satisfies . A maximum a posteriori (MAP) estimate is a mode of the posterior, a value of that maximizes , or equivalently minimizes .
Suppose the weights get independent priors and the intercept is left out of the prior. Dividing the MAP objective by gives the mean NLL plus a penalty, up to a constant that does not depend on . A normal, or Gaussian, prior with mean and variance has plus a constant, so the MAP estimate minimizes
which is the ridge penalty. A Laplace prior with density and scale has plus a constant, which gives the lasso objective with . Leaving the intercept unpenalized corresponds to a flat prior on , which is not a probability distribution on the whole real line. The penalized fit is still well defined when its objective has a minimizer, but calling it a posterior mode then needs a separate check that the posterior is a proper distribution.
In plain words The prior says which weights seemed plausible before these data, and MAP picks the parameter value with the highest posterior density. A normal prior centered at pulls weights smoothly toward , which is ridge, and a Laplace prior has a sharp peak at , which is why lasso can set weights exactly to . With the MAP estimate is generally not the MLE. It is a single point and says nothing by itself about how uncertain the parameters are, which needs the whole posterior. A prior also depends on units, since a prior variance for a weight per kilometer means something else for a weight per meter. With the prior fixed, shrinks as grows, so the data count for more.
Example Take training examples and normal priors with mean and variance on the weights, so . A weight adds to the summed negative log prior, or after dividing by . The ridge term gives the same value, . With Laplace priors of scale instead, , and the same weight adds to the sum, or after dividing by , which matches .
Check yourself
- A model gives probability to class for an example whose label is . Find the log loss of this example and compare it with the loss when the probability is . Then explain why maximizing the likelihood is the same as minimizing this kind of loss.
Show the answer
The observed label received probability , so the loss is . With probability the loss is , about times smaller. The natural logarithm is strictly increasing, so a parameter value with a larger likelihood has a larger log likelihood. The minus sign reverses that order, so the best parameters minimize the NLL, and dividing by the positive keeps the same minimizers. Each example’s share of the NLL is minus the log of the probability given to its actual label, which is exactly the loss computed here.
- Five labels are modeled as independent Bernoulli labels with one shared probability . Find the MLE of and the mean log loss at that value. Then suppose all five labels are . What is the MLE over , and what happens when an intercept-only logistic model is fitted to these labels?
Show the answer
There are ones among labels, so . The mean log loss at this value is , smaller than at , as it must be. With five ones the likelihood is , which increases on , so the MLE is the endpoint . The logistic model reaches only for finite , and its mean loss keeps decreasing as grows, about at and at , without reaching . No finite is an MLE, and gradient descent keeps increasing . A penalty or prior on would give a finite estimate, but it changes the objective.
- A logistic model with two weights is trained on examples by minimizing the mean NLL plus , with the intercept unpenalized. Which matches independent normal priors with mean and variance on the weights? Check your answer at , and find for the same prior with . A colleague calls the fitted weights maximum likelihood estimates and quotes the prior standard deviation as their uncertainty. What is wrong?
Show the answer
The match is . At the summed negative log prior, without its constant, is , which is after dividing by . The ridge term gives as well. With and the same prior, . The fitted weights are a MAP estimate, which minimizes a penalized objective and is generally not the MLE. The prior standard deviation describes plausibility before the data, not uncertainty after them, and a MAP point by itself gives no measure of posterior uncertainty.