Skip to content

Ignore zero weights in immediate mean and variance updates - #71

Open
trestoncuzzort wants to merge 1 commit into
boostorg:developfrom
trestoncuzzort:zero-weight-immediate-statistics
Open

trestoncuzzort wants to merge 1 commit into
boostorg:developfrom
trestoncuzzort:zero-weight-immediate-statistics

Conversation

@trestoncuzzort

Copy link
Copy Markdown

Fixes #44.

An immediate weighted-variance accumulator fed (99, weight=0), (2, weight=1), (4, weight=1) returns NaN for both mean and variance instead of 3 and 1. The initial zero weight poisons the immediate mean, and the variance recurrence later divides by the previous total weight, which is still zero.

Ignore zero-weight updates in the immediate mean, including variates. Guard the variance recurrence against a zero current weight or zero previous total weight. This also avoids squaring a large unused sample before multiplying by zero. Observation counts still include zero-weight samples. Document that extraction with zero total weight remains undefined.

Extend the existing mean and variance tests with leading, interleaved and trailing zeros, integer/floating weights, variates and large finite zero-weight samples.

Validation on Linux:

  • GCC 16.2.1, C++17: all 49 executables in the complete B2 test target pass before and after.
  • GCC C++11: both updated executables pass all four cases / 74 assertions. The new mean and variance regressions fail 12 and 20 assertions respectively with the original headers; existing positive-weight cases pass.
  • 85,500 bounded weighted prefixes across int, float, double and long double sample/weight pairs, compared with an independent two-pass batch calculation: immediate mean and variance each have 13,500 failures before, zero after. The lazy calculation and observation counts match throughout. The check rejects nonfinite results explicitly.
  • Clang 23, C++17, ASan/UBSan plus float-division-by-zero checks and leak detection: both executables pass all four cases / 74 assertions.
  • git diff --check passes. Native Windows and 32-bit builds were not run; generated documentation was not rebuilt.

OpenAI Codex assisted with investigation, implementation and tests. dawnr checked the recurrence guard's denominator conditions with seven provers under an exact-integer, nonnegative-weight abstraction. That does not prove floating-point variance accuracy or the generic/compiled C++ implementation.

Skip zero-weight updates in the immediate mean so an initial zero weight
does not leave a NaN that poisons later samples. Apply the variance
recurrence only when both the new weight and the previous total weight
are nonzero, avoiding its zero denominator after an initial zero weight.

Cover leading, interleaved and trailing zeros, variates, integer and
floating weights, and finite values whose squared deviation overflows.
Document that extraction with zero total weight is undefined.

Assisted-by: OpenAI Codex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

weighted_variance_impl divides by zero if the first weight is zero

1 participant