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 6 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
29 changes: 29 additions & 0 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,35 @@ function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:)
return mean(A, dims=dims)
end

"""
mean(f, A::AbstractArray, w::AbstractWeights)

Compute the weighted mean of array `A`, after transforming it'S
contents with the function `f`, with weight vector `w` (of type
`AbstractWeights`).

# Examples
```julia
n = 20
x = rand(n)
w = rand(n)
mean(√, x, weights(w))
```
"""
mean(f, A::AbstractArray, w::AbstractWeights) =
_funcweightedmean(f, A, w)
Copy link
Member

Choose a reason for hiding this comment

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

I don't remember, was there any particular reason for introducing _funcweightedmean instead of implementing two methods for mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No there wasn't any such reason. I am removing this function, and implementing it as a method for mean


function _funcweightedmean(f, A::AbstractArray, w::AbstractWeights)
return sum(Broadcast.broadcasted(f, A, w) do f, a_i, wg
return f(a_i) * wg
end) / sum(w)
Copy link
Member

Choose a reason for hiding this comment

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

You should use Broadcast.instantiate. Moreover, to me it seems this should be changed to

Suggested change
return sum(Broadcast.broadcasted(f, A, w) do f, a_i, wg
return f(a_i) * wg
end) / sum(w)
return sum(Broadcast.instantiate(Broadcast.broadcasted(A, w) do a_i, wg
return f(a_i) * wg
end)) / sum(w)

Copy link
Contributor

Choose a reason for hiding this comment

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

Using Broadcast.instantiate causes extra allocations, no?

Copy link
Member

Choose a reason for hiding this comment

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

I haven't tried. But without Broadcast.instantiate it will be slow and fall back to Cartesian indexing. See, e.g., JuliaLang/julia#31020 ("we require Broadcast.instantiate for fast reduce").

end

function mean(f, A::AbstractArray, w::UnitWeights)
length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension."))
return mean(f, A)
end

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

"""
Expand Down
21 changes: 21 additions & 0 deletions test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,27 @@ 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

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
Loading