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

Population and Sample Variance

Variance as a ledger of squared deviations.

2:35 clip2:22:32–2:25:08 in the full courseWatch on YouTube

Transcript

20 sentences · select one to jump there

Check your understanding

Why can two correct variance implementations disagree 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.

045-population-and-sample-variance.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 045 of 120
 * Population and Sample Variance
 * Module 05: Dispersion, Shape, and Robust Statistics
 *
 * Scenario: Variance as a ledger of squared deviations
 * Rule:     population: Σd²/n; sample: Σd²/(n−1)
 *
 * Try it:   Why can two correct variance implementations disagree here?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dispersion-shape-and-robust-statistics/population-and-sample-variance/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson045() {
  const x = [1,2,2,4,9];
  if (x.length < 2) throw new Error("Sample variance needs n >= 2");
  const mean = x.reduce((s,v)=>s+v,0)/x.length;
  const ss = x.reduce((s,v)=>s+(v-mean)**2,0);
  const result = {ss, population: ss/x.length,
    sample: ss/(x.length-1)};
  return result;
}

export const checkedResult = {"ss":41.2,"population":8.24,"sample":10.3};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "ss": 41.2,
  "population": 8.24,
  "sample": 10.3
}

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

Lesson notes

The rule

population: Σd²/n; sample: Σd²/(n−1)