Module 9 · Dependence, Regression, and Model Foundations Lesson 88 of 120

Residuals, MAE, MSE, and RMSE

Choosing an error metric that reflects the cost of mistakes.

2:34 clip4:41:12–4:43:47 in the full courseWatch on YouTube

Transcript

19 sentences · select one to jump there

Check your understanding

Why does RMSE have the original output units while MSE does not?

Choose one answer

Code lab

Run it yourself

The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.

088-residuals-mae-mse-and-rmse.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 088 of 120
 * Residuals, MAE, MSE, and RMSE
 * Module 09: Dependence, Regression, and Model Foundations
 *
 * Scenario: Choosing an error metric that reflects the cost of mistakes
 * Rule:     MAE=mean|e|; MSE=mean(e²); RMSE=√MSE
 *
 * Try it:   Why does RMSE have the original output units while MSE does not?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/residuals-mae-mse-and-rmse/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson088() {
  const actual=[2,4,5,4,5],predicted=[2.8,3.4,4,4.6,5.2];
  const residuals=actual.map((v,i)=>v-predicted[i]);
  const mae=residuals.reduce((s,e)=>s+Math.abs(e),0)/residuals.length;
  const mse=residuals.reduce((s,e)=>s+e*e,0)/residuals.length;
  const result={residuals,mae,mse,rmse:Math.sqrt(mse)};
  return result;
}

export const checkedResult = {"residuals":[-0.7999999999999998,0.6000000000000001,1,-0.5999999999999996,-0.20000000000000018],"mae":0.6399999999999999,"mse":0.47999999999999987,"rmse":0.6928203230275508};

// Run this file directly: npx tsx lessons/09-dependence-regression-and-model-foundations/088-residuals-mae-mse-and-rmse.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson088(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "residuals": [
    -0.7999999999999998,
    0.6000000000000001,
    1,
    -0.5999999999999996,
    -0.20000000000000018
  ],
  "mae": 0.6399999999999999,
  "mse": 0.47999999999999987,
  "rmse": 0.6928203230275508
}

Prefer your own machine? Every file is in the course repository · open it in Codespaces.

Lesson notes

The rule

MAE=mean|e|; MSE=mean(e²); RMSE=√MSE