Skip to content

Fix var/std along a dimension with UnitWeights#1001

Merged
devmotion merged 3 commits into
masterfrom
dmw/fix-var-unitweights-dim
Jun 17, 2026
Merged

Fix var/std along a dimension with UnitWeights#1001
devmotion merged 3 commits into
masterfrom
dmw/fix-var-unitweights-dim

Conversation

@devmotion

Copy link
Copy Markdown
Member

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)):

function var(v::AbstractArray{<:Real}, w::UnitWeights, dim::Int; mean=nothing,
                  corrected::Union{Bool, Nothing}=nothing)
    length(w) == length(v) || throw(DimensionMismatch("Inconsistent array lengths."))
    ...
end

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:

julia> using StatsBase

julia> var(reshape(1.0:12.0, 3, 4), uweights(3), 1)
ERROR: DimensionMismatch: Inconsistent array lengths.

This compares against size(v, dim) instead, consistent with the existing mean(A, w::UnitWeights; dims) method which already checks size(A, dims) == length(w).

weight_funcs in test/moments.jl is (weights, aweights, fweights, pweights) and does not include uweights, so the matrix var(x, w, dim) / std(x, w, dim) tests never exercised UnitWeights — hence the gap. Added a regression testset covering weighted var/std along a dimension with UnitWeights, including the mismatched-length error case.

Also bumped the patch version to 0.34.12.

🤖 Generated with Claude Code

devmotion and others added 2 commits June 16, 2026 15:09
`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>
Comment thread test/moments.jl Outdated
@devmotion devmotion enabled auto-merge (squash) June 17, 2026 06:06
@devmotion devmotion requested a review from andreasnoack June 17, 2026 06:06
@devmotion devmotion merged commit eafd935 into master Jun 17, 2026
10 checks passed
@devmotion devmotion deleted the dmw/fix-var-unitweights-dim branch June 17, 2026 07:17
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.

2 participants