Skip to content
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

Improve quantile performance v2 #91

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,7 @@ function quantile!(q::AbstractArray, v::AbstractVector, p::AbstractArray;
end
isempty(q) && return q

minp, maxp = extrema(p)
_quantilesort!(v, sorted, minp, maxp)
_quantilesort!(v, sorted, p)

for (i, j) in zip(eachindex(p), eachindex(q))
@inbounds q[j] = _quantile(v,p[i], alpha=alpha, beta=beta)
Expand All @@ -949,35 +948,35 @@ end
function quantile!(v::AbstractVector, p::Union{AbstractArray, Tuple{Vararg{Real}}};
sorted::Bool=false, alpha::Real=1., beta::Real=alpha)
if !isempty(p)
minp, maxp = extrema(p)
_quantilesort!(v, sorted, minp, maxp)
_quantilesort!(v, sorted, p isa Tuple : collect(p) ? p)
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end
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) =
_quantile(_quantilesort!(v, sorted, p, p), p, alpha=alpha, beta=beta)
_quantile(_quantilesort!(v, sorted, [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::AbstractArray, sorted::Bool, p::AbstractVector{<:Real})
bkamins marked this conversation as resolved.
Show resolved Hide resolved
isempty(v) && throw(ArgumentError("empty data vector"))
require_one_based_indexing(v)

if !sorted
lv = length(v)
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)
start = 1
for pv in sort(p)
lv = length(v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this out of the loop? BTW, better use lastindex even if we call require_one_based_indexing(v).

lo = floor(Int,pv*(lv))
hi = ceil(Int,1+pv*(lv))
sort!(v, start, lv, Base.Sort.PartialQuickSort(lo:hi), Base.Sort.Forward)
start = hi + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you completely sure of the +1? Is that still correct if p contains duplicates? That would be worth testing.

end
end
if (sorted && (ismissing(v[end]) || (v[end] isa Number && isnan(v[end])))) ||
any(x -> ismissing(x) || (x isa Number && isnan(x)), v)
throw(ArgumentError("quantiles are undefined in presence of NaNs or missing values"))
end
return v
end

bkamins marked this conversation as resolved.
Show resolved Hide resolved
# Core quantile lookup function: assumes `v` sorted
@inline function _quantile(v::AbstractVector, p::Real; alpha::Real=1.0, beta::Real=alpha)
0 <= p <= 1 || throw(ArgumentError("input probability out of [0,1] range"))
Expand Down