Module 11 · Financial Risk and Performance Statistics Lesson 110 of 120

Covariance Matrices, Portfolio Variance, and Diversification

Why two individually quiet holdings can become a risky combination.

2:53 clip5:51:32–5:54:25 in the full courseWatch on YouTube

Transcript

19 sentences · select one to jump there

Check your understanding

What changed between the two portfolio calculations?

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.

110-covariance-matrices-portfolio-variance-and-diversification.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 110 of 120
 * Covariance Matrices, Portfolio Variance, and Diversification
 * Module 11: Financial Risk and Performance Statistics
 *
 * Scenario: Why two individually quiet holdings can become a risky combination
 * Rule:     portfolio variance = wᵀΣw
 *
 * Try it:   What changed between the two portfolio calculations?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-risk-and-performance-statistics/covariance-matrices-portfolio-variance-and-diversification/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson110() {
  const w=[.5,.5],vol=[.01,.01];
  const variance=(rho:number)=>{ // w' Σ w
    const cov=[[vol[0]**2,rho*vol[0]*vol[1]],[rho*vol[0]*vol[1],vol[1]**2]];
    return w.reduce((s,wi,i)=>s+wi*w.reduce((t,wj,j)=>t+cov[i][j]*wj,0),0);};
  const result={atMinusHalf:variance(-.5),atPointNine:variance(.9),
    dollarSDLow:100000*Math.sqrt(variance(-.5)),
    dollarSDHigh:100000*Math.sqrt(variance(.9))};
  return result;
}

export const checkedResult = {"atMinusHalf":0.000025,"atPointNine":0.000095,"dollarSDLow":500,"dollarSDHigh":974.6794344808964};

// Run this file directly: npx tsx lessons/11-financial-risk-and-performance-statistics/110-covariance-matrices-portfolio-variance-and-diversification.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson110(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "atMinusHalf": 0.000025,
  "atPointNine": 0.000095,
  "dollarSDLow": 500,
  "dollarSDHigh": 974.6794344808964
}

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

Lesson notes

The rule

portfolio variance = wᵀΣw