Skip to content

Commit cde87c8

Browse files
bkaminsnalimilan
andauthored
Fix bug in quantile for case of 1 element vector (#42)
* Fix bug in quantile for case of 1 element vector * add tests * make the implementation type-stable * Remove ) Co-authored-by: Milan Bouchet-Valat <[email protected]>
1 parent 81a1cdd commit cde87c8

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Statistics.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,14 +969,22 @@ end
969969
require_one_based_indexing(v)
970970

971971
n = length(v)
972+
973+
@assert n > 0 # this case should never happen here
974+
972975
m = alpha + p * (one(alpha) - alpha - beta)
973976
aleph = n*p + oftype(p, m)
974977
j = clamp(trunc(Int, aleph), 1, n-1)
975978
γ = clamp(aleph - j, 0, 1)
976979

977-
a = v[j]
978-
b = v[j + 1]
979-
980+
if n == 1
981+
a = v[1]
982+
b = v[1]
983+
else
984+
a = v[j]
985+
b = v[j + 1]
986+
end
987+
980988
if isfinite(a) && isfinite(b)
981989
return a + γ*(b-a)
982990
else

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ end
557557
@test_throws ArgumentError quantile([1, missing], 0.5)
558558
@test_throws ArgumentError quantile([1, NaN], 0.5)
559559
@test quantile(skipmissing([1, missing, 2]), 0.5) === 1.5
560+
@test quantile([1], 0.5) === 1.0
560561

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

0 commit comments

Comments
 (0)