All formulas

Statistics

Mean squared error

stats.mseAlso called mean squared error, mse, l2 loss, squared error

MSE=1n∑i=1n(yi−y^i)2\mathrm{MSE} = \frac{1}{n}\sum_{i=1}^{n} (y_i - \hat{y}_i)^2

Average of the squared differences between observed and predicted values.

Notation

yy

observed values

y ∈ ℝn

y^\hat{y}

predicted values

ŷ ∈ ℝn

nn

number of observations

Size, at least 1

MSE\mathrm{MSE}

Result: the mean squared error

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    y=[1,2,3],y^=[1,1,5]y = [1, 2, 3], \hat{y} = [1, 1, 5]

    Result

    MSE=53\displaystyle \mathrm{MSE} = \frac{5}{3}

    ≈ 1.66666666667

  2. Given

    y=[0.5,−1],y^=[1,1]y = [0.5, -1], \hat{y} = [1, 1]

    Result

    MSE=178\displaystyle \mathrm{MSE} = \frac{17}{8}

    ≈ 2.125

  3. Given

    y=[3],y^=[3]y = [3], \hat{y} = [3]

    Result

    MSE=0\displaystyle \mathrm{MSE} = 0

Common mistakes

  • Sums instead of averaging

    The squared errors are summed, giving n times the mean.

    Fix: Divide by n (reduction='mean').

  • Includes a factor of 1/2

    The 1/2 used to simplify gradients is folded into the reported value.

    Fix: Drop the 1/2 when the quantity is reported as the MSE.

  • Returns the root mean squared error

    A square root is applied to the mean.

    Fix: Remove the square root.

Source

  • Murphy 2022

    Murphy, Kevin P. Probabilistic Machine Learning: An Introduction. MIT Press, 2022.