# Mean squared error

In statistics, the mean squared error (MSE) or mean squared deviation (MSD) of an estimator (of a procedure for estimating an unobserved quantity) measures the average of the squares of the errors—that is, the average squared difference between the estimated values and what is estimated. MSE is a risk function, corresponding to the expected value of the squared error loss. The fact that MSE is almost always strictly positive (and not zero) is because of randomness or because the estimator does not account for information that could produce a more accurate estimate.[1]

The MSE is a measure of the quality of an estimator—it is always non-negative, and values closer to zero are better.

The MSE is the second moment (about the origin) of the error, and thus incorporates both the variance of the estimator (how widely spread the estimates are from one data sample to another) and its bias (how far off the average estimated value is from the truth). For an unbiased estimator, the MSE is the variance of the estimator. Like the variance, MSE has the same units of measurement as the square of the quantity being estimated. In an analogy to standard deviation, taking the square root of MSE yields the root-mean-square error or root-mean-square deviation (RMSE or RMSD), which has the same units as the quantity being estimated; for an unbiased estimator, the RMSE is the square root of the variance, known as the standard error.

## Definition and basic properties

The MSE assesses the quality of a predictor (i.e., a function mapping arbitrary inputs to a sample of values of some random variable), or an estimator (i.e., a mathematical function mapping a sample of data to an estimate of a parameter of the population from which the data is sampled). The definition of an MSE differs according to whether one is describing a predictor or an estimator.

### Predictor

If a vector of ${\displaystyle n}$  predictions generated from a sample of n data points on all variables, and ${\displaystyle Y}$  is the vector of observed values of the variable being predicted, then the within-sample MSE of the predictor is computed as

${\displaystyle \operatorname {MSE} ={\frac {1}{n}}\sum _{i=1}^{n}(Y_{i}-{\hat {Y_{i}}})^{2}.}$

I.e., the MSE is the mean ${\displaystyle \left({\frac {1}{n}}\sum _{i=1}^{n}\right)}$  of the squares of the errors ${\displaystyle (Y_{i}-{\hat {Y_{i}}})^{2}}$ . This is an easily computable quantity for a particular sample (and hence is sample-dependent).

The MSE can also be computed on q data points that were not used in estimating the model, either because they were held back for this purpose or because these data have been newly obtained. In this process, which is known as cross-validation, the MSE is often called the mean squared prediction error, and is computed as

${\displaystyle \operatorname {MSPE} ={\frac {1}{q}}\sum _{i=n+1}^{n+q}(Y_{i}-{\hat {Y_{i}}})^{2}.}$

### Estimator

The MSE of an estimator ${\displaystyle {\hat {\theta }}}$  with respect to an unknown parameter ${\displaystyle \theta }$  is defined as

${\displaystyle \operatorname {MSE} ({\hat {\theta }})=\operatorname {E} _{\hat {\theta }}\left[({\hat {\theta }}-\theta )^{2}\right].}$

This definition depends on the unknown parameter, but the MSE is a priori a property of an estimator. Since an MSE is an expectation, it is not a random variable. That being said, the MSE could be a function of unknown parameters, in which case any estimator of the MSE based on estimates of these parameters would be a function of the data and thus a random variable. If the estimator ${\displaystyle {\hat {\theta }}}$  is derived from a sample statistic and is used to estimate some population statistic, then the expectation is with respect to the sampling distribution of the sample statistic.

The MSE can be written as the sum of the variance of the estimator and the squared bias of the estimator, providing a useful way to calculate the MSE and implying that in the case of unbiased estimators, the MSE and variance are equivalent.[2]

${\displaystyle \operatorname {MSE} ({\hat {\theta }})=\operatorname {Var} _{\hat {\theta }}({\hat {\theta }})+\operatorname {Bias} ({\hat {\theta }},\theta )^{2}.}$

#### Proof of variance and bias relationship

{\displaystyle {\begin{aligned}\operatorname {MSE} ({\hat {\theta }})&=\operatorname {E} _{\hat {\theta }}\left[({\hat {\theta }}-\theta )^{2}\right]\\&=\operatorname {E} _{\hat {\theta }}\left[\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]+\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)^{2}\right]\\&=\operatorname {E} _{\hat {\theta }}\left[\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)^{2}+2\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)+\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)^{2}\right]\\&=\operatorname {E} _{\hat {\theta }}\left[\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)^{2}\right]+\operatorname {E} _{\hat {\theta }}\left[2\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)\right]+\operatorname {E} _{\hat {\theta }}\left[\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)^{2}\right]\\&=\operatorname {E} _{\hat {\theta }}\left[\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)^{2}\right]+2\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)\operatorname {E} _{\hat {\theta }}\left[{\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right]+\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)^{2}&&\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta ={\text{const.}}\\&=\operatorname {E} _{\hat {\theta }}\left[\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)^{2}\right]+2\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)+\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)^{2}&&\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]={\text{const.}}\\&=\operatorname {E} _{\hat {\theta }}\left[\left({\hat {\theta }}-\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]\right)^{2}\right]+\left(\operatorname {E} _{\hat {\theta }}[{\hat {\theta }}]-\theta \right)^{2}\\&=\operatorname {Var} _{\hat {\theta }}({\hat {\theta }})+\operatorname {Bias} _{\hat {\theta }}({\hat {\theta }},\theta )^{2}\end{aligned}}}

## Regression

In regression analysis, the term mean squared error is sometimes used to refer to the unbiased estimate of error variance: the residual sum of squares divided by the number of degrees of freedom. This definition for a known, computed quantity differs from the above definition for the computed MSE of a predictor in that a different denominator is used. The denominator is the sample size reduced by the number of model parameters estimated from the same data, (n-p) for p regressors or (n-p-1) if an intercept is used.[3] For more details, see errors and residuals in statistics. Note that, although the MSE (as defined in the present article) is not an unbiased estimator of the error variance, it is consistent, given the consistency of the predictor.

Also in regression analysis, "mean squared error", often referred to as mean squared prediction error or "out-of-sample mean squared error", can refer to the mean value of the squared deviations of the predictions from the true values, over an out-of-sample test space, generated by a model estimated over a particular sample space. This also is a known, computed quantity, and it varies by sample and by out-of-sample test space.

## Examples

### Mean

Suppose we have a random sample of size ${\displaystyle n}$  from a population, ${\displaystyle X_{1},\dots ,X_{n}}$ . Suppose the sample units were chosen with replacement. That is, the ${\displaystyle n}$  units are selected one at a time, and previously selected units are still eligible for selection for all ${\displaystyle n}$  draws. The usual estimator for the ${\displaystyle \mu }$  is the sample average

${\displaystyle {\overline {X}}={\frac {1}{n}}\sum _{i=1}^{n}X_{i}}$

which has an expected value equal to the true mean ${\displaystyle \mu }$  (so it is unbiased) and a mean square error of

${\displaystyle \operatorname {MSE} \left({\overline {X}}\right)=\operatorname {E} \left[\left({\overline {X}}-\mu \right)^{2}\right]=\left({\frac {\sigma }{\sqrt {n}}}\right)^{2}={\frac {\sigma ^{2}}{n}}}$

where ${\displaystyle \sigma ^{2}}$  is the population variance.

For a Gaussian distribution this is the best unbiased estimator (that is, it has the lowest MSE among all unbiased estimators), but not, say, for a uniform distribution.

### Variance

The usual estimator for the variance is the corrected sample variance:

${\displaystyle S_{n-1}^{2}={\frac {1}{n-1}}\sum _{i=1}^{n}\left(X_{i}-{\overline {X}}\right)^{2}={\frac {1}{n-1}}\left(\sum _{i=1}^{n}X_{i}^{2}-n{\overline {X}}^{2}\right).}$

This is unbiased (its expected value is ${\displaystyle \sigma ^{2}}$ ), hence also called the unbiased sample variance, and its MSE is[4]

${\displaystyle \operatorname {MSE} (S_{n-1}^{2})={\frac {1}{n}}\left(\mu _{4}-{\frac {n-3}{n-1}}\sigma ^{4}\right)={\frac {1}{n}}\left(\gamma _{2}+{\frac {2n}{n-1}}\right)\sigma ^{4},}$

where ${\displaystyle \mu _{4}}$  is the fourth central moment of the distribution or population and ${\displaystyle \gamma _{2}=\mu _{4}/\sigma ^{4}-3}$  is the excess kurtosis.

However, one can use other estimators for ${\displaystyle \sigma ^{2}}$  which are proportional to ${\displaystyle S_{n-1}^{2}}$ , and an appropriate choice can always give a lower mean square error. If we define

${\displaystyle S_{a}^{2}={\frac {n-1}{a}}S_{n-1}^{2}={\frac {1}{a}}\sum _{i=1}^{n}\left(X_{i}-{\overline {X}}\,\right)^{2}}$

then we calculate:

{\displaystyle {\begin{aligned}\operatorname {MSE} (S_{a}^{2})&=\operatorname {E} \left[\left({\frac {n-1}{a}}S_{n-1}^{2}-\sigma ^{2}\right)^{2}\right]\\&=\operatorname {E} \left[{\frac {(n-1)^{2}}{a^{2}}}S_{n-1}^{4}-2\left({\frac {n-1}{a}}S_{n-1}^{2}\right)\sigma ^{2}+\sigma ^{4}\right]\\&={\frac {(n-1)^{2}}{a^{2}}}\operatorname {E} \left[S_{n-1}^{4}\right]-2\left({\frac {n-1}{a}}\right)\operatorname {E} \left[S_{n-1}^{2}\right]\sigma ^{2}+\sigma ^{4}\\&={\frac {(n-1)^{2}}{a^{2}}}\operatorname {E} \left[S_{n-1}^{4}\right]-2\left({\frac {n-1}{a}}\right)\sigma ^{4}+\sigma ^{4}&&\operatorname {E} \left[S_{n-1}^{2}\right]=\sigma ^{2}\\&={\frac {(n-1)^{2}}{a^{2}}}\left({\frac {\gamma _{2}}{n}}+{\frac {n+1}{n-1}}\right)\sigma ^{4}-2\left({\frac {n-1}{a}}\right)\sigma ^{4}+\sigma ^{4}&&\operatorname {E} \left[S_{n-1}^{4}\right]=\operatorname {MSE} (S_{n-1}^{2})+\sigma ^{4}\\&={\frac {n-1}{na^{2}}}\left((n-1)\gamma _{2}+n^{2}+n\right)\sigma ^{4}-2\left({\frac {n-1}{a}}\right)\sigma ^{4}+\sigma ^{4}\end{aligned}}}

This is minimized when

${\displaystyle a={\frac {(n-1)\gamma _{2}+n^{2}+n}{n}}=n+1+{\frac {n-1}{n}}\gamma _{2}.}$

For a Gaussian distribution, where ${\displaystyle \gamma _{2}=0}$ , this means the MSE is minimized when dividing the sum by ${\displaystyle a=n+1}$ . The minimum excess kurtosis is ${\displaystyle \gamma _{2}=-2}$ ,[a] which is achieved by a Bernoulli distribution with p = 1/2 (a coin flip), and the MSE is minimized for ${\displaystyle a=n-1+{\tfrac {2}{n}}.}$  So no matter what the kurtosis, we get a "better" estimate (in the sense of having a lower MSE) by scaling down the unbiased estimator a little bit; this is a simple example of a shrinkage estimator: one "shrinks" the estimator towards zero (scales down the unbiased estimator).

Further, while the corrected sample variance is the best unbiased estimator (minimum mean square error among unbiased estimators) of variance for Gaussian distributions, if the distribution is not Gaussian then even among unbiased estimators, the best unbiased estimator of the variance may not be ${\displaystyle S_{n-1}^{2}.}$

### Gaussian distribution

The following table gives several estimators of the true parameters of the population, μ and σ2, for the Gaussian case.[5]

True value Estimator Mean squared error
${\displaystyle \theta =\mu }$  ${\displaystyle {\hat {\theta }}}$  = the unbiased estimator of the population mean, ${\displaystyle {\overline {X}}={\frac {1}{n}}\sum _{i=1}^{n}(X_{i})}$  ${\displaystyle \operatorname {MSE} ({\overline {X}})=\operatorname {E} (({\overline {X}}-\mu )^{2})=\left({\frac {\sigma }{\sqrt {n}}}\right)^{2}}$
${\displaystyle \theta =\sigma ^{2}}$  ${\displaystyle {\hat {\theta }}}$  = the unbiased estimator of the population variance, ${\displaystyle S_{n-1}^{2}={\frac {1}{n-1}}\sum _{i=1}^{n}\left(X_{i}-{\overline {X}}\,\right)^{2}}$  ${\displaystyle \operatorname {MSE} (S_{n-1}^{2})=\operatorname {E} ((S_{n-1}^{2}-\sigma ^{2})^{2})={\frac {2}{n-1}}\sigma ^{4}}$
${\displaystyle \theta =\sigma ^{2}}$  ${\displaystyle {\hat {\theta }}}$  = the biased estimator of the population variance, ${\displaystyle S_{n}^{2}={\frac {1}{n}}\sum _{i=1}^{n}\left(X_{i}-{\overline {X}}\,\right)^{2}}$  ${\displaystyle \operatorname {MSE} (S_{n}^{2})=\operatorname {E} ((S_{n}^{2}-\sigma ^{2})^{2})={\frac {2n-1}{n^{2}}}\sigma ^{4}}$
${\displaystyle \theta =\sigma ^{2}}$  ${\displaystyle {\hat {\theta }}}$  = the biased estimator of the population variance, ${\displaystyle S_{n+1}^{2}={\frac {1}{n+1}}\sum _{i=1}^{n}\left(X_{i}-{\overline {X}}\,\right)^{2}}$  ${\displaystyle \operatorname {MSE} (S_{n+1}^{2})=\operatorname {E} ((S_{n+1}^{2}-\sigma ^{2})^{2})={\frac {2}{n+1}}\sigma ^{4}}$

## Interpretation

An MSE of zero, meaning that the estimator ${\displaystyle {\hat {\theta }}}$  predicts observations of the parameter ${\displaystyle \theta }$  with perfect accuracy, is the ideal, but is typically not possible.

Values of MSE may be used for comparative purposes. Two or more statistical models may be compared using their MSEs as a measure of how well they explain a given set of observations: An unbiased estimator (estimated from a statistical model) with the smallest variance among all unbiased estimators is the best unbiased estimator or MVUE (Minimum Variance Unbiased Estimator).

Both linear regression techniques such as analysis of variance estimate the MSE as part of the analysis and use the estimated MSE to determine the statistical significance of the factors or predictors under study. The goal of experimental design is to construct experiments in such a way that when the observations are analyzed, the MSE is close to zero relative to the magnitude of at least one of the estimated treatment effects.

MSE is also used in several stepwise regression techniques as part of the determination as to how many predictors from a candidate set to include in a model for a given set of observations.

## Applications

• Minimizing MSE is a key criterion in selecting estimators: see minimum mean-square error. Among unbiased estimators, minimizing the MSE is equivalent to minimizing the variance, and the estimator that does this is the minimum variance unbiased estimator. However, a biased estimator may have lower MSE; see estimator bias.
• In statistical modelling the MSE can represent the difference between the actual observations and the observation values predicted by the model. In this context, it is used to determine the extent to which the model fits the data as well as whether removing some explanatory variables is possible without significantly harming the model's predictive ability.

## Loss function

Squared error loss is one of the most widely used loss functions in statistics, though its widespread use stems more from mathematical convenience than considerations of actual loss in applications. Carl Friedrich Gauss, who introduced the use of mean squared error, was aware of its arbitrariness and was in agreement with objections to it on these grounds.[1] The mathematical benefits of mean squared error are particularly evident in its use at analyzing the performance of linear regression, as it allows one to partition the variation in a dataset into variation explained by the model and variation explained by randomness.

### Criticism

The use of mean squared error without question has been criticized by the decision theorist James Berger. Mean squared error is the negative of the expected value of one specific utility function, the quadratic utility function, which may not be the appropriate utility function to use under a given set of circumstances. There are, however, some scenarios where mean squared error can serve as a good approximation to a loss function occurring naturally in an application.[6]

Like variance, mean squared error has the disadvantage of heavily weighting outliers.[7] This is a result of the squaring of each term, which effectively weights large errors more heavily than small ones. This property, undesirable in many applications, has led researchers to use alternatives such as the mean absolute error, or those based on the median.

## Notes

1. ^ This can be proved by Jensen's inequality as follows. The fourth central moment is an upper bound for the square of variance, so that the least value for their ratio is one, therefore, the least value for the excess kurtosis is −2, achieved, for instance, by a Bernoulli with p=1/2.

## References

1. ^ a b Lehmann, E. L.; Casella, George (1998). Theory of Point Estimation (2nd ed.). New York: Springer. ISBN 978-0-387-98502-2. MR 1639875.
2. ^ Wackerly, Dennis; Mendenhall, William; Scheaffer, Richard L. (2008). Mathematical Statistics with Applications (7 ed.). Belmont, CA, USA: Thomson Higher Education. ISBN 978-0-495-38508-0.
3. ^ Steel, R.G.D, and Torrie, J. H., Principles and Procedures of Statistics with Special Reference to the Biological Sciences., McGraw Hill, 1960, page 288.
4. ^ Mood, A.; Graybill, F.; Boes, D. (1974). Introduction to the Theory of Statistics (3rd ed.). McGraw-Hill. p. 229.
5. ^ DeGroot, Morris H. (1980). Probability and Statistics (2nd ed.). Addison-Wesley.
6. ^ Berger, James O. (1985). "2.4.2 Certain Standard Loss Functions". Statistical Decision Theory and Bayesian Analysis (2nd ed.). New York: Springer-Verlag. p. 60. ISBN 978-0-387-96098-2. MR 0804611.
7. ^ Bermejo, Sergio; Cabestany, Joan (2001). "Oriented principal component analysis for large margin classifiers". Neural Networks. 14 (10): 1447–1461. doi:10.1016/S0893-6080(01)00106-X. Archived from the original on 2009-01-23.