Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsBase"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.34.11"
version = "0.34.12"
authors = ["JuliaStats"]

[deps]
Expand Down
2 changes: 1 addition & 1 deletion src/moments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/moments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"