JuliaStats / StatsBase.jl

Basic statistics for Julia
Other
584 stars 194 forks source link

quantile is broken with unitful quantities #883

Open juliohm opened 1 year ago

juliohm commented 1 year ago

The quantile function with unitful quantities works fine without weights:

julia> x = rand(10) .* 1u"m"
10-element Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}:
   0.4560455822873759 m
   0.4307569757744304 m
   0.4840857672199611 m
   0.7853776015820557 m
   0.5865440640498318 m
  0.15996668178343565 m
 0.042204899384880146 m
   0.3310111766347721 m
   0.8691765138294111 m
   0.5325561348479884 m

julia> quantile(x, 0.5)
0.4700656747536685 m

However, if we try to pass weights, it crashes:

julia> quantile(x, Weights(1:10), 0.5)
ERROR: MethodError: no method matching quantile(::Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, ::Weights{Int64, Int64, UnitRange{Int64}}, ::Float64)

Closest candidates are:
  quantile(::AbstractVector{T} where T<:Real, ::AbstractWeights{var"#s42", T} where {var"#s42"<:Real, T<:Real}, ::Number)
   @ StatsBase ~/.julia/packages/StatsBase/XgjIN/src/weights.jl:772
  quantile(::AbstractVector{V}, ::AbstractWeights{W, T} where T<:Real, ::AbstractVector{T} where T<:Real) where {V, W<:Real}
   @ StatsBase ~/.julia/packages/StatsBase/XgjIN/src/weights.jl:700
  quantile(::AbstractVector, ::Any; sorted, alpha, beta)
   @ Statistics ~/.julia/juliaup/julia-1.9.2+0.x64.linux.gnu/share/julia/stdlib/v1.9/Statistics/src/Statistics.jl:1073
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

This method definition is too restrictive: https://github.com/JuliaStats/StatsBase.jl/blob/8696d51cddf65b7fd927fccafdf8b6255cfb0ee9/src/weights.jl#L707

ParadaCarleton commented 1 year ago

where T<:Real

Non-Real Unitful units strike again...