Prediction error and coefficient of determination against observed values
score_prediction_error
Works out MSE, RMSE, MAE and the coefficient of determination R² of predicted against observed values.
- Runs
- On the server
- Level
- Checked against a reference: exact arithmetic (Python fractions; one square root at 60 digits), checked against scikit-learn 1.9.1 and eighteen values the primary source prints
What it is
Returns MSE, RMSE, MAE and the coefficient of determination R² of predicted against observed values. Use when a model's numeric predictions are scored against measurements; R² is one minus squared error over the observations' spread, and can be negative. Not for a correlation coefficient: use `relate_correlation`; not for fitting a line: use `relate_linear_regression`; not for comparing two measurement methods: use `relate_method_comparison`. Both vectors share one unit and one length, 2 to 4,000 pairs; R² is withheld when every observation is equal. A measurement's unit is required, never assumed: any unit of the kit's table, returned in its canonical spelling; a unit group takes one unit, never converted.
Field: Statistics and design.
What it does
You give
observed(required): The observed (measured) values, one per pair, in the order of the predictions. Unit group values (absolute).predicted(required): The model's predicted values, one per pair, in the same unit and order as the observations. Unit group values (absolute).
No request is recorded for this tool.
Limits
- Research arithmetic scoring predictions against outcomes you supply; it does not judge a model, choose a threshold or know whether the cases were held out, and is not for diagnosis or treatment decisions.
- R² is one minus the squared error over the spread of the observations about their mean: not a squared correlation, negative when predictions do worse than that mean, withheld when all observations are equal.
- Errors are means over the pairs.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.
It will not answer when
- an error, a deviation from the observed mean, one of their squares, a sum of them, the mean squared or mean absolute error, or the ratio of the two sums of squares is not finite, or is not zero and below the smallest normal double.
- the observed and the predicted vectors have different lengths: the values are scored pair by pair.
Every input, with its units2 inputs
observed(required): The observed (measured) values, one per pair, in the order of the predictions. Unit group values (absolute).predicted(required): The model's predicted values, one per pair, in the same unit and order as the observations. Unit group values (absolute).
Every refusal, with its code2 refusals
| Code | When |
|---|---|
RESULT_OUTSIDE_DOUBLE_RANGE | an error, a deviation from the observed mean, one of their squares, a sum of them, the mean squared or mean absolute error, or the ratio of the two sums of squares is not finite, or is not zero and below the smallest normal double. |
LENGTHS_DIFFER | the observed and the predicted vectors have different lengths: the values are scored pair by pair. |
The calculation
The method's equations, constants and worked example.
Equation 1
MSE = (1/m)·Σᵢ (X_i − Y_i)²
| Symbol | Meaning |
|---|---|
X_i | the predicted value of pair i (argument predicted), as-given |
Y_i | the observed value of pair i (argument observed), as-given |
m | the number of pairs |
MSE | mean squared error, returned as meanSquaredError, as-given^2 |
Source 1.
Equation 2
RMSE = √MSE
| Symbol | Meaning |
|---|---|
MSE | mean squared error, as-given^2 |
RMSE | root mean squared error, returned as rootMeanSquaredError, as-given |
Source 1.
Equation 3
| Symbol | Meaning |
|---|---|
X_i | the predicted value of pair i (argument predicted), as-given |
Y_i | the observed value of pair i (argument observed), as-given |
m | the number of pairs |
MAE | mean absolute error, returned as meanAbsoluteError, as-given |
Source 1.
Equation 4
Ȳ = (1/m)·Σᵢ Y_i
R² = 1 − Σᵢ (X_i − Y_i)² / Σᵢ (Ȳ − Y_i)²
| Symbol | Meaning |
|---|---|
X_i | the predicted value of pair i (argument predicted), as-given |
Y_i | the observed value of pair i (argument observed), as-given |
m | the number of pairs |
Ȳ | the mean of the observed values, as-given |
R² | coefficient of determination, returned as rSquared; at most 1, with no lower bound; undefined when every observation is equal |
Source 1.
A definition, not a model: the equation states what the quantity means. A different convention gives a different number.
Worked example
Worked example: one case of score-prediction-error@1.0.2, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.
Inputs
- observed.values[0] = 5
- observed.values[1] = 6
- observed.values[2] = 7
- observed.values[3] = 8
- observed.values[4] = 9
- observed.unit = 1
- predicted.values[0] = 5.5
- predicted.values[1] = 5.5
- predicted.values[2] = 7.5
- predicted.values[3] = 8
- predicted.values[4] = 8
- predicted.unit = 1
Steps
- squaredErrorSum = 1.75, from observed, predicted; by equation 1.
- mse = 0.35, from squaredErrorSum; by equation 1; gives meanSquaredError.
- rmse = 0.59160798, from mse; by equation 2; gives rootMeanSquaredError.
- absoluteErrorSum = 2.5, from observed, predicted; by equation 3.
- mae = 0.5, from absoluteErrorSum; by equation 3; gives meanAbsoluteError.
- observedMean = 7, from observed; by equation 4.
- spread = 10, from observed, observedMean; by equation 4.
- rSquared = 0.825, from squaredErrorSum, spread; by equation 4; gives rSquared.
The working, as the case writes it:
errors 0.5, -0.5, 0.5, 0, -1
Σ(X − Y)² = 1.75, so MSE = 1.75/5 = 0.35 and RMSE = 0.59160798
Σ|X − Y| = 2.5, so MAE = 0.5
the observations' mean is 7 and Σ(Ȳ − Y)² = 10, so R² = 1 − 1.75/10 = 0.825 (worked by hand by Drybench on 12 November 2026
not an independent check)
Answer
meanSquaredError = 0.35
rootMeanSquaredError = 0.59160798
meanAbsoluteError = 0.5
rSquared = 0.825
Where these values came from: our own exact-arithmetic script and a hand-worked case; stored reference values (fractions; 60-digit decimal square root).
At build the tool returned this answer within the tolerance the file states: MSE, RMSE, MAE and R² relative 1e-9, with a floor of 1e-12 below 1e-3; a case in another unit states the same rule in its own units; statuses, codes, the withholding rule and units exact.
Sources
Chicco D, Warrens MJ, Jurman G, The coefficient of determination R-squared is more informative than SMAPE, MAE, MAPE, MSE and RMSE in regression analysis evaluation, PeerJ Comput Sci 7:e623 (2021)
Used for: Prediction error and coefficient of determination of predicted against observed values. At: equation 4, section Methods, Mathematical background; equation 5, section Methods, Mathematical background; equation 6, section Methods, Mathematical background; equation 1 and 3, section Methods, Mathematical background.
Read in full, 12 November 2026
How it is checked
- Level
Checked against a reference.
- Checked against
Our own exact-arithmetic script and a hand-worked case; the source's own worked example, Chicco, Warrens and Jurman 2021, Table 1 and UC2 (printed values), version of record.
- Cases
No case set is linked to this version.
- Not checked
How the pairs were chosen and whether they were held out; the source's other error measures (MAPE, SMAPE), which are not computed; its UC2 start case, whose formula zeroes nine points while its printed value is that of ten; observations whose spread is within rounding of their mean; logarithms other than base 10, which are not taken; a squared unit carries a UCUM code only for the units the tool lists; any sampling interval.
Changes to its methods0 changes
No change is logged for these method versions.