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

Percentiles, Quantiles, and Quartiles

A percentile is incomplete without its interpolation rule.

2:34 clip1:53:00–1:55:34 in the full courseWatch on YouTube

Transcript

20 sentences · select one to jump there

Check your understanding

Was five hours observed in the sample?

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.

036-percentiles-quantiles-and-quartiles.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 036 of 120
 * Percentiles, Quantiles, and Quartiles
 * Module 04: Location, Ranking, and Exploratory Summaries
 *
 * Scenario: A percentile is incomplete without its interpolation rule
 * Rule:     type 7: h=(n−1)p, interpolate adjacent values
 *
 * Try it:   Was five hours observed in the sample?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/location-ranking-and-exploratory-summaries/percentiles-quantiles-and-quartiles/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson036() {
  const x = [1, 2, 2, 4, 9].sort((a,b) => a-b);
  const q = (p: number) => { // type-7 linear interpolation
    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 = {q25:q(.25), q50:q(.5), q75:q(.75), q80:q(.8)};
  return result;
}

export const checkedResult = {"q25":2,"q50":2,"q75":4,"q80":5.000000000000001};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "q25": 2,
  "q50": 2,
  "q75": 4,
  "q80": 5.000000000000001
}

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

Lesson notes

The rule

type 7: h=(n−1)p, interpolate adjacent values