Module 6 · Probability and Random Variables Lesson 58 of 120

Variance, Moments, and Moment-Generating Intuition

Measuring uncertainty around an expected claim count.

2:39 clip3:02:45–3:05:25 in the full courseWatch on YouTube

Transcript

19 sentences · select one to jump there

Check your understanding

Is E[X²] generally equal to E[X]²?

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.

058-variance-moments-and-moment-generating-intuition.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 058 of 120
 * Variance, Moments, and Moment-Generating Intuition
 * Module 06: Probability and Random Variables
 *
 * Scenario: Measuring uncertainty around an expected claim count
 * Rule:     Var(X) = E[X²] − E[X]²
 *
 * Try it:   Is E[X²] generally equal to E[X]²?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/probability-and-random-variables/variance-moments-and-moment-generating-intuition/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson058() {
  const x=[0,1,2], p=[.2,.5,.3];
  const moment=(k:number)=>x.reduce((s,v,i)=>s+p[i]*v**k,0);
  const mean=moment(1), second=moment(2);
  const mgf=(t:number)=>x.reduce((s,v,i)=>s+p[i]*Math.exp(t*v),0);
  const result={mean,second,variance:second-mean**2,mgfAtZero:mgf(0)};
  return result;
}

export const checkedResult = {"mean":1.1,"second":1.7,"variance":0.48999999999999977,"mgfAtZero":1};

// Run this file directly: npx tsx lessons/06-probability-and-random-variables/058-variance-moments-and-moment-generating-intuition.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson058(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "mean": 1.1,
  "second": 1.7,
  "variance": 0.48999999999999977,
  "mgfAtZero": 1
}

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

Lesson notes

The rule

Var(X) = E[X²] − E[X]²