-
Notifications
You must be signed in to change notification settings - Fork 40
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
Incorrect quantiles for floating-point and integer arrays #144
Comments
Thanks. The problem seems to be due to this line: Statistics.jl/src/Statistics.jl Line 1014 in f13706e
E.g. with julia> (a, b, γ) = (-1.0e20, 100.0, 1)
(-1.0e20, 100.0, 1)
julia> a + γ*(b-a)
0.0
julia> (1-γ)*a + γ*b
100.0 We could change it to use Adding a small quantity like Statistics.jl/src/Statistics.jl Line 1001 in f13706e
@andreasnoack What do you think? |
I tried to read through the old issue. It's not clear to me why the original version causes unsorted quantiles. |
Actually it's not the original/current version which causes unsorted quantiles, it's the one I tested ( Here's what happens using the test case from JuliaLang/julia#16572 and making the julia> y = [0.40003674665581906,0.4085630862624367,0.41662034698690303,0.41662034698690303,0.42189053966652057,0.42189053966652057,0.42553514344518345,0.43985732442991354]
julia> quantile(y, range(0.01, 0.99, length=17)[6])
(a, b, γ) = (0.41662034698690303, 0.41662034698690303, 0.2137500000000001)
0.4166203469869031
julia> (a, b, γ) = (0.41662034698690303, 0.41662034698690303, 0.2137500000000001)
(0.41662034698690303, 0.41662034698690303, 0.2137500000000001)
julia> a + γ*(b-a)
0.41662034698690303
julia> (1-γ)*a + γ*b
0.4166203469869031
julia> quantile(y, range(0.01, 0.99, length=17)[7])
(a, b, γ) = (0.41662034698690303, 0.41662034698690303, 0.6425000000000001)
0.416620346986903
julia> (a, b, γ) = (0.41662034698690303, 0.41662034698690303, 0.6425000000000001)
(0.41662034698690303, 0.41662034698690303, 0.6425000000000001)
julia> a + γ*(b-a)
0.41662034698690303
julia> (1-γ)*a + γ*b
0.416620346986903 I'm not sure what can be done about this. We could easily check whether julia> (a, γ) = (0.41662034698690303, 0.2137500000000001)
(0.41662034698690303, 0.2137500000000001)
julia> (1-γ)*nextfloat(a) + γ*a
0.4166203469869031
julia> (a, γ) = (0.41662034698690303, 0.6425000000000001)
(0.41662034698690303, 0.6425000000000001)
julia> (1-γ)*nextfloat(a) + γ*a
0.41662034698690303 Maybe we should check whether the result is approximately equal to |
Say we have an array with a large negative number and a smaller positive number:
Both of these numbers are represented exactly in
Float16
:Julia will return the wrong answer for quantile queries over this array:
If we make the large number bigger, then quantile queries over
Float32
andFloat64
arrays are also incorrect:This can happen when both numbers are small:
And it can happen when the array contains more than two numbers:
For integers there’s the interesting twist – the quantiles can exceed the representable values of the integer type:
In some cases every quantile other than the 0th percentile is incorrect. Interestingly, the values decrease as we query successively higher percentiles:
If the numbers are big enough, comparable results can be found for
Int32
andInt64
:Randomized testing suggests that for
Int32
this behavior occurs more frequently for short integer arrays. Based on a million samples at each length, approximatelyFor
Int64
it looks like the error rate may not go down as array size decreases, and approximatelyThe function I used to compute these estimates
This behavior reproduces on the current LTS release, Julia 1.6, as well as Julia 1.9.1, the current release as of 2023-06-07.
The text was updated successfully, but these errors were encountered: