Module 7 · Probability Distributions and Simulation Basics Lesson 70 of 120

Random Sampling and Monte Carlo Intuition

Estimating a quantity by simulation and checking the uncertainty.

2:45 clip3:40:41–3:43:26 in the full courseWatch on YouTube

Transcript

19 sentences · select one to jump there

Check your understanding

Does increasing simulation count fix model misspecification?

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.

070-random-sampling-and-monte-carlo-intuition.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 070 of 120
 * Random Sampling and Monte Carlo Intuition
 * Module 07: Probability Distributions and Simulation Basics
 *
 * Scenario: Estimating a quantity by simulation and checking the uncertainty
 * Rule:     Monte Carlo estimate = mean(g(Uᵢ))
 *
 * Try it:   Does increasing simulation count fix model misspecification?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/probability-distributions-and-simulation-basics/random-sampling-and-monte-carlo-intuition/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson070() {
  const draws=[.10,.40,.70,.80]; // frozen synthetic draw fixture
  const outputs=draws.map(u=>u*u);
  const estimate=outputs.reduce((s,x)=>s+x,0)/outputs.length;
  const result={outputs,estimate,exact:1/3,error:estimate-1/3};
  return result;
}

export const checkedResult = {"outputs":[0.010000000000000002,0.16000000000000003,0.48999999999999994,0.6400000000000001],"estimate":0.325,"exact":0.3333333333333333,"error":-0.008333333333333304};

// Run this file directly: npx tsx lessons/07-probability-distributions-and-simulation-basics/070-random-sampling-and-monte-carlo-intuition.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson070(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "outputs": [
    0.010000000000000002,
    0.16000000000000003,
    0.48999999999999994,
    0.6400000000000001
  ],
  "estimate": 0.325,
  "exact": 0.3333333333333333,
  "error": -0.008333333333333304
}

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

Lesson notes

The rule

Monte Carlo estimate = mean(g(Uᵢ))