StatsBase.jl
StatsBase.jl copied to clipboard
fitting Histogram throws InexactError for very small values
fit(Histogram, X) fails when X contains values x for which iszero(x) == false but isinf(1 / x) == true.
Example:
julia> fit(Histogram, [2e-314; zeros(100)])
ERROR: InexactError: Int64(NaN)
Stacktrace:
[1] Int64
@ ./float.jl:723 [inlined]
[2] histrange(lo::Float64, hi::Float64, n::Int64, closed::Symbol)
@ StatsBase ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:99
[3] histrange(v::Vector{Float64}, n::Int64, closed::Symbol)
@ StatsBase ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:39
[4] #142
@ ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:103 [inlined]
[5] map
@ ./tuple.jl:232 [inlined]
[6] histrange
@ ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:102 [inlined]
[7] #fit#158
@ ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:332 [inlined]
[8] #fit#151
@ ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:276 [inlined]
[9] fit(::Type{Histogram{Int64, N, E} where {N, E}}, v::Vector{Float64})
@ StatsBase ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:276
[10] #fit#161
@ ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:383 [inlined]
[11] fit(::Type{Histogram}, args::Vector{Float64})
@ StatsBase ~/.julia/packages/StatsBase/pJqvO/src/hist.jl:383
[12] top-level scope
@ REPL[66]:1
julia> iszero(2e-314)
false
julia> isinf(1 / 2e-314)
true
Huh, weird edge case. Perhaps worth noting that 2e-314 is subnormal, i.e. it's less than floatmin(), and using floatmin() here works as expected. This isn't my area of expertise; perhaps @simonbyrne might have some sagely guidance here?