Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weighted mean with function #886

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,31 @@ function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:)
return mean(A, dims=dims)
end

"""
mean(f, A::AbstractArray, w::AbstractWeights[; dims])

Compute the weighted mean of array `A`, after transforming it'S
contents with the function `f`, with weight vector `w` (of type
`AbstractWeights`). If `dims` is provided, compute the
weighted mean along dimension `dims`.

# Examples
```julia
n = 20
x = rand(n)
w = rand(n)
mean(√, x, weights(w))
```
"""
mean(f, A::AbstractArray, w::AbstractWeights; kwargs...) =
itsdebartha marked this conversation as resolved.
Show resolved Hide resolved
mean(collect(Iterators.map(f, A)), w; kwargs...)
ParadaCarleton marked this conversation as resolved.
Show resolved Hide resolved

function mean(f, A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slightly more generic version of the same thing, which is less likely to require maintenance in the future (if we add additional keyword arguments to mean). I suggest using this pattern when you can.

Suggested change
function mean(f, A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:)
function mean(f, A::AbstractArray, w::UnitWeights; kwargs...)

a = (dims === :) ? length(A) : size(A, dims)
a != length(w) && throw(DimensionMismatch("Inconsistent array dimension."))
ParadaCarleton marked this conversation as resolved.
Show resolved Hide resolved
return mean(collect(Iterators.map(f, A)), dims=dims)
end

##### Weighted quantile #####

"""
Expand Down
47 changes: 47 additions & 0 deletions test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,53 @@ end
@test mean(a, f(wt), dims=3) ≈ sum(a.*reshape(wt, 1, 1, length(wt)), dims=3)/sum(wt)
@test_throws ErrorException mean(a, f(wt), dims=4)
end

@test mean(√, [1:3;], f([1.0, 1.0, 0.5])) ≈ 1.3120956
@test mean(√, 1:3, f([1.0, 1.0, 0.5])) ≈ 1.3120956
@test mean(√, [1 + 2im, 4 + 5im], f([1.0, 0.5])) ≈ 1.60824421 + 0.88948688im
ParadaCarleton marked this conversation as resolved.
Show resolved Hide resolved

@test mean(log, [1:3;], f([1.0, 1.0, 0.5])) ≈ 0.49698133
@test mean(log, 1:3, f([1.0, 1.0, 0.5])) ≈ 0.49698133
@test mean(log, [1 + 2im, 4 + 5im], f([1.0, 0.5])) ≈ 1.155407982 + 1.03678427im

@test mean(x -> x^2, [1:3;], f([1.0, 1.0, 0.5])) ≈ 3.8
@test mean(x -> x^2, 1:3, f([1.0, 1.0, 0.5])) ≈ 3.8
@test mean(x -> x^2, [1 + 2im, 4 + 5im], f([1.0, 0.5])) ≈ -5.0 + 16.0im

for wt in ([1.0, 1.0, 1.0], [1.0, 0.2, 0.0], [0.2, 0.0, 1.0])
@test mean(√, a, f(wt); dims=1) ≈ sum(sqrt.(a).*reshape(wt, length(wt), 1, 1), dims=1)/sum(wt)
@test mean(√, a, f(wt); dims=2) ≈ sum(sqrt.(a).*reshape(wt, 1, length(wt), 1), dims=2)/sum(wt)
@test mean(√, a, f(wt); dims=3) ≈ sum(sqrt.(a).*reshape(wt, 1, 1, length(wt)), dims=3)/sum(wt)
@test_throws ErrorException mean(√, a, f(wt), dims=4)

@test mean(log, a, f(wt); dims=1) ≈ sum(log.(a).*reshape(wt, length(wt), 1, 1), dims=1)/sum(wt)
@test mean(log, a, f(wt); dims=2) ≈ sum(log.(a).*reshape(wt, 1, length(wt), 1), dims=2)/sum(wt)
@test mean(log, a, f(wt); dims=3) ≈ sum(log.(a).*reshape(wt, 1, 1, length(wt)), dims=3)/sum(wt)
@test_throws ErrorException mean(log, a, f(wt), dims=4)

@test mean(x -> x^2, a, f(wt); dims=1) ≈ sum((a.^2).*reshape(wt, length(wt), 1, 1), dims=1)/sum(wt)
@test mean(x -> x^2, a, f(wt); dims=2) ≈ sum((a.^2).*reshape(wt, 1, length(wt), 1), dims=2)/sum(wt)
@test mean(x -> x^2, a, f(wt); dims=3) ≈ sum((a.^2).*reshape(wt, 1, 1, length(wt)), dims=3)/sum(wt)
@test_throws ErrorException mean(log, a, f(wt), dims=4)
end

b = reshape(1.0:9.0, 3, 3)
w = UnitWeights{Float64}(3)
@test mean(√, b, w; dims=1) ≈ reshape(w, :, 3) * sqrt.(b) / sum(w)
@test mean(√, b, w; dims=2) ≈ sqrt.(b) * w / sum(w)
@test mean(log, b, w; dims=1) ≈ reshape(w, :, 3) * log.(b) / sum(w)
@test mean(log, b, w; dims=2) ≈ log.(b) * w / sum(w)
@test mean(x -> x^2, b, w; dims=1) ≈ reshape(w, :, 3) * (b.^2) / sum(w)
@test mean(x -> x^2, b, w; dims=2) ≈ (b.^2) * w / sum(w)

c = 1.0:9.0
w = UnitWeights{Float64}(9)
@test mean(√, c, w) ≈ sum(sqrt.(c)) / length(c)
@test_throws DimensionMismatch mean(√, c, UnitWeights{Float64}(6))
@test mean(log, c, w) ≈ sum(log.(c)) / length(c)
@test_throws DimensionMismatch mean(log, c, UnitWeights{Float64}(6))
@test mean(x -> x^2, c, w) ≈ sum(c.^2) / length(c)
@test_throws DimensionMismatch mean(x -> x^2, c, UnitWeights{Float64}(6))
end

@testset "Quantile fweights" begin
Expand Down