FA-326 / Floating-point arithmetic / Open access
Subtracting two large moments destroys a small variance · case 01
A nonconstant sample reports zero or an inaccurate variance after its mean is shifted far from zero.
ROOT CAUSE
E[x²] and E[x]² are rounded separately before subtracting two nearly equal large values.
VERIFIED REPAIR
Use the standard-library population-variance computation, which avoids cancellation in the raw-moment subtraction.
Unsuccessful approach: Clamping negative results to zero prevents an invalid sign but cannot restore precision already lost to cancellation.
Case contract
Return statistics.pvariance for a nonempty sequence of finite floats; return None for empty input. This is population variance, so the denominator is n rather than n-1.
Why this case matters
Variance should not change when all observations receive the same offset. Large baselines with small spreads are a direct regression check for cancellation-prone moment formulas.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import statistics
N = 1
observations = []
def solve(values):
if not values:
return None
mean = sum(values) / len(values)
return sum(value * value for value in values) / len(values) - mean * mean
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
base = float(10 ** (N + 6))
check('unit spread on large baseline', solve([base, base + 1, base + 2]), 2 / 3)
check('same spread around zero', solve([0.0, 1.0, 2.0]), 2 / 3)
check('symmetric wider spread on baseline', solve([base - 2, base, base + 2]), 8 / 3)
check('constant sample has zero variance', solve([base] * (N + 1)), 0.0)
check('population denominator for two values', solve([-float(N), float(N)]), float(N * N))
check('single value', solve([base]), 0.0)
check('empty sample', solve([]), None)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| unit spread on large baseline | 0.671875 | 0.6666666666666666 | Failed |
| same spread around zero | 0.6666666666666667 | 0.6666666666666666 | Failed |
| symmetric wider spread on baseline | 2.671875 | 2.6666666666666665 | Failed |
| constant sample has zero variance | 0.0 | 0.0 | Passed |
| population denominator for two values | 1.0 | 1.0 | Passed |
| single value | 0.0 | 0.0 | Passed |
| empty sample | None | None | Passed |
SHA-256 / 2e908f94ab6696ac4eab0138b610046288e97dd9858c8af81eeff44a7c45f183
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import statistics
N = 1
observations = []
def solve(values):
if not values:
return None
mean = sum(values) / len(values)
variance = sum(value * value for value in values) / len(values) - mean * mean
return max(0.0, variance)
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
base = float(10 ** (N + 6))
check('unit spread on large baseline', solve([base, base + 1, base + 2]), 2 / 3)
check('same spread around zero', solve([0.0, 1.0, 2.0]), 2 / 3)
check('symmetric wider spread on baseline', solve([base - 2, base, base + 2]), 8 / 3)
check('constant sample has zero variance', solve([base] * (N + 1)), 0.0)
check('population denominator for two values', solve([-float(N), float(N)]), float(N * N))
check('single value', solve([base]), 0.0)
check('empty sample', solve([]), None)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| unit spread on large baseline | 0.671875 | 0.6666666666666666 | Failed |
| same spread around zero | 0.6666666666666667 | 0.6666666666666666 | Failed |
| symmetric wider spread on baseline | 2.671875 | 2.6666666666666665 | Failed |
| constant sample has zero variance | 0.0 | 0.0 | Passed |
| population denominator for two values | 1.0 | 1.0 | Passed |
| single value | 0.0 | 0.0 | Passed |
| empty sample | None | None | Passed |
SHA-256 / f3b26229d2adef184b211d022edf05429f17436fa1bff861e2a4f47fa4a8d9c7
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
import statistics
N = 1
observations = []
def solve(values):
return statistics.pvariance(values) if values else None
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
base = float(10 ** (N + 6))
check('unit spread on large baseline', solve([base, base + 1, base + 2]), 2 / 3)
check('same spread around zero', solve([0.0, 1.0, 2.0]), 2 / 3)
check('symmetric wider spread on baseline', solve([base - 2, base, base + 2]), 8 / 3)
check('constant sample has zero variance', solve([base] * (N + 1)), 0.0)
check('population denominator for two values', solve([-float(N), float(N)]), float(N * N))
check('single value', solve([base]), 0.0)
check('empty sample', solve([]), None)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| unit spread on large baseline | 0.6666666666666666 | 0.6666666666666666 | Passed |
| same spread around zero | 0.6666666666666666 | 0.6666666666666666 | Passed |
| symmetric wider spread on baseline | 2.6666666666666665 | 2.6666666666666665 | Passed |
| constant sample has zero variance | 0.0 | 0.0 | Passed |
| population denominator for two values | 1.0 | 1.0 | Passed |
| single value | 0.0 | 0.0 | Passed |
| empty sample | None | None | Passed |
SHA-256 / 91f9989eaaf3204df309fb82ea1713907cfb1b4c8cd39e448da3c9c448820f32
Verification & scope
This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:36:52.102142+00:00.
Case digest / 088b511816c89ddcc3a6cc30273b2f24325e42cf9201a9f5ceaf6fafe687b9d6