Skip to content

Commit

Permalink
Fix bug in quantile for case of 1 element vector (JuliaStats#42)
Browse files Browse the repository at this point in the history
* Fix bug in quantile for case of 1 element vector

* add tests

* make the implementation type-stable

* Remove )

Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
  • Loading branch information
bkamins and nalimilan authored Jun 5, 2020
1 parent 81a1cdd commit cde87c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -969,14 +969,22 @@ end
require_one_based_indexing(v)

n = length(v)

@assert n > 0 # this case should never happen here

m = alpha + p * (one(alpha) - alpha - beta)
aleph = n*p + oftype(p, m)
j = clamp(trunc(Int, aleph), 1, n-1)
γ = clamp(aleph - j, 0, 1)

a = v[j]
b = v[j + 1]

if n == 1
a = v[1]
b = v[1]
else
a = v[j]
b = v[j + 1]
end

if isfinite(a) && isfinite(b)
return a + γ*(b-a)
else
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ end
@test_throws ArgumentError quantile([1, missing], 0.5)
@test_throws ArgumentError quantile([1, NaN], 0.5)
@test quantile(skipmissing([1, missing, 2]), 0.5) === 1.5
@test quantile([1], 0.5) === 1.0

# make sure that type inference works correctly in normal cases
for T in [Int, BigInt, Float64, Float16, BigFloat, Rational{Int}, Rational{BigInt}]
Expand Down

0 comments on commit cde87c8

Please sign in to comment.