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

Histograms and Empirical Distribution Functions

A histogram changes with bins; an ECDF preserves sample order.

2:29 clip2:00:52–2:03:21 in the full courseWatch on YouTube

Transcript

18 sentences · select one to jump there

Check your understanding

Where does 9 go with these half-open bins?

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.

039-histograms-and-empirical-distribution-functions.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 039 of 120
 * Histograms and Empirical Distribution Functions
 * Module 04: Location, Ranking, and Exploratory Summaries
 *
 * Scenario: A histogram changes with bins; an ECDF preserves sample order
 * Rule:     Fₙ(x) = count(Xᵢ ≤ x)/n
 *
 * Try it:   Where does 9 go with these half-open bins?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/location-ranking-and-exploratory-summaries/histograms-and-empirical-distribution-functions/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson039() {
  const x = [1,2,2,4,9];
  const edges = [0,3,6,9,12];
  const counts = edges.slice(0,-1).map((lo,i) =>
    x.filter(v => v>=lo && v<edges[i+1]).length);
  const ecdfAt2 = x.filter(v=>v<=2).length/x.length;
  const result = {counts, ecdfAt2};
  return result;
}

export const checkedResult = {"counts":[3,1,0,1],"ecdfAt2":0.6};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "counts": [
    3,
    1,
    0,
    1
  ],
  "ecdfAt2": 0.6
}

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

Lesson notes

The rule

Fₙ(x) = count(Xᵢ ≤ x)/n