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..6ba730707 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(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"