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

Range and Interquartile Range

Separating the overall spread from the typical middle spread.

2:23 clip2:15:14–2:17:38 in the full courseWatch on YouTube

Transcript

20 sentences · select one to jump there

Check your understanding

Does IQR use every observation magnitude directly?

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.

042-range-and-interquartile-range.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 042 of 120
 * Range and Interquartile Range
 * Module 05: Dispersion, Shape, and Robust Statistics
 *
 * Scenario: Separating the overall spread from the typical middle spread
 * Rule:     range = max − min; IQR = Q₇₅ − Q₂₅
 *
 * Try it:   Does IQR use every observation magnitude directly?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dispersion-shape-and-robust-statistics/range-and-interquartile-range/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson042() {
  const x = [1,2,2,4,9].sort((a,b)=>a-b);
  const q = (p: number) => { const h=(x.length-1)*p, i=Math.floor(h);
    return x[i] + (h-i)*(x[Math.min(i+1,x.length-1)]-x[i]); };
  const result = {range: x[x.length-1]-x[0], iqr: q(.75)-q(.25)};
  return result;
}

export const checkedResult = {"range":8,"iqr":2};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "range": 8,
  "iqr": 2
}

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

Lesson notes

The rule

range = max − min; IQR = Q₇₅ − Q₂₅