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 1 commit
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
Prev Previous commit
Next Next commit
Minor modifications
- Add keyword arguments for the weights
- Modified functions to use `Iterators.map`
- Add more tests
  • Loading branch information
itsdebartha committed Aug 22, 2023
commit b8be0a536a3261270bd80f5dac9673e2f66c2961
10 changes: 5 additions & 5 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,11 @@ function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:)
end

"""
mean(f, A::AbstractArray, w::AbstractWeights[, dims::Int])
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 `dim` is provided, compute the
`AbstractWeights`). If `dims` is provided, compute the
weighted mean along dimension `dims`.

# Examples
Expand All @@ -698,13 +698,13 @@ w = rand(n)
mean(√, x, weights(w))
```
"""
mean(f, A::AbstractArray, w::AbstractWeights; dims::Union{Colon,Int}=:) =
_mean(f.(A), w, dims)
mean(f, A::AbstractArray, w::AbstractWeights; kwargs...) =
mean(collect(Iterators.map(f, A)), w; kwargs...)

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."))
return mean(f.(A), dims=dims)
return mean(collect(Iterators.map(f, A)), dims=dims)
end

##### Weighted quantile #####
Expand Down
32 changes: 29 additions & 3 deletions test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,48 @@ end
@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

@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 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