Module 9 · Dependence, Regression, and Model Foundations Lesson 81 of 120

Scatter Plots, Association, and Nonlinear Patterns

Looking at paired observations before compressing them.

2:34 clip4:23:06–4:25:41 in the full courseWatch on YouTube

Transcript

19 sentences · select one to jump there

Check your understanding

Can zero sample covariance hide an obvious pattern?

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.

081-scatter-plots-association-and-nonlinear-patterns.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 081 of 120
 * Scatter Plots, Association, and Nonlinear Patterns
 * Module 09: Dependence, Regression, and Model Foundations
 *
 * Scenario: Looking at paired observations before compressing them
 * Rule:     a scatter plot preserves paired coordinates (xᵢ,yᵢ)
 *
 * Try it:   Can zero sample covariance hide an obvious pattern?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/dependence-regression-and-model-foundations/scatter-plots-association-and-nonlinear-patterns/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson081() {
  const x=[-2,-1,0,1,2];
  const y=x.map(v=>v*v);
  const mean=(a:number[])=>a.reduce((s,v)=>s+v,0)/a.length;
  const mx=mean(x),my=mean(y);
  const covariance=x.reduce((s,v,i)=>s+(v-mx)*(y[i]-my),0)/(x.length-1);
  const result={points:x.map((v,i)=>[v,y[i]]),covariance};
  return result;
}

export const checkedResult = {"points":[[-2,4],[-1,1],[0,0],[1,1],[2,4]],"covariance":0};

// Run this file directly: npx tsx lessons/09-dependence-regression-and-model-foundations/081-scatter-plots-association-and-nonlinear-patterns.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson081(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "points": [
    [
      -2,
      4
    ],
    [
      -1,
      1
    ],
    [
      0,
      0
    ],
    [
      1,
      1
    ],
    [
      2,
      4
    ]
  ],
  "covariance": 0
}

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

Lesson notes

The rule

a scatter plot preserves paired coordinates (xᵢ,yᵢ)