Module 3 · Data, Variables, Samples, and Measurement Lesson 28 of 120
Measurement Error, Resolution, Accuracy, and Precision
A clock can be consistent and still wrong.
Transcript
23 sentences · select one to jump thereCheck your understanding
Does a small spread prove accuracy?
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 028 of 120
* Measurement Error, Resolution, Accuracy, and Precision
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A clock can be consistent and still wrong
* Rule: error = measured − reference
*
* Try it: Does a small spread prove accuracy?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson028() {
const reference = 100;
const measured = [105, 105, 106, 104];
const errors = measured.map(x => x - reference);
const bias = errors.reduce((s,x) => s+x,0) / errors.length;
const result = {errors, bias};
return result;
}
export const checkedResult = {"errors":[5,5,6,4],"bias":5};
// Run this file directly: npx tsx lessons/03-data-variables-samples-and-measurement/028-measurement-error-resolution-accuracy-and-precision.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson028(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}"""
Fintech Math Bootcamp · Lesson 028 of 120
Measurement Error, Resolution, Accuracy, and Precision
Module 03: Data, Variables, Samples, and Measurement
Scenario: A clock can be consistent and still wrong
Rule: error = measured − reference
Try it: Does a small spread prove accuracy?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson028() -> dict:
reference = 100
measured = [105, 105, 106, 104]
errors = [x - reference for x in measured]
bias = sum(errors) / len(errors)
result = {"errors": errors, "bias": bias}
return result
if __name__ == "__main__":
print(json.dumps(lesson028(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}/**
* Fintech Math Bootcamp · Lesson 028 of 120
* Measurement Error, Resolution, Accuracy, and Precision
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A clock can be consistent and still wrong
* Rule: error = measured − reference
*
* Try it: Does a small spread prove accuracy?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
import java.util.Arrays;
public class Main {
static final class MeasurementBias {
final double[] errors;
final double bias;
MeasurementBias(double[] errors, double bias) {
this.errors = errors;
this.bias = bias;
}
}
static MeasurementBias lesson028() {
double reference = 100;
double[] measured = {105, 105, 106, 104};
double[] errors = Arrays.stream(measured).map(x -> x - reference).toArray();
double bias = Arrays.stream(errors).reduce(0, (s, x) -> s + x) / errors.length;
MeasurementBias result = new MeasurementBias(errors, bias);
return result;
}
public static void main(String[] args) {
MeasurementBias result = lesson028();
System.out.println(object("errors", numbers(result.errors), "bias", num(result.bias)));
}
// Formats a double the way JSON.stringify does: whole numbers without ".0", null for NaN or infinity.
static String num(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) return "null";
if (x == Math.rint(x) && Math.abs(x) < 1e15) return String.valueOf((long) x);
return String.valueOf(x);
}
static String quote(String text) {
return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
// Indents an already formatted JSON value by one level.
static String nested(String json) {
return json.replace("\n", "\n ");
}
static String object(String... keysAndValues) {
if (keysAndValues.length == 0) return "{}";
StringBuilder out = new StringBuilder("{");
for (int i = 0; i < keysAndValues.length; i += 2) {
out.append(i == 0 ? "\n " : ",\n ")
.append(quote(keysAndValues[i])).append(": ").append(nested(keysAndValues[i + 1]));
}
return out.append("\n}").toString();
}
static String array(java.util.List<String> items) {
if (items.isEmpty()) return "[]";
StringBuilder out = new StringBuilder("[");
for (int i = 0; i < items.size(); i++) {
out.append(i == 0 ? "\n " : ",\n ").append(nested(items.get(i)));
}
return out.append("\n]").toString();
}
static String numbers(double... values) {
java.util.List<String> items = new java.util.ArrayList<String>();
for (double value : values) items.add(num(value));
return array(items);
}
}
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
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}// Fintech Math Bootcamp · Lesson 028 of 120
// Measurement Error, Resolution, Accuracy, and Precision
// Module 03: Data, Variables, Samples, and Measurement
//
// Scenario: A clock can be consistent and still wrong
// Rule: error = measured − reference
//
// Try it: Does a small spread prove accuracy?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
//
// Run it: go run main.go
package main
import (
"encoding/json"
"fmt"
)
type MeasurementBias struct {
Errors []float64 `json:"errors"`
Bias float64 `json:"bias"`
}
func lesson028() MeasurementBias {
reference := 100.0
measured := []float64{105, 105, 106, 104}
errors := make([]float64, len(measured))
for i, x := range measured {
errors[i] = x - reference
}
sum := 0.0
for _, x := range errors {
sum += x
}
bias := sum / float64(len(errors))
result := MeasurementBias{Errors: errors, Bias: bias}
return result
}
func main() {
printJSON(lesson028())
}
// printJSON prints a value as JSON indented with two spaces, like JSON.stringify(value, null, 2).
func printJSON(value any) {
out, err := json.MarshalIndent(value, "", " ")
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
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}/**
* Fintech Math Bootcamp · Lesson 028 of 120
* Measurement Error, Resolution, Accuracy, and Precision
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A clock can be consistent and still wrong
* Rule: error = measured − reference
*
* Try it: Does a small spread prove accuracy?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: g++ -std=c++17 -o main main.cpp && ./main
*/
#include <charconv>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
// Formats a double the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
std::string num(double x) {
if (!std::isfinite(x)) return "null";
char buffer[32];
auto [end, error] = std::to_chars(buffer, buffer + sizeof buffer, x);
(void)error;
return std::string(buffer, end);
}
std::string quote(const std::string& text) {
std::string out = "\"";
for (char c : text) {
if (c == '"' || c == '\\') out += '\\';
out += c;
}
return out + "\"";
}
// Indents an already formatted JSON value by one level.
std::string nested(const std::string& json) {
std::string out;
for (char c : json) {
out += c;
if (c == '\n') out += " ";
}
return out;
}
std::string object(const std::vector<std::pair<std::string, std::string>>& fields) {
if (fields.empty()) return "{}";
std::string out = "{";
for (std::size_t i = 0; i < fields.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += quote(fields[i].first) + ": " + nested(fields[i].second);
}
return out + "\n}";
}
std::string array(const std::vector<std::string>& items) {
if (items.empty()) return "[]";
std::string out = "[";
for (std::size_t i = 0; i < items.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += nested(items[i]);
}
return out + "\n]";
}
std::string numbers(const std::vector<double>& values) {
std::vector<std::string> items;
for (double value : values) items.push_back(num(value));
return array(items);
}
struct MeasurementBias {
std::vector<double> errors;
double bias;
};
MeasurementBias lesson028() {
const double reference = 100;
const std::vector<double> measured{105, 105, 106, 104};
std::vector<double> errors;
for (double x : measured) errors.push_back(x - reference);
const double bias = std::accumulate(errors.begin(), errors.end(), 0.0) / errors.size();
const MeasurementBias result{errors, bias};
return result;
}
int main() {
const MeasurementBias result = lesson028();
std::cout << object({{"errors", numbers(result.errors)}, {"bias", num(result.bias)}}) << "\n";
}
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
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}//! Fintech Math Bootcamp · Lesson 028 of 120
//! Measurement Error, Resolution, Accuracy, and Precision
//! Module 03: Data, Variables, Samples, and Measurement
//!
//! Scenario: A clock can be consistent and still wrong
//! Rule: error = measured − reference
//!
//! Try it: Does a small spread prove accuracy?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
struct MeasurementBias {
errors: Vec<f64>,
bias: f64,
}
fn lesson028() -> MeasurementBias {
let reference = 100.0;
let measured = [105.0, 105.0, 106.0, 104.0];
let errors: Vec<f64> = measured.iter().map(|&x| x - reference).collect();
let bias = errors.iter().fold(0.0, |s, &x| s + x) / errors.len() as f64;
let result = MeasurementBias { errors, bias };
result
}
fn main() {
let result = lesson028();
println!(
"{}",
object(&[("errors", numbers(&result.errors)), ("bias", num(result.bias))])
);
}
/// Formats a number the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
fn num(x: f64) -> String {
if x.is_finite() {
format!("{}", x)
} else {
"null".to_string()
}
}
fn quote(text: &str) -> String {
format!("\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""))
}
/// Indents an already formatted JSON value by one level.
fn nested(json: &str) -> String {
json.replace('\n', "\n ")
}
fn object(fields: &[(&str, String)]) -> String {
if fields.is_empty() {
return "{}".to_string();
}
let lines: Vec<String> = fields
.iter()
.map(|(key, value)| format!(" {}: {}", quote(key), nested(value)))
.collect();
format!("{{\n{}\n}}", lines.join(",\n"))
}
fn array(items: &[String]) -> String {
if items.is_empty() {
return "[]".to_string();
}
let lines: Vec<String> = items.iter().map(|item| format!(" {}", nested(item))).collect();
format!("[\n{}\n]", lines.join(",\n"))
}
fn numbers(values: &[f64]) -> String {
let items: Vec<String> = values.iter().map(|&value| num(value)).collect();
array(&items)
}
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
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}/**
* Fintech Math Bootcamp · Lesson 028 of 120
* Measurement Error, Resolution, Accuracy, and Precision
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: A clock can be consistent and still wrong
* Rule: error = measured − reference
*
* Try it: Does a small spread prove accuracy?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/measurement-error-resolution-accuracy-and-precision/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: dotnet run (inside a console project that holds this Program.cs)
*/
using System.Text.Json;
var jsonOptions = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Console.WriteLine(JsonSerializer.Serialize(Lesson028(), jsonOptions));
static MeasurementBias Lesson028()
{
const double reference = 100;
double[] measured = { 105, 105, 106, 104 };
var errors = measured.Select(x => x - reference).ToArray();
var bias = errors.Aggregate(0.0, (s, x) => s + x) / errors.Length;
var result = new MeasurementBias(errors, bias);
return result;
}
record MeasurementBias(double[] Errors, double Bias);
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
{
"errors": [
5,
5,
6,
4
],
"bias": 5
}Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
error = measured − reference