Where does the Mean Squared Error come from?

Tivadar Danka small portrait Tivadar Danka
regression dataset

Understanding math will make you a better engineer.

So, I am writing the best and most comprehensive book about it.

The simplest and one of the most commonly used loss functions in machine learning is the mean squared error, defined by

MSE(D,f)=1ni=1nyif(xi)2\text{MSE}(\mathcal{D}, f) = \frac{1}{n} \sum_{i=1}^{n} | y_i - f(x_i) |^2

which is just the mean of the squared distances between ground truth and prediction.

The mean squared error conveys a completely natural idea. Intuitively, the distance between ground truth and prediction should indicate how well our model fits the data. The closer they are, the better the fit.

Beyond this intuition, mean squared error has its roots in probability theory. For one, as the observations yᵢ can be thought of as samples from a probability distribution, MSE is the empirical expected value of

yif(xi)2.| y_i - f(x_i) |^2.

Minimizing the MSE is the same as minimizing the expected value of the error between prediction and ground truth. However, this interpretation does not offer an insight into why such a model would explain the dataset. Consider the following simple example below.

2021-08-mse-03-dataset.png

Notice that around each x\textstyle x, the observations y\textstyle y fluctuate and exhibit high variance. It seems like that a deterministic model y=f(x)y = f(x) is not enough to "explain" the data. Instead of fitting a function, let's look for a model that also accounts for the variance in the dataset. Finding a parametrized function like f(x)=ax+bf(x) = ax + b is not good enough. We need a model that can explain the variance of the data, not just its mean, unlike a linear regression would. So, let's model it with probability distributions instead of just a function!

Mean squared error as maximum likelihood

To explore this idea, let's go back to a simple special case and suppose that both our input and output are a single number. Say we are trying to predict the price of an apartment from its size in square foot.

Suppose that the data x\textstyle x and the observation y\textstyle y comes from the distributions X\textstyle X and Y\textstyle Y. What we are looking for is the conditional distribution of Y\textstyle Y given X\textstyle X. As it is a probability distribution, it provides a more refined understanding of the underlying process.

How can we approximate it? For one we can model it with Gaussian distributions, so that

PY(yX=x,f)=N(yf(x),σ2),P_Y(y | X = x, f) = \mathcal{N}(y | f(x), \sigma^2),

where f(x)f(x) describes the expected value function. To fit such a model, we can turn to maximum likelihood estimation. Here, the maximum likelihood function is

L(f,x,y)=i=1nPYi(yif,xi).\text{L}(f, \mathbf{x}, \mathbf{y}) = \prod_{i=1}^{n} P_{Y_i}(y_i | f, x_i).

After a bit of calculation, we obtain

L(f,x,y)=i=1nPYi(yif,xi)=i=1nN(yif(xi),σ2)=i=1n1σ2πe(yif(xi))22σ2\begin{align*} \text{L}(f, \mathbf{x}, \mathbf{y}) &= \prod_{i=1}^{n} P_{Y_i}(y_i | f, x_i) \\ &= \prod_{i=1}^{n} \mathcal{N}(y_i | f(x_i), \sigma^2) \\ &= \prod_{i=1}^{n} \frac{1}{\sigma \sqrt{2 \pi}} e^{- \frac{(y_i - f(x_i))^2}{2 \sigma^2}} \end{align*}

This is what we want to maximize. Now we do a trick that should be familiar by now: maximizing a function is the same as maximizing its logarithm. (Since the logarithm is monotone increasing.)

The purpose of this is to turn the product into a sum:

L(f,x,y)=i=1n[log1σ2π12σ2(yif(xi))2]2=nlog1σ2πi=1n12σ2(yif(xi))2\begin{align*} \text{L}(f, \mathbf{x}, \mathbf{y}) &= \sum_{i=1}^{n} \bigg[ \log \frac{1}{\sigma \sqrt{2 \pi}} - \frac{1}{2\sigma^2} \big( y_i - f(x_i) \big)^2 \bigg]^2 \\ &= n \log \frac{1}{\sigma \sqrt{2 \pi}} - \sum_{i=1}^{n} \frac{1}{2\sigma^2} \big( y_i - f(x_i) \big)^2 \end{align*}

Because we want to maximize the above formula in f\textstyle f, we can omit the terms where it is not present. Moreover, maximizing a function is the same as minimizing its negative. In the end, we are left with

argmaxfL(f,x,y)=argmaxfi=1n(yif(xi))2.\arg \max_{f} \text{L}(f, \mathbf{x}, \mathbf{y}) = \arg \max_{f} \sum_{i=1}^{n} \big( y_i - f(x_i) \big)^2.

This is almost the mean squared error! One thing is missing, though: scaling with the number of samples.

Taking the average of norms

The mean squared error is the mean of squared errors. Why not just take the sum? The reason is to keep the loss independent of the dataset size. Consider the two scenarios:

  1. A model that fits poorly on a dataset with 10 samples. 
  2. A model that fits excellently on a dataset with 1000 samples.

Without scaling taking the mean, the sum of squared errors can be the same in both cases. Roughly speaking,

i=110(some large number)ii=11000(some small number)i,\sum_{i=1}^{10} (\text{some large number})_i \approx \sum_{i=1}^{1000} (\text{some small number})_i,

which is not something we would like. By scaling, we have

110i=110(some large number)i>11000i=11000(some small number)i,\frac{1}{10} \sum_{i=1}^{10} (\text{some large number})_i > \frac{1}{1000} \sum_{i=1}^{1000} (\text{some small number})_i,

indicating the fact that the second model fits better. There is an additional benefit: it keeps the gradient in check. For large datasets, the gradient can be huge, throwing the optimization off by forcing it to take giant steps.

Having a deep understanding of math will make you a better engineer.

I want to help you with this, so I am writing a comprehensive book that takes you from high school math to the advanced stuff.
Join me on this journey and let's do this together!