Repository navigation
Fix axis variance divisor when ddof is applied - #2972
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
Contributor
Merging this PR will not alter performance1 warning
Comparing |
This branch has not been deployed
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.
Fixes #2937
Summary
Axis-wise
variance(andmeanwith a non-zeroddof) divides by the wrong value when the reduction produces more than one output element. For a 2x3 array reduced over axis 1 withddof=2, each row has squared-deviation sum 8 and 3 samples, so the expected result is8 / (3 - 2) = 8. xtensor returned4.Root Cause & Problem
detail::meancalledmean_division(s, size - ddof).mean_divisionthen computed the divisor as(size - ddof) / s.size()using integer arithmetic, wheresizeis the total input element count ands.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_divisionnow receives the totalsizeandddofseparately and derives the divisor from the sample count per output element, applying theddofcorrection per lane and guarding against an invalidddoflarger than that count. Both axis-reductiondetail::meanoverloads passsizeandddofdirectly. A Google Test regression case intest/test_extended_xmath_reducers.cppcovers 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.cppthen run: before the fix printed4 4with exit 1; after the fix printed8 8with exit 0.clang++ -std=c++20 ... xtensor_ddof_targeted.cppthen run: compiled with exit 0 and all 9 checks passed (variance(a,{1},2),variance(a,vector{1},2),ddof=0andddof=1controls, and the flattenedvariance(a,2)path).clang-format --dry-run --Werroron both changed files: exit 0, no violations.The full Google Test suite is left to CI. No full local build was run.