# Confusion on Mean Squared Error for Regression

In common statistical textbooks' linear regression topic, Mean Squared Error is often defined as $$MSE = \dfrac{(y-\hat{y})^T(y-\hat{y})}{n-p} = \dfrac{RSS}{n-p}$$ where the $$y$$ and $$\hat{y}$$ is a column vector containing each y and its predicted values respectively, RSS is the residual sum of squares, and n is the number of training samples, p being the number of variables including the intercept term. It can be shown that this definition of MSE is the unbiased estimator of the variance of the error term $$\varepsilon$$.

Now, the question comes, in Python's sklearn, or in fact in most ML books, MSE is just simply defined as the more intuitive (at least for me) $$MSE = \dfrac{RSS}{n}$$

Although one can argue as $$n \to \infty$$, both MSE converges to the same values. But I still get very confused over which one to use and why the difference? If someone can link me or explain to me it would be great; I tried searching but I dont see a common answer.

• Two points worth mentioning are 1) when you optimize the loss function to get parameter estimates, both denominators result in the same value that minimizes the loss (as does straight residual sum of squares) and 2) if you want to compare models to see which is performing better, as long as you use the same denominator for evaluating each model, it doesn't matter what denominator you use (again, RSS is adequate for this task, too). – Dave Feb 6 at 18:57

The formula that divides by $$n-p$$ is an estimator for unbiased mean squared error for regression, where you divide by the degrees of freedom. Imagine you would like to compare results returned by different machine learning algorithms, in many cases it would be hard to say what does exactly $$p$$ is, and for some models, like non-parametric models, or neural networks, $$n-p$$ could go all the way down to zero, or get negative, so this wouldn't have much sense. That is why in machine learning we use the simplified version, that works no matter what algorithms do you compare with each other.
• @nan it depends what you mean by "correct". In ML you focus on train/validation/test performance, dividing by $n$ is fine for this purpose. If you want to read about the differences between stats vs ML, check stats.stackexchange.com/questions/6/… – Tim Feb 5 at 16:00