Conversation
Add StdDev operator sections to the cache and list instruction references, covering the sample-deviation formula, fallback behavior, and per-change-reason handling.
|
P2: Integer standard deviation loses small variances when the moments are converted to double. Locations: Casting Reproduced with the explicit integer overload, Suggested fix: calculate variance using a numerically stable centered representation, such as a running mean and centered sum of squares with matching add/remove updates, rather than subtracting two large floating-point moments. This requires coordinated accumulator changes, not a replacement of the final cast alone. Add translation-invariance coverage for both |
Fixes #1181
Problem
Every
StdDevoverload computed the result with a misplaced divisor. The corrected sum of squares was divided byCountinsideMath.Sqrt, and the1 / (Count - 1)sample factor was then applied outside the square root:That is not the sample standard deviation, nor the population standard deviation. It is the square root of the total corrected sum of squares scaled by
1 / (n - 1), so results were wrong for every input of size 2 or more.The
intandlongoverloads had a second defect:(SumOfItems * SumOfItems) / Countwas evaluated in integer arithmetic, truncating the mean correction before it reached the floating-point path.Fix
The divisor now lives inside the square root, and the integer overloads widen to
doublebefore dividing:The
decimaloverload gets the same restructuring using itsSqrthelper.This changes the numbers returned by
StdDevfor all callers. Any consumer comparing against previously observed output, or with recorded baselines/snapshots, will see different values. The new values are the correct sample standard deviation (Bessel-corrected,n - 1denominator); the old values were not a standard deviation under any convention, so the change is a correction rather than a redefinition. The public signatures are unchanged.Fallback behavior is unchanged: when fewer than two items remain, the configured
fallbackValueis returned.Coverage
StdDevpreviously had no test coverage at all. This addsStdDevFixturewith tests over all five numeric selector types (int,long,float,double,decimal), validating computed values against independently calculated expectations, plus incremental add/remove/update behavior and the fewer-than-two-items fallback path.Documentation
Adds
StdDevoperator sections to the cache and list instruction references, documenting the sample-deviation formula, the fallback rule, and per-change-reason handling.