Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]

Expand Down