Skip to content

Commit 0ce3149

Browse files
authored
add methods for median and quantile with function as the first argument (#186)
Adds `quantile(f, v)` and `median(f, v)` methods, where `f` is a function, and `v` is an abstract array. Fixes #141, #27.
1 parent 77bd570 commit 0ce3149

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Statistics.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,24 @@ _median(v::AbstractArray{T}, ::Colon) where {T} = median!(copyto!(Array{T,1}(und
904904

905905
median(r::AbstractRange{<:Real}) = mean(r)
906906

907+
"""
908+
median(f, v)
909+
910+
Apply the function `f` to each element of collection `v`
911+
and then compute the median.
912+
913+
```jldoctest
914+
julia> using Statistics
915+
916+
julia> median(√, [1, 3, 2])
917+
1.4142135623730951
918+
919+
julia> median([√1, √3, √2])
920+
1.4142135623730951
921+
```
922+
"""
923+
median(f::Function, v) = median!(f.(v))
924+
907925
"""
908926
quantile!([q::AbstractArray, ] v::AbstractVector, p; sorted=false, alpha::Real=1.0, beta::Real=alpha)
909927
@@ -1115,6 +1133,33 @@ julia> quantile(skipmissing([1, 10, missing]), 0.5)
11151133
quantile(itr, p; sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
11161134
quantile!(collect(itr), p, sorted=sorted, alpha=alpha, beta=beta)
11171135

1136+
1137+
"""
1138+
quantile(f, v)
1139+
1140+
Apply the function `f` to each element of collection `v`
1141+
and then compute the quantile(s) at a specified probability
1142+
or vector or tuple of probabilities `p` on the interval [0,1].
1143+
1144+
```jldoctest
1145+
julia> using Statistics
1146+
1147+
julia> quantile(√, [1, 3, 2], 0.3)
1148+
1.248528137423857
1149+
1150+
julia> quantile([√1, √3, √2], 0.3)
1151+
1.248528137423857
1152+
1153+
julia> quantile(√, [1, 3, 2], (0.3, 0.4, 0.5))
1154+
(1.248528137423857, 1.3313708498984762, 1.4142135623730951)
1155+
1156+
julia> quantile(.√[1, 3, 2], (0.3, 0.4, 0.5))
1157+
(1.248528137423857, 1.3313708498984762, 1.4142135623730951)
1158+
```
1159+
"""
1160+
quantile(f::Function, v, p; sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
1161+
quantile!(f.(v), p; sorted=sorted, alpha=alpha, beta=beta)
1162+
11181163
quantile(v::AbstractVector, p; sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
11191164
quantile!(sorted ? v : Base.copymutable(v), p; sorted=sorted, alpha=alpha, beta=beta)
11201165

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,19 @@ end
836836
@test_throws InexactError quantile([DateTime(2023, 09, 02), DateTime(2023, 09, 03)], .1)
837837
end
838838

839+
@testset "quantile and median with functions (issue #141, PR #186)" begin
840+
xvec = [3, 1, 2, 4]
841+
for p in (0.2, 0.5, 0.8)
842+
@test quantile(, xvec, p) quantile(.√xvec, p)
843+
@test quantile(x -> x^2, xvec, p) quantile(xvec.^2, p)
844+
@test median(, xvec) median(.√xvec)
845+
@test median(x -> x^2, xvec) median(xvec.^2)
846+
end
847+
848+
y = rand(4)
849+
@test all(quantile(, y, (0.3, 0.4, 0.5)) .≈ quantile(.√y, (0.3, 0.4, 0.5)))
850+
end
851+
839852
@testset "variance of complex arrays (#13309)" begin
840853
z = rand(ComplexF64, 10)
841854
@test var(z) invoke(var, Tuple{Any}, z) cov(z) var(z,dims=1)[1] sum(abs2, z .- mean(z))/9

0 commit comments

Comments
 (0)