Module 6 · Probability and Random Variables Lesson 60 of 120

Covariance and Correlation of Random Variables

Dependence in shared credit or insurance losses.

2:38 clip3:07:54–3:10:32 in the full courseWatch on YouTube

Transcript

21 sentences · select one to jump there

Check your understanding

Are X and Y independent when Y=X² and covariance is zero?

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.

060-covariance-and-correlation-of-random-variables.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 060 of 120
 * Covariance and Correlation of Random Variables
 * Module 06: Probability and Random Variables
 *
 * Scenario: Dependence in shared credit or insurance losses
 * Rule:     Cov(X,Y)=E[(X−EX)(Y−EY)]
 *
 * Try it:   Are X and Y independent when Y=X² and covariance is zero?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/probability-and-random-variables/covariance-and-correlation-of-random-variables/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson060() {
  const states=[{x:-1,y:1,p:.25},{x:0,y:0,p:.5},{x:1,y:1,p:.25}];
  const ex=states.reduce((s,a)=>s+a.p*a.x,0);
  const ey=states.reduce((s,a)=>s+a.p*a.y,0);
  const covariance=states.reduce((s,a)=>s+a.p*(a.x-ex)*(a.y-ey),0);
  const result={ex,ey,covariance};
  return result;
}

export const checkedResult = {"ex":0,"ey":0.5,"covariance":0};

// Run this file directly: npx tsx lessons/06-probability-and-random-variables/060-covariance-and-correlation-of-random-variables.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson060(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "ex": 0,
  "ey": 0.5,
  "covariance": 0
}

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

Lesson notes

The rule

Cov(X,Y)=E[(X−EX)(Y−EY)]