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

Beta and Market-Relative Risk

Market sensitivity is not total portfolio risk.

2:33 clip5:43:35–5:46:08 in the full courseWatch on YouTube

Transcript

18 sentences · select one to jump there

Check your understanding

Does beta near zero imply no risk?

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.

107-beta-and-market-relative-risk.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 107 of 120
 * Beta and Market-Relative Risk
 * Module 11: Financial Risk and Performance Statistics
 *
 * Scenario: Market sensitivity is not total portfolio risk
 * Rule:     beta = Cov(asset,market)/Var(market)
 *
 * Try it:   Does beta near zero imply no risk?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-risk-and-performance-statistics/beta-and-market-relative-risk/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson107() {
  const market=[-.02,-.01,0,.01,.02];
  const asset=market.map(r=>1.5*r);
  const avg=(a:number[])=>a.reduce((s,x)=>s+x,0)/a.length;
  const ma=avg(asset),mm=avg(market);
  const cov=asset.reduce((s,r,i)=>s+(r-ma)*(market[i]-mm),0)/(asset.length-1);
  const varM=market.reduce((s,r)=>s+(r-mm)**2,0)/(market.length-1);
  if(varM<=0) throw new Error("Market variance must be positive");
  const result={beta:cov/varM,asset};
  return result;
}

export const checkedResult = {"beta":1.4999999999999998,"asset":[-0.03,-0.015,0,0.015,0.03]};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "beta": 1.4999999999999998,
  "asset": [
    -0.03,
    -0.015,
    0,
    0.015,
    0.03
  ]
}

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

Lesson notes

The rule

beta = Cov(asset,market)/Var(market)