diff --git a/src/Statistics.jl b/src/Statistics.jl index 33fc737e..30da0adc 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -958,12 +958,19 @@ function quantile!(v::AbstractVector, p::Union{AbstractArray, Tuple{Vararg{Real} end return map(x->_quantile(v, x, alpha=alpha, beta=beta), p) end +quantile!(a::AbstractArray, p::Union{AbstractArray,Tuple{Vararg{Real}}}; + sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) = + quantile!(vec(a), p, sorted=sorted, alpha=alpha, beta=alpha) -quantile!(v::AbstractVector, p::Real; sorted::Bool=false, alpha::Real=1., beta::Real=alpha) = +quantile!(q::AbstractArray, a::AbstractArray, p::Union{AbstractArray,Tuple{Vararg{Real}}}; + sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) = + quantile!(q, vec(a), p, sorted=sorted, alpha=alpha, beta=alpha) + +quantile!(v::AbstractVector, p::Real; sorted::Bool=false, alpha::Real=1.0, beta::Real=alpha) = _quantile(_quantilesort!(v, sorted, p, p), p, alpha=alpha, beta=beta) # Function to perform partial sort of v for quantiles in given range -function _quantilesort!(v::AbstractArray, sorted::Bool, minp::Real, maxp::Real) +function _quantilesort!(v::AbstractVector, sorted::Bool, minp::Real, maxp::Real) isempty(v) && throw(ArgumentError("empty data vector")) require_one_based_indexing(v) diff --git a/test/runtests.jl b/test/runtests.jl index 783f6302..e4d4ba81 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -613,6 +613,9 @@ end @test quantile(Any[1, Float16(2), 3], Float16(0.5)) isa Float16 @test quantile(Any[1, big(2), 3], Float16(0.5)) isa BigFloat + @test quantile(reshape(1:100, (10, 10)), [0.00, 0.25, 0.50, 0.75, 1.00]) == + [1.0, 25.75, 50.5, 75.25, 100.0] + # Need a large vector to actually check consequences of partial sorting x = rand(50) for sorted in (false, true) @@ -639,6 +642,11 @@ end @test quantile!(y, x, [0.1, 0.5, 0.9]) === y @test y ≈ [1.2, 2.0, 2.8] + x = reshape(collect(1:100), (10, 10)) + y = zeros(5) + @test quantile!(y, x, [0.00, 0.25, 0.50, 0.75, 1.00]) === y + @test y ≈ [1.0, 25.75, 50.5, 75.25, 100.0] + #tests for quantile calculation with configurable alpha and beta parameters v = [2, 3, 4, 6, 9, 2, 6, 2, 21, 17]