Skip to content

Fix axis variance divisor when ddof is applied - #2972

Open
Tyagiquamar wants to merge 1 commit into
xtensor-stack:masterfrom
Tyagiquamar:fix/variance-axis-ddof-per-lane
Open

Tyagiquamar wants to merge 1 commit into
xtensor-stack:masterfrom
Tyagiquamar:fix/variance-axis-ddof-per-lane

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Fixes #2937

Summary

Axis-wise variance (and mean with a non-zero ddof) divides by the wrong value when the reduction produces more than one output element. For a 2x3 array reduced over axis 1 with ddof=2, each row has squared-deviation sum 8 and 3 samples, so the expected result is 8 / (3 - 2) = 8. xtensor returned 4.

Root Cause & Problem

detail::mean called mean_division(s, size - ddof). mean_division then computed the divisor as (size - ddof) / s.size() using integer arithmetic, where size is the total input element count and s.size() is the number of output elements. For the example this is (6 - 2) / 2 = 2, so each row was divided by 2 instead of 1. The delta-degrees-of-freedom correction must be applied to the per-output sample count (size / s.size() - ddof = 6 / 2 - 2 = 1), not to the total size before it is split into output lanes. The flattened/noaxis path was already correct and is unchanged.

Technical Solution

mean_division now receives the total size and ddof separately and derives the divisor from the sample count per output element, applying the ddof correction per lane and guarding against an invalid ddof larger than that count. Both axis-reduction detail::mean overloads pass size and ddof directly. A Google Test regression case in test/test_extended_xmath_reducers.cpp covers both the braced-init and explicit vector axis containers from the report.

Test Plan

Local validation executed on this branch (clang 22.1.7, -std=c++20, headers from this checkout and xtl master):

  • clang++ -std=c++20 ... repro.cpp then run: before the fix printed 4 4 with exit 1; after the fix printed 8 8 with exit 0.
  • clang++ -std=c++20 ... xtensor_ddof_targeted.cpp then run: compiled with exit 0 and all 9 checks passed (variance(a,{1},2), variance(a,vector{1},2), ddof=0 and ddof=1 controls, and the flattened variance(a,2) path).
  • clang-format --dry-run --Werror on both changed files: exit 0, no violations.

The full Google Test suite is left to CI. No full local build was run.

@codspeed

codspeed Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ Unable to measure the execution of 1 benchmark

This benchmark measured no execution time, so its result is not comparable and counts as unchanged. This usually happens when the compiler removes the code under test.

Prevent compiler optimizations

See affected benchmarks

✅ 255 untouched benchmarks


Comparing Tyagiquamar:fix/variance-axis-ddof-per-lane (c7fb515) with master (eb899ea)

Open in CodSpeed

This branch has not been deployed

No deployments
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.

Axis variance applies ddof before computing the per-lane sample count

1 participant