Repository navigation
Preserve mean accumulator state and recover extreme means and standard deviations - #176
Merged
Merged
Conversation
jmh530
force-pushed
the
codex/univariate-review
branch
from
October 18, 2026 21:33
d768613 to
ca7b216
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merge matching-element-type summators directly instead of converting the incoming accumulator to a scalar sum. The conversion discarded saved corrections and precise overflow information before subsequent observations could cancel the large terms. Use the summator's existing merge operation without changing public fields, scalar insertion, or batch accumulation. Retain the existing behavior for other element types. Add regressions for compensated low-order contributions, precise overflow followed by cancellation, source preservation, and empty and self merges. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Retry nonfinite means when the input supports an independent saved traversal. Finite observations such as two maximum values can have a representable mean even when their raw sum overflows. Accumulate large observations with power-of-two scaling and retain tiny observations in a separate unscaled precise sum. Combine the subtotals before rounding when the unscaled subtotal is finite, preserving small remainders from cancellation between the groups. Otherwise divide before restoring the scale. Keep the selected summation algorithm for ordinary finite results and preserve its handling of nonfinite converted observations. Save forward ranges before consumption and support flattened slice traversal. Document that single-pass inputs, streaming accumulators, and finite cancellation errors are outside this recovery path. Add regressions for extreme finite values, subnormal cancellation, arrays and strided slices, shared-cursor forward ranges, single-pass inputs, and nonfinite observations across float, double, and real. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Computing sqrt(variance) can return zero or infinity even when the standard deviation is representable. Retry nonfinite, negative, zero, and subnormal variances for replayable floating-point inputs. Normalize observations by a power of two and use compensated two-pass accumulation. Subtract a fixed normalized observation before centering to avoid introducing spread into identical large values. Preserve the zero-centered definition of assumeZeroMean and use a cheaper equality check for zero-variance constant inputs. Keep the selected algorithm for positive normal variances, preserve existing contracts and nonfinite-input behavior, and leave single-pass inputs unchanged. Use direct loops to retain allocation-free operation. This does not repair inaccurate positive normal variances. Add regressions for extreme and subnormal magnitudes, sample and population results, constant inputs, varargs, strided slices, and saved forward-range cursors. Document recovery behavior and update the naive algorithm example to account for recoverable cancellation. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Explain how converting large integer observations to floating-point accumulators can discard small differences and produce zero variance. Illustrate the limitation with five consecutive long values near 2^60, whose sample variance is 2.5. Describe subtracting a known common offset before conversion as a workaround, provided the integer subtraction does not overflow. Clarify that this translation is unsuitable for assumeZeroMean and that standard deviation's floating-point recovery does not repair lost integer precision. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Extract the summator merge policy into an internal mergeSummator helper. Merge matching element types directly when the operation compiles, preserving saved partial sums and corrections. Otherwise retain the existing scalar conversion path and its constraints. Use the helper in MeanAccumulator to restore compatibility with decimal summation, whose summator currently lacks a direct merge operation. Keep capability detection entirely at compile time, with counts and statistical correction formulas remaining the caller's responsibility. Document the distinct-source requirement for self merges. Add regressions for const sources, compensated-state preservation, decimal merging, and empty and self merges. Register the helper module with Meson. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Explain how mergeSummator preserves summation state when direct merging is supported and falls back to scalar conversion otherwise. Clarify that callers manage observation counts and statistical corrections. Follow the univariate internal modules' documentation style by stating the helper's package visibility and linking to the public statistical functions. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Exercise the recovery fallbacks through the public assumeZeroMean API, which has no minimum-count contract. Verify that empty population input and a zero-valued singleton sample preserve their NaN results rather than attempting recovery without observations or a nonzero scale. Also verify that the singleton's population standard deviation is zero. These regressions cover both previously missed fallback returns without changing the implementation or input contracts. AI-Assisted-By: Codex (GPT-6 Astra Medium)
jmh530
force-pushed
the
codex/univariate-review
branch
from
October 18, 2026 22:06
00c1955 to
a2fed35
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Means and standard deviations can be representable even when their intermediate sums or variances overflow or underflow. This PR adds recovery for replayable floating-point inputs and preserves saved summation state when merging mean accumulators.
Changes
assumeZeroMean. Save forward-range traversal state before consuming the input.For example, the mean of two maximum finite values now remains finite. Likewise, the sample standard deviations of
[-1e200, 0, 1e200]and[-1e-200, 0, 1e-200]can be recovered even though their variances exceed the representable range ofdouble.Scope and performance
Ordinary finite means and positive normal variances retain their selected algorithms. Single-pass inputs do not receive recovery. This does not generally repair finite cancellation errors, inaccurate positive normal variances, streaming accumulator overflow, or integer differences lost during floating-point conversion.
Paired local benchmarks found no consistent material slowdown for ordinary larger floating-point arrays. Large constant arrays incurred approximately 24–26% overhead in standard deviation because zero variance requires checking whether the observations are genuinely identical.
Validation
AI-Assisted-By: Codex (GPT-6 Astra Medium)