Skip to content

Commit 576db0f

Browse files
authored
Allow for quantile to operate over an entire Matrix (#108)
1 parent e133202 commit 576db0f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Statistics.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,19 @@ function quantile!(v::AbstractVector, p::Union{AbstractArray, Tuple{Vararg{Real}
958958
end
959959
return map(x->_quantile(v, x, alpha=alpha, beta=beta), p)
960960
end
961+
quantile!(a::AbstractArray, p::Union{AbstractArray,Tuple{Vararg{Real}}};
962+
sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
963+
quantile!(vec(a), p, sorted=sorted, alpha=alpha, beta=alpha)
961964

962-
quantile!(v::AbstractVector, p::Real; sorted::Bool=false, alpha::Real=1., beta::Real=alpha) =
965+
quantile!(q::AbstractArray, a::AbstractArray, p::Union{AbstractArray,Tuple{Vararg{Real}}};
966+
sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
967+
quantile!(q, vec(a), p, sorted=sorted, alpha=alpha, beta=alpha)
968+
969+
quantile!(v::AbstractVector, p::Real; sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) =
963970
_quantile(_quantilesort!(v, sorted, p, p), p, alpha=alpha, beta=beta)
964971

965972
# Function to perform partial sort of v for quantiles in given range
966-
function _quantilesort!(v::AbstractArray, sorted::Bool, minp::Real, maxp::Real)
973+
function _quantilesort!(v::AbstractVector, sorted::Bool, minp::Real, maxp::Real)
967974
isempty(v) && throw(ArgumentError("empty data vector"))
968975
require_one_based_indexing(v)
969976

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ end
613613
@test quantile(Any[1, Float16(2), 3], Float16(0.5)) isa Float16
614614
@test quantile(Any[1, big(2), 3], Float16(0.5)) isa BigFloat
615615

616+
@test quantile(reshape(1:100, (10, 10)), [0.00, 0.25, 0.50, 0.75, 1.00]) ==
617+
[1.0, 25.75, 50.5, 75.25, 100.0]
618+
616619
# Need a large vector to actually check consequences of partial sorting
617620
x = rand(50)
618621
for sorted in (false, true)
@@ -639,6 +642,11 @@ end
639642
@test quantile!(y, x, [0.1, 0.5, 0.9]) === y
640643
@test y [1.2, 2.0, 2.8]
641644

645+
x = reshape(collect(1:100), (10, 10))
646+
y = zeros(5)
647+
@test quantile!(y, x, [0.00, 0.25, 0.50, 0.75, 1.00]) === y
648+
@test y [1.0, 25.75, 50.5, 75.25, 100.0]
649+
642650
#tests for quantile calculation with configurable alpha and beta parameters
643651
v = [2, 3, 4, 6, 9, 2, 6, 2, 21, 17]
644652

0 commit comments

Comments
 (0)