Module 12 · Statistical Computing and Reproducibility Lesson 119 of 120

Fixtures, Numerical Tolerances, and Property Tests

Testing mathematical behavior, not just one screenshot.

2:43 clip6:22:18–6:25:01 in the full courseWatch on YouTube

Transcript

17 sentences · select one to jump there

Check your understanding

Why test a shifted dataset as well as the original fixture?

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.

119-fixtures-numerical-tolerances-and-property-tests.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 119 of 120
 * Fixtures, Numerical Tolerances, and Property Tests
 * Module 12: Statistical Computing and Reproducibility
 *
 * Scenario: Testing mathematical behavior, not just one screenshot
 * Rule:     |actual−expected| ≤ absTol + relTol·max(|actual|,|expected|)
 *
 * Try it:   Why test a shifted dataset as well as the original fixture?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/statistical-computing-and-reproducibility/fixtures-numerical-tolerances-and-property-tests/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson119() {
  const near=(a:number,b:number)=>Math.abs(a-b)<=1e-12+1e-10*Math.max(Math.abs(a),Math.abs(b));
  const x=[1,2,3,4,5];
  const variance=(a:number[])=>{const m=a.reduce((s,v)=>s+v,0)/a.length;
    return a.reduce((s,v)=>s+(v-m)**2,0)/(a.length-1);};
  const result={decimal:near(.1+.2,.3),fixture:near(variance(x),2.5),
    shift:near(variance(x.map(v=>v+100)),variance(x)),
    scale:near(variance(x.map(v=>2*v)),4*variance(x))};
  return result;
}

export const checkedResult = {"decimal":true,"fixture":true,"shift":true,"scale":true};

// Run this file directly: npx tsx lessons/12-statistical-computing-and-reproducibility/119-fixtures-numerical-tolerances-and-property-tests.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson119(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "decimal": true,
  "fixture": true,
  "shift": true,
  "scale": true
}

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

Lesson notes

The rule

|actual−expected| ≤ absTol + relTol·max(|actual|,|expected|)