-
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 2 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,31 @@ function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) | |||||||||
return mean(A, dims=dims) | ||||||||||
end | ||||||||||
|
||||||||||
""" | ||||||||||
mean(f, A::AbstractArray, w::AbstractWeights[, dims::Int]) | ||||||||||
|
||||||||||
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 | ||||||||||
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; dims::Union{Colon,Int}=:) = | ||||||||||
_mean(f.(A), w, dims) | ||||||||||
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. Avoid memory allocation by using
Suggested change
I'd also suggest making it slightly more generic, as
Suggested change
(See below for more details.) |
||||||||||
|
||||||||||
function mean(f, A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) | ||||||||||
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. 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
Suggested change
|
||||||||||
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(f.(A), dims=dims) | ||||||||||
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. Avoid memory allocation by using
Suggested change
I'd also suggest making it slightly more generic, as
Suggested change
|
||||||||||
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.
dims
shouldn't be required to be an integer.