Deriving Machine Learning Cost Functions using Maximum Likelihood Estimation (MLE) - Part II
Cross-Entropy Loss - a commonly used cost function for binary classification problems derived using Maximum Likelihood Estimation (MLE)

In Part I of this article, we introduced Maximum Likelihood Estimation (MLE), Likelihood function, and derived Mean Squared Error (MSE) using Maximum likelihood estimation. In this article, we will use Maximum likelihood estimation to derive Cross-Entropy cost function, which is commonly used for binary classification problems.
Background
Binary logistic regression is used to model the relationship between a categorical target variable and a predictor vector . The target variable will have two possible values, such as whether a student passes an exam or not, or whether a visitor to a website subscribes to the website’s newsletter or not. The two possible categories are coded as ‘1’, called the positive class, and ‘0’, called the negative class. Binary logistic regression estimates the probability that the response variable belongs to the positive class given .
In linear regression, we model the expected value (the mean ) of the continuous target variable as a linear combination of the predictor vector and estimate the weight parameters using our training data.
In this case where our target variable is categorical and has two possible values coded as 0 and 1, the expected value or mean of is the probability of observing the positive class1. It seems sensible then to model the expected value of our categorical variable using equation (2), as in linear regression.
The problem with modelling the probability as a linear combination of the predictor variables is that probability has a range , but the right-hand side of the equation outputs values in the range . In other words, we will get meaningless estimates of the probability if we use that equation.
The solution is to use a function of probability that provides a suitable relationship between the linear combination of the predictor variables and , the mean of the response variable. This function is called a link function, and it maps the probability range to .
The most commonly used link function for binary logistic regression is the logit function (or log-odds2), given as:
How do we then go from the logit function to getting the estimate of the probability p(X) of observing the positive class? Because logit is a function of probability, we can take its inverse to map arbitrary values in the range back to the probability range .
Recall that the inverse function of the natural logarithm function is the exponential function, so if we take the inverse of equation (4), we get:
If we solve for in equation (5), we get3:
Equation (6) is the logistic (or sigmoid) function, and it maps values in the logit range back into the range of probabilities.
Deriving Cost Entropy using MLE
Given a set of training examples , binary cross-entropy is given by:
where $x^{(i)}$ is the feature vector, $y^{(i)}$ is the true label (0 or 1) for the $i^{th}$ training example, and $p^{(i)}$ is the predicted probability that the $i^{th}$ training example belongs to the positive class, that is, $Pr(Y = 1 | X = x^{(i)})$.
In this section, we will derive cross-entropy using MLE. If you are not already familiar with MLE and likelihood function, I will advise that you read the section that explains both concepts in Part I of this article.
The derivation of cross-entropy follows from using MLE to estimate the parameters of our logistic model on our training data.
We start by describing the random process that generated .
is a realisation of the Bernoulli random variable4 . The Bernoulli distribution is parameterised by , and its probability mass function (pmf) is given by:
which can be written in the more compact form:
We then define our Likelihood function. The estimates of we choose will be the ones that maximise the likelihood function. The likelihood function is a function of our parameter given our training data:
It is easier to work with the log of the likelihood function5, called the log-likelihood, so if we take the natural logarithm of equation (10), we get:
Recall that for our training data, in equation (11) is the predicted probability of the training example gotten from the logisitic function, so it is a function of the parameters . The maximum likelihood estimate is therefore the value of the parameters that maximises the log-likelihood function.
We also know that maximising a function is the same as minimising its negative.
Taking the average across our training examples, we get:
which is the cross-entropy as defined in equation (7).
Footnotes
Footnotes
-
is a Bernoulli random variable. The Bernoulli distribution is a discrete probability distribution that describes processes that have only two possible outcomes: 1, with a probability of and 0, with a probability of . The expectation (or mean) of the Bernoulli distribution is . [Proof] ↩
-
The odds is defined as the ratio of the probability of observing an event to the probability of not observing that event. . If, for example, the odds of an event happening is :1, it means that the event happened times out of a total of occurrences. The log-odds is simply the natural logarithm of the odds. ↩
-
Bernoulli distribution is the discrete probability distribution of a random variable that takes on two possible values: 1 with probability and 0 with probability . An experiment modelled by the Bernoulli distribution is called a Bernoull trial. Examples of Bernoulli trials include: tossing a coin (head/tail), playing a game (winning/not winning). ↩
-
The reasons why this is the case is explained clearly in Part I. Check the ‘What is Maximum Likelihood Estimation?’ section. ↩