-
Notifications
You must be signed in to change notification settings - Fork 194
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
d6a36af
2acfbfa
b8be0a5
7653f2c
448e6ee
6a6997a
f9984f8
c1768a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) | ||||||||||||||
|
||||||||||||||
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) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't tried. But without |
||||||||||||||
end | ||||||||||||||
|
||||||||||||||
function mean(f, A::AbstractArray, w::UnitWeights) | ||||||||||||||
length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) | ||||||||||||||
return mean(f, A) | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
##### Weighted quantile ##### | ||||||||||||||
|
||||||||||||||
""" | ||||||||||||||
|
There was a problem hiding this comment.
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 formean
?There was a problem hiding this comment.
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