-
Notifications
You must be signed in to change notification settings - Fork 45
Improve quantile performance v2 #91
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
b6ceb83
7943e97
af561f2
6e233de
d84deda
fb4c8d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -949,35 +948,34 @@ 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) | ||
| 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}) | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this out of the loop? BTW, better use |
||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you completely sure of the |
||
| 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 | ||
|
|
||
| # 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")) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.