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

Bernoulli and Binomial Distributions

Counting failures across a fixed number of comparable attempts.

2:37 clip3:19:38–3:22:15 in the full courseWatch on YouTube

Transcript

19 sentences · select one to jump there

Check your understanding

Why is exactly two failures multiplied by three?

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.

062-bernoulli-and-binomial-distributions.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 062 of 120
 * Bernoulli and Binomial Distributions
 * Module 07: Probability Distributions and Simulation Basics
 *
 * Scenario: Counting failures across a fixed number of comparable attempts
 * Rule:     K ~ Binomial(n,p); E[K]=np; Var(K)=np(1−p)
 *
 * Try it:   Why is exactly two failures multiplied by three?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/probability-distributions-and-simulation-basics/bernoulli-and-binomial-distributions/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson062() {
  const n=3,p=.2,k=2;
  const choose=(n:number,k:number)=>{let c=1;for(let i=1;i<=k;i++)c=c*(n-k+i)/i;return c;};
  const exactlyTwo=choose(n,k)*p**k*(1-p)**(n-k);
  const result={exactlyTwo,atLeastOne:1-(1-p)**n,
    mean:n*p,variance:n*p*(1-p)};
  return result;
}

export const checkedResult = {"exactlyTwo":0.09600000000000003,"atLeastOne":0.4879999999999999,"mean":0.6000000000000001,"variance":0.4800000000000001};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "exactlyTwo": 0.09600000000000003,
  "atLeastOne": 0.4879999999999999,
  "mean": 0.6000000000000001,
  "variance": 0.4800000000000001
}

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

Lesson notes

The rule

K ~ Binomial(n,p); E[K]=np; Var(K)=np(1−p)