Jan 27, 2019
tags: * | math notes |
*

## The real reason you use MSE and cross-entropy loss functions

If your knowledge of machine learning methods comes from an online course, chances are you’ve been taught highly simplified math. It’s not an issue if you are interested primarily in gaining practical skills. However, if you tend to wonder why things are the way they are, not knowing the proper math can be tormenting at times since you end up missing important insights. For example, why do we default to using mean squared error loss in linear regression? Hint: it’s not just because it’s conveniently convex, and it’s not because it represents the sum of Euclidean distances. Similarly, why do we use cross-entropy loss in logistic regression? The answer in both cases has to do with maximum likelihood estimation.

The likelihood function (L) of some model parameter (or a combination of parameters) θ is defined as the probability of obtaining the observed data (O) estimated by the model with parameter(s) θ.

Suppose you know the general form of your statistical model, but you don’t know which parameter values to pick. The intuitive solution is to choose the parameter values that best explain the existing observations. This is equivalent to finding the θ that maximizes the likelihood function, and this method is formally known as maximum likelihood estimation (MLE).

In linear regression, the explained variable is modelled as a linear function of the inputs plus an error term:

In the ideal case, the error term ε is normally distributed around zero with the variance independent of the input variables:

In that case y is normally distributed around :

The probability density function is described by the following equation:

For continuous distributions, maximum likelihood estimation is performed with probability densities instead of probabilities. We are going to maximize the product of the probability densities of each observation:

In max-min problems any objective function can be replaced with a monotone function that takes the original objective function as its argument. First, let’s get rid of , then take the natural logarithm, and finally let’s remove from the denominator and flip the sign. Thus maximizing the original likelihood function becomes equivalent to minimizing . Looks familiar doesn’t it? If we divide it by n, we’ll get the expression for the MSE loss function. In other words, minimizing the MSE is equivalent to applying maximum likelihood estimation to our model.

So if the MSE is such a meaningful loss function, why do we need other types of loss (e.g., L1 and lasso/ridge regression)? In part this has to do with the assumptions that went into our idealized linear model: (1) the error term is normally distributed; (2) the variance of the error term is not a function of the input variables. If either of them is violated, the connection between minimizing the MSE and maximizing the likelihood function becomes broken (*womp-womp*). More importantly, in the framework of Bayesian statistics, the method of MLE can be extended by incorporating non-uniform priors, and this results in the alternative loss functions.

Now let’s consider the logistic model (a binary classifier). We attempt to describe log-odds using a linear model:

The probability of observing outcome y=1 under this model is given by the following function (sigmoid):

With 0 and 1 being the only possible outcomes, the probability of observing outcome y=0 is simply . It is easy to write a universal expression for outcome y = o (“oh”, not zero):

The likelihood function is given by the product of all individual probabilities:

It’s easier to maximize the log-likelihood:

Thus once again maximum liklihood estimation yields a familiar loss function (cross-entropy in this case).