Module 5 · Dispersion, Shape, and Robust Statistics Lesson 43 of 120

Mean Absolute Deviation

An interpretable average distance from a central delay.

2:22 clip2:17:38–2:20:00 in the full courseWatch on YouTube

Transcript

20 sentences · select one to jump there

Check your understanding

What happens after adding ten hours to every delay?

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.

043-mean-absolute-deviation.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 043 of 120
 * Mean Absolute Deviation
 * Module 05: Dispersion, Shape, and Robust Statistics
 *
 * Scenario: An interpretable average distance from a central delay
 * Rule:     mean absolute deviation = mean(|x − mean(x)|)
 *
 * Try it:   What happens after adding ten hours to every delay?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dispersion-shape-and-robust-statistics/mean-absolute-deviation/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson043() {
  const x = [1,2,2,4,9];
  const mean = x.reduce((a,b)=>a+b,0)/x.length;
  const distances = x.map(v=>Math.abs(v-mean));
  const result = distances.reduce((a,b)=>a+b,0)/x.length;
  return result;
}

export const checkedResult = 2.3200000000000003;

// Run this file directly: npx tsx lessons/05-dispersion-shape-and-robust-statistics/043-mean-absolute-deviation.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson043(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

2.3200000000000003

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

Lesson notes

The rule

mean absolute deviation = mean(|x − mean(x)|)