Fix var/std along a dimension with UnitWeights#1001
Merged
Conversation
`var(A::AbstractArray, w::UnitWeights, dim::Int)` (and therefore `std`
along a dimension, which dispatches to it) compared the weight length
against the *total* number of array elements (`length(v)`) instead of the
size along the reduction dimension (`size(v, dim)`).
As a result the method threw `DimensionMismatch("Inconsistent array
lengths.")` for essentially any multidimensional array, e.g.
var(reshape(1.0:12.0, 3, 4), uweights(3), 1)
even though the weight vector correctly matched the reduced dimension.
Compare against `size(v, dim)`, consistent with the existing
`mean(A, w::UnitWeights; dims)` method, and add regression tests covering
weighted `var`/`std` along a dimension with `UnitWeights`.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
devmotion
commented
Jun 17, 2026
andreasnoack
approved these changes
Jun 17, 2026
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.
var(A::AbstractArray, w::UnitWeights, dim::Int)— and thereforestdalong a dimension, which dispatches to it — compared the weight length against the total number of array elements (length(v)) instead of the size along the reduction dimension (size(v, dim)):As a result the method threw
DimensionMismatch("Inconsistent array lengths.")for essentially any multidimensional array, even when the weight vector correctly matched the reduced dimension:This compares against
size(v, dim)instead, consistent with the existingmean(A, w::UnitWeights; dims)method which already checkssize(A, dims) == length(w).weight_funcsintest/moments.jlis(weights, aweights, fweights, pweights)and does not includeuweights, so the matrixvar(x, w, dim)/std(x, w, dim)tests never exercisedUnitWeights— hence the gap. Added a regression testset covering weightedvar/stdalong a dimension withUnitWeights, including the mismatched-length error case.Also bumped the patch version to 0.34.12.
🤖 Generated with Claude Code