Module 4 · Location, Ranking, and Exploratory Summaries Lesson 40 of 120

Five-Number Summary and Box Plot

A box-plot fence is not an observed whisker.

2:30 clip2:03:21–2:05:52 in the full courseWatch on YouTube

Transcript

20 sentences · select one to jump there

Check your understanding

Is seven an observed upper whisker here?

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.

040-five-number-summary-and-box-plot.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 040 of 120
 * Five-Number Summary and Box Plot
 * Module 04: Location, Ranking, and Exploratory Summaries
 *
 * Scenario: A box-plot fence is not an observed whisker
 * Rule:     fences = Q1 − 1.5 IQR, Q3 + 1.5 IQR
 *
 * Try it:   Is seven an observed upper whisker here?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/location-ranking-and-exploratory-summaries/five-number-summary-and-box-plot/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson040() {
  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 q1 = q(.25), median = q(.5), q3 = q(.75);
  const iqr = q3-q1, lo=q1-1.5*iqr, hi=q3+1.5*iqr;
  const inside = x.filter(v=>v>=lo && v<=hi);
  const result = {five:[x[0],q1,median,q3,x[x.length-1]], fences:[lo,hi],
    whiskers:[Math.min(...inside),Math.max(...inside)], outliers:x.filter(v=>v<lo||v>hi)};
  return result;
}

export const checkedResult = {"five":[1,2,2,4,9],"fences":[-1,7],"whiskers":[1,4],"outliers":[9]};

// Run this file directly: npx tsx lessons/04-location-ranking-and-exploratory-summaries/040-five-number-summary-and-box-plot.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson040(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "five": [
    1,
    2,
    2,
    4,
    9
  ],
  "fences": [
    -1,
    7
  ],
  "whiskers": [
    1,
    4
  ],
  "outliers": [
    9
  ]
}

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

Lesson notes

The rule

fences = Q1 − 1.5 IQR, Q3 + 1.5 IQR