From f8015c88b90f7aaf89bae53df90fcc7171f40480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Wed, 8 Sep 2021 11:14:11 +0200 Subject: [PATCH] Update Statistics.jl --- src/Statistics.jl | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Statistics.jl b/src/Statistics.jl index 252137f5..8a8701d6 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -937,22 +937,33 @@ function quantile!(q::AbstractArray, v::AbstractVector, p::AbstractArray; end isempty(q) && return q - minp, maxp = extrema(p) - _quantilesort!(v, sorted, minp, maxp) + if length(p) == 2 + for (i, j) in zip(eachindex(p), eachindex(q)) + @inbounds q[j] = quantile!(v, p[i], sorted=sorted, alpha=alpha, beta=beta) + end + else + minp, maxp = extrema(p) + _quantilesort!(v, sorted, minp, maxp) - for (i, j) in zip(eachindex(p), eachindex(q)) - @inbounds q[j] = _quantile(v,p[i], alpha=alpha, beta=beta) + for (i, j) in zip(eachindex(p), eachindex(q)) + @inbounds q[j] = _quantile(v, p[i], alpha=alpha, beta=beta) + end end + return q end function quantile!(v::AbstractVector, p::Union{AbstractArray, Tuple{Vararg{Real}}}; sorted::Bool=false, alpha::Real=1., beta::Real=alpha) + if length(p) == 2 + return map(x -> quantile!(v, x, sorted=sorted, alpha=alpha, beta=beta), p) + end + if !isempty(p) minp, maxp = extrema(p) _quantilesort!(v, sorted, minp, maxp) end - return map(x->_quantile(v, x, alpha=alpha, beta=beta), p) + return map(x -> _quantile(v, x, alpha=alpha, beta=beta), p) end quantile!(v::AbstractVector, p::Real; sorted::Bool=false, alpha::Real=1., beta::Real=alpha) = @@ -969,8 +980,7 @@ function _quantilesort!(v::AbstractArray, sorted::Bool, minp::Real, maxp::Real) lo = floor(Int,minp*(lv)) hi = ceil(Int,1+maxp*(lv)) - # only need to perform partial sort - sort!(v, 1, lv, Base.Sort.PartialQuickSort(lo:hi), Base.Sort.Forward) end + sort!(v, 1, lv, Base.Sort.PartialQuickSort(lo:hi), Base.Sort.Forward) end if (sorted && (ismissing(v[end]) || (v[end] isa Number && isnan(v[end])))) || any(x -> ismissing(x) || (x isa Number && isnan(x)), v)