Module 12 · Statistical Computing and Reproducibility Lesson 119 of 120
Fixtures, Numerical Tolerances, and Property Tests
Testing mathematical behavior, not just one screenshot.
Transcript
17 sentences · select one to jump thereCheck your understanding
Why test a shifted dataset as well as the original fixture?
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.
/**
* 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
}# 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.
#
# Run: python main.py
import json
def lesson_119():
def near(a, b):
return abs(a - b) <= 1e-12 + 1e-10 * max(abs(a), abs(b))
x = [1, 2, 3, 4, 5]
def variance(a):
total = 0.0
for v in a:
total += v
m = total / len(a)
squares = 0.0
for v in a:
squares += (v - m) ** 2
return squares / (len(a) - 1)
return {
"decimal": near(0.1 + 0.2, 0.3),
"fixture": near(variance(x), 2.5),
"shift": near(variance([v + 100 for v in x]), variance(x)),
"scale": near(variance([2 * v for v in x]), 4 * variance(x)),
}
if __name__ == "__main__":
print(json.dumps(lesson_119(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"decimal": true,
"fixture": true,
"shift": true,
"scale": true
}/*
* 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.
*
* Run: javac Main.java && java Main
*/
import java.util.*;
public class Main {
static boolean near(double a, double b) {
return Math.abs(a - b) <= 1e-12 + 1e-10 * Math.max(Math.abs(a), Math.abs(b));
}
static double variance(double[] a) {
double m = 0;
for (double v : a) m += v;
m /= a.length;
double s = 0;
for (double v : a) s += Math.pow(v - m, 2);
return s / (a.length - 1);
}
static Map<String, Object> lesson119() {
double[] x = {1, 2, 3, 4, 5};
double[] shifted = new double[x.length];
double[] scaled = new double[x.length];
for (int i = 0; i < x.length; i++) {
shifted[i] = x[i] + 100;
scaled[i] = 2 * x[i];
}
return obj("decimal", near(.1 + .2, .3), "fixture", near(variance(x), 2.5),
"shift", near(variance(shifted), variance(x)), "scale", near(variance(scaled), 4 * variance(x)));
}
public static void main(String[] args) {
System.out.println(toJson(lesson119(), ""));
}
// Minimal JSON writer: insertion-ordered Map, List, Number, Boolean, String and null.
static String toJson(Object value, String indent) {
if (value == null) return "null";
if (value instanceof String) return quote((String) value);
if (value instanceof Boolean) return value.toString();
if (value instanceof Double) return formatNumber((Double) value);
if (value instanceof Number) return value.toString();
String inner = indent + " ";
StringBuilder sb = new StringBuilder();
if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
if (map.isEmpty()) return "{}";
sb.append("{\n");
int i = 0;
for (Map.Entry<?, ?> e : map.entrySet()) {
sb.append(inner).append(quote(e.getKey().toString())).append(": ").append(toJson(e.getValue(), inner));
sb.append(++i < map.size() ? ",\n" : "\n");
}
return sb.append(indent).append("}").toString();
}
List<?> list = (List<?>) value;
if (list.isEmpty()) return "[]";
sb.append("[\n");
for (int i = 0; i < list.size(); i++) {
sb.append(inner).append(toJson(list.get(i), inner)).append(i + 1 < list.size() ? ",\n" : "\n");
}
return sb.append(indent).append("]").toString();
}
// Integral doubles print without ".0", as JavaScript does; others use Java's round-trip form.
static String formatNumber(double x) {
if (x == Math.rint(x) && Math.abs(x) < 1e15) return Long.toString((long) x);
return Double.toString(x);
}
static String quote(String s) {
StringBuilder sb = new StringBuilder("\"");
for (char c : s.toCharArray()) {
if (c == '"' || c == '\\') sb.append('\\').append(c);
else if (c < 0x20) sb.append(String.format("\\u%04x", (int) c));
else sb.append(c);
}
return sb.append('"').toString();
}
// Builds an insertion-ordered object from alternating keys and values.
static Map<String, Object> obj(Object... keysAndValues) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (int i = 0; i < keysAndValues.length; i += 2) map.put((String) keysAndValues[i], keysAndValues[i + 1]);
return map;
}
static List<Object> list(double... values) {
List<Object> out = new ArrayList<Object>();
for (double v : values) out.add(v);
return out;
}
}
No browser runner for Java yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"decimal": true,
"fixture": true,
"shift": true,
"scale": true
}// 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.
//
// Run: go run main.go
package main
import (
"encoding/json"
"fmt"
"math"
)
type Result struct {
Decimal bool `json:"decimal"`
Fixture bool `json:"fixture"`
Shift bool `json:"shift"`
Scale bool `json:"scale"`
}
func near(a, b float64) bool {
return math.Abs(a-b) <= 1e-12+1e-10*math.Max(math.Abs(a), math.Abs(b))
}
func variance(a []float64) float64 {
m := 0.0
for _, v := range a {
m += v
}
m /= float64(len(a))
s := 0.0
for _, v := range a {
s += math.Pow(v-m, 2)
}
return s / float64(len(a)-1)
}
func mapValues(a []float64, f func(float64) float64) []float64 {
out := make([]float64, len(a))
for i, v := range a {
out[i] = f(v)
}
return out
}
func lesson119() Result {
x := []float64{1, 2, 3, 4, 5}
// Variables, not constants: a constant 0.1 + 0.2 would be evaluated exactly in Go.
a, b, c := 0.1, 0.2, 0.3
return Result{
Decimal: near(a+b, c),
Fixture: near(variance(x), 2.5),
Shift: near(variance(mapValues(x, func(v float64) float64 { return v + 100 })), variance(x)),
Scale: near(variance(mapValues(x, func(v float64) float64 { return 2 * v })), 4*variance(x)),
}
}
func main() {
out, err := json.MarshalIndent(lesson119(), "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
No browser runner for Go yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"decimal": true,
"fixture": true,
"shift": true,
"scale": true
}// 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.
//
// Run: g++ -std=c++17 -O1 -o lesson main.cpp && ./lesson
#include <algorithm>
#include <charconv>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <variant>
#include <vector>
// Minimal JSON value: objects keep insertion order; numbers print in shortest round-trip form.
struct Json {
enum class Kind { Null, Bool, Number, String, Array, Object };
Kind kind = Kind::Null;
bool flag = false;
double number = 0.0;
std::string text;
std::vector<std::string> keys; // object keys, parallel to items
std::vector<Json> items; // array items, or object values
};
Json jnull() { return Json{}; }
Json jbool(bool value) { Json j; j.kind = Json::Kind::Bool; j.flag = value; return j; }
Json jnum(double value) { Json j; j.kind = Json::Kind::Number; j.number = value; return j; }
Json jstr(const std::string& value) { Json j; j.kind = Json::Kind::String; j.text = value; return j; }
Json jarr(std::vector<Json> items) { Json j; j.kind = Json::Kind::Array; j.items = std::move(items); return j; }
Json jobj(std::vector<std::pair<std::string, Json>> fields) {
Json j;
j.kind = Json::Kind::Object;
for (auto& field : fields) {
j.keys.push_back(field.first);
j.items.push_back(std::move(field.second));
}
return j;
}
Json jnums(const std::vector<double>& values) {
std::vector<Json> items;
for (double v : values) items.push_back(jnum(v));
return jarr(std::move(items));
}
Json jnums(const std::vector<std::optional<double>>& values) {
std::vector<Json> items;
for (const auto& v : values) items.push_back(v ? jnum(*v) : jnull());
return jarr(std::move(items));
}
std::string formatNumber(double value) {
char buffer[64];
auto result = std::to_chars(buffer, buffer + sizeof buffer, value); // shortest round-trip form
return std::string(buffer, result.ptr);
}
std::string quote(const std::string& text) {
std::string out = "\"";
for (char c : text) {
if (c == '"' || c == '\\') {
out += '\\';
out += c;
} else if (static_cast<unsigned char>(c) < 0x20) {
char buffer[8];
std::snprintf(buffer, sizeof buffer, "\\u%04x", static_cast<unsigned>(c));
out += buffer;
} else {
out += c;
}
}
return out + "\"";
}
void writeJson(std::ostream& out, const Json& value, const std::string& indent = "") {
const std::string inner = indent + " ";
switch (value.kind) {
case Json::Kind::Null: out << "null"; return;
case Json::Kind::Bool: out << (value.flag ? "true" : "false"); return;
case Json::Kind::Number: out << formatNumber(value.number); return;
case Json::Kind::String: out << quote(value.text); return;
case Json::Kind::Array:
case Json::Kind::Object: {
const bool isObject = value.kind == Json::Kind::Object;
if (value.items.empty()) {
out << (isObject ? "{}" : "[]");
return;
}
out << (isObject ? "{\n" : "[\n");
for (std::size_t i = 0; i < value.items.size(); ++i) {
out << inner;
if (isObject) out << quote(value.keys[i]) << ": ";
writeJson(out, value.items[i], inner);
out << (i + 1 < value.items.size() ? ",\n" : "\n");
}
out << indent << (isObject ? "}" : "]");
}
}
}
bool near(double a, double b) {
return std::abs(a - b) <= 1e-12 + 1e-10 * std::max(std::abs(a), std::abs(b));
}
double variance(const std::vector<double>& a) {
double m = 0;
for (double v : a) m += v;
m /= static_cast<double>(a.size());
double s = 0;
for (double v : a) s += std::pow(v - m, 2);
return s / static_cast<double>(a.size() - 1);
}
Json lesson119() {
const std::vector<double> x{1, 2, 3, 4, 5};
std::vector<double> shifted, scaled;
for (double v : x) {
shifted.push_back(v + 100);
scaled.push_back(2 * v);
}
return jobj({{"decimal", jbool(near(.1 + .2, .3))},
{"fixture", jbool(near(variance(x), 2.5))},
{"shift", jbool(near(variance(shifted), variance(x)))},
{"scale", jbool(near(variance(scaled), 4 * variance(x)))}});
}
int main() {
writeJson(std::cout, lesson119());
std::cout << "\n";
return 0;
}
No browser runner for C++ yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"decimal": true,
"fixture": true,
"shift": true,
"scale": true
}// 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.
//
// Run: rustc -O main.rs && ./main
#![allow(dead_code)]
/// Minimal JSON value; objects keep insertion order.
enum Json {
Null,
Bool(bool),
Num(f64),
Str(String),
Arr(Vec<Json>),
Obj(Vec<(String, Json)>),
}
fn obj(fields: Vec<(&str, Json)>) -> Json {
Json::Obj(fields.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
}
fn nums(values: &[f64]) -> Json {
Json::Arr(values.iter().map(|&v| Json::Num(v)).collect())
}
fn optional_nums(values: &[Option<f64>]) -> Json {
Json::Arr(values.iter().map(|v| v.map_or(Json::Null, Json::Num)).collect())
}
fn quote(text: &str) -> String {
let mut out = String::from("\"");
for c in text.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
c => out.push(c),
}
}
out.push('"');
out
}
// Display for f64 prints the shortest string that round-trips, as JavaScript does.
fn write_json(value: &Json, indent: &str, out: &mut String) {
let inner = format!("{} ", indent);
match value {
Json::Null => out.push_str("null"),
Json::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
Json::Num(n) => out.push_str(&format!("{}", n)),
Json::Str(s) => out.push_str("e(s)),
Json::Arr(items) if items.is_empty() => out.push_str("[]"),
Json::Obj(fields) if fields.is_empty() => out.push_str("{}"),
Json::Arr(items) => {
out.push_str("[\n");
for (i, item) in items.iter().enumerate() {
out.push_str(&inner);
write_json(item, &inner, out);
out.push_str(if i + 1 < items.len() { ",\n" } else { "\n" });
}
out.push_str(indent);
out.push(']');
}
Json::Obj(fields) => {
out.push_str("{\n");
for (i, (key, item)) in fields.iter().enumerate() {
out.push_str(&inner);
out.push_str("e(key));
out.push_str(": ");
write_json(item, &inner, out);
out.push_str(if i + 1 < fields.len() { ",\n" } else { "\n" });
}
out.push_str(indent);
out.push('}');
}
}
}
fn near(a: f64, b: f64) -> bool {
(a - b).abs() <= 1e-12 + 1e-10 * a.abs().max(b.abs())
}
fn variance(a: &[f64]) -> f64 {
let n = a.len() as f64;
let m = a.iter().fold(0.0, |s, v| s + v) / n;
a.iter().fold(0.0, |s, v| s + (v - m).powi(2)) / (n - 1.0)
}
fn lesson_119() -> Json {
let x: [f64; 5] = [1.0, 2.0, 3.0, 4.0, 5.0];
let shifted: Vec<f64> = x.iter().map(|v| v + 100.0).collect();
let scaled: Vec<f64> = x.iter().map(|v| 2.0 * v).collect();
obj(vec![
("decimal", Json::Bool(near(0.1 + 0.2, 0.3))),
("fixture", Json::Bool(near(variance(&x), 2.5))),
("shift", Json::Bool(near(variance(&shifted), variance(&x)))),
("scale", Json::Bool(near(variance(&scaled), 4.0 * variance(&x)))),
])
}
fn main() {
let mut out = String::new();
write_json(&lesson_119(), "", &mut out);
println!("{}", out);
}
No browser runner for Rust yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
{
"decimal": true,
"fixture": true,
"shift": true,
"scale": true
}// 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.
//
// Run: dotnet run
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
var options = new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
Console.WriteLine(JsonSerializer.Serialize(Lesson119(), options));
static object Lesson119()
{
static bool Near(double a, double b) => Math.Abs(a - b) <= 1e-12 + 1e-10 * Math.Max(Math.Abs(a), Math.Abs(b));
static double Variance(double[] a)
{
double m = a.Aggregate(0.0, (s, v) => s + v) / a.Length;
return a.Aggregate(0.0, (s, v) => s + Math.Pow(v - m, 2)) / (a.Length - 1);
}
double[] x = { 1, 2, 3, 4, 5 };
return new
{
@decimal = Near(.1 + .2, .3),
fixture = Near(Variance(x), 2.5),
shift = Near(Variance(x.Select(v => v + 100).ToArray()), Variance(x)),
scale = Near(Variance(x.Select(v => 2 * v).ToArray()), 4 * Variance(x)),
};
}
No browser runner for C# yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
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|)