From 7597c37e034d5365e08789b058acb3ab0372bd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Tue, 16 Jun 2026 15:07:05 +0200 Subject: [PATCH 1/2] Fix `var`/`std` along a dimension with `UnitWeights` `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) --- Project.toml | 2 +- src/moments.jl | 2 +- test/moments.jl | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index e1c2daed1..07569e162 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "StatsBase" uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" -version = "0.34.11" +version = "0.34.12" authors = ["JuliaStats"] [deps] diff --git a/src/moments.jl b/src/moments.jl index 04de97a83..dcfe1e6b7 100644 --- a/src/moments.jl +++ b/src/moments.jl @@ -73,7 +73,7 @@ function var(A::AbstractArray{<:Real}, w::AbstractWeights, dim::Int; mean=nothin end 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.")) + size(v, dim) == length(w) || throw(DimensionMismatch("Inconsistent array lengths.")) corrected = depcheck(:var, :corrected, corrected) return var(v; mean, corrected, dims=dim) end diff --git a/test/moments.jl b/test/moments.jl index a3402b965..fd4ad33c2 100644 --- a/test/moments.jl +++ b/test/moments.jl @@ -481,4 +481,20 @@ end end end +@testset "weighted var/std along a dimension with UnitWeights" begin + x = reshape(collect(1.0:12.0), 3, 4) + + # `UnitWeights` along a dimension must match the unweighted result and must + # not erroneously compare the weight length against the total array length. + @test var(x, uweights(3), 1; corrected=true) ≈ var(x; dims=1) + @test var(x, uweights(4), 2; corrected=true) ≈ var(x; dims=2) + @test var(x, uweights(3), 1; corrected=false) ≈ var(x; dims=1, corrected=false) + @test std(x, uweights(3), 1; corrected=true) ≈ std(x; dims=1) + @test std(x, uweights(4), 2; corrected=true) ≈ std(x; dims=2) + + # A weight vector whose length does not match the reduction dimension errors. + @test_throws DimensionMismatch var(x, uweights(4), 1; corrected=true) + @test_throws DimensionMismatch var(x, uweights(3), 2; corrected=true) +end + end # @testset "StatsBase.Moments" From d930fdbe6906cdd7d3c164e94ce3d70e78093fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Wed, 17 Jun 2026 07:19:26 +0200 Subject: [PATCH 2/2] Update test/moments.jl --- test/moments.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/moments.jl b/test/moments.jl index fd4ad33c2..6ba730707 100644 --- a/test/moments.jl +++ b/test/moments.jl @@ -482,7 +482,7 @@ end end @testset "weighted var/std along a dimension with UnitWeights" begin - x = reshape(collect(1.0:12.0), 3, 4) + x = reshape(1.0:12.0, 3, 4) # `UnitWeights` along a dimension must match the unweighted result and must # not erroneously compare the weight length against the total array length.