Residuals, MAE, MSE, and RMSE
Install and import#
npm install fintech-algorithmsimport { residualsMaeMseAndRmse } from "fintech-algorithms/foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse";Signature#
residualsMaeMseAndRmse(input)Returns the least squares residuals of input.y on input.x together with the three usual summaries of their size.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | One record holding the predictor series x and the outcome series y, aligned position by position.x: non-empty list of finite numbers, at least two, not all identical · y: finite numbers, same length as x |
Returns#
D00Output
An object with residuals (observed minus fitted, one per observation), mae, mse and rmse.
Errors#
- When x or y is missing, empty, or contains a non-finite number — throws RangeError
- When x and y have different lengths, or fewer than two observations — the least-squares fit is computed for every topic in the family before the topic branch is taken — throws RangeError
- When x is constant, which leaves the regression slope undefined — throws RangeError
Complexity: time O(n),
space O(n).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
{
"x": [1, 2, 3, 4, 5, 6],
"y": [2, 3, 5, 4, 6, 7],
"groups": ["A", "A", "A", "B", "B", "B"],
"predictX": 7,
"otherPredictor": [2, 4, 5, 8, 9, 13]
}Call#
residualsMaeMseAndRmse(input)Returns#
object with 2 fields: residuals, mae
{
"residuals": [
-0.14285714285714324,
-0.08571428571428585,
0.9714285714285715,
-0.9714285714285715,
0.08571428571428541,
0.14285714285714324
],
"mae": 0.40000000000000013
}Diagrams#
Calculation flow#
Reasoning flow — D00-F09-A08
flowchart LR
A["Synthetic input + metadata"] --> B{"Contract valid?"}
B -->|No| C["Reject or route with reason"]
B -->|Yes| D["Apply Residuals, MAE, MSE, and RMSE"]
D --> E["Formula: eᵢ=yᵢ−ŷᵢ; MAE=mean|e|; MSE=mean e²; RMSE=√MSE"]
E --> F["Verified fixture output"]
F --> G{"Interpretation within boundary?"}
G -->|Yes| H["Report value + convention + audit"]
G -->|No| I["Add companion view or narrower claim"]
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- Linear Least Squares Regression — NIST/SEMATECH e-Handbook
- Correlation — NIST/SEMATECH e-Handbook
- Historical-example decision