Skip to content
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

Adding support for more auto histogram methods #533

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,52 @@ function sturges(n) # Sturges' formula
ceil(Integer, log2(n))+1
end

sturges(v::AbstractVector{T}) where {T <: Real} = sturges(length(v))

function _ptp(v::AbstractVector)
e = extrema(v)
return e[2] - e[1]
end

function scott(v::AbstractVector{T}) where {T <: Real}
n = length(v)
h = 3.5 * std(v) * n^(-1/3)
# in the case of a 0 std we need to ensure we
# don't divide by 0
if (h>0)
return ceil(Integer, _ptp(v)/h)
end
return 1
end

function FD(v::AbstractVector{T}) where {T <: Real}
n = length(v)
h = 2 * iqr(v) * n ^ (-1 / 3)

if (h > 0)
return ceil(Integer, _ptp(v)/h)
end

return 1
end

bin_estimator(estimator::Int, v::AbstractVector{T}) where {T<:Real} = estimator

function bin_estimator(estimator::Symbol, v::AbstractVector{T}) where {T<:Real}
if (estimator == :sturges)
nbins = sturges(length(v))
elseif (estimator == :scott)
nbins = scott(v)
elseif (estimator == :FD || estimator == :fd)
nbins = FD(v)
else
throw(ArgumentError("estimator must be one of :sturges, :scott, FD"))
end
# println("Using $estimator, calculated $nbins")
return nbins
end


abstract type AbstractHistogram{T<:Real,N,E} end

# N-dimensional histogram object
Expand Down Expand Up @@ -271,12 +317,18 @@ append!(h::AbstractHistogram{T,1}, v::AbstractVector, wv::Union{AbstractVector,A

fit(::Type{Histogram{T}},v::AbstractVector, edg::AbstractVector; closed::Symbol=:left) where {T} =
fit(Histogram{T},(v,), (edg,), closed=closed)
fit(::Type{Histogram{T}},v::AbstractVector; closed::Symbol=:left, nbins=sturges(length(v))) where {T} =
fit(Histogram{T},(v,); closed=closed, nbins=nbins)

# fit(::Type{Histogram{T}},v::AbstractVector, bins::Symbol; closed::Symbol=:left) where {T} =
# fit(Histogram{T},(v,), (edg,), closed=closed, nbins=bin_estimator(bins, v))
# fit(::Type{Histogram{T}},v::AbstractVector, bins::Int; closed::Symbol=:left) where {T} =
# fit(Histogram{T},(v,), (edg,), closed=closed, nbins=bins)

fit(::Type{Histogram{T}},v::AbstractVector; closed::Symbol=:left, nbins=:sturges) where {T} =
fit(Histogram{T},(v,); closed=closed, nbins=bin_estimator(nbins, v))
fit(::Type{Histogram{T}},v::AbstractVector, wv::AbstractWeights, edg::AbstractVector; closed::Symbol=:left) where {T} =
fit(Histogram{T},(v,), wv, (edg,), closed=closed)
fit(::Type{Histogram{T}},v::AbstractVector, wv::AbstractWeights; closed::Symbol=:left, nbins=sturges(length(v))) where {T} =
fit(Histogram{T}, (v,), wv; closed=closed, nbins=nbins)
fit(::Type{Histogram{T}},v::AbstractVector, wv::AbstractWeights; closed::Symbol=:left, nbins=:sturges) where {T} =
fit(Histogram{T}, (v,), wv; closed=closed, nbins=bin_estimator(nbins, v))

fit(::Type{Histogram}, v::AbstractVector, wv::AbstractWeights{W}, args...; kwargs...) where {W} = fit(Histogram{W}, v, wv, args...; kwargs...)

Expand Down
12 changes: 12 additions & 0 deletions test/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,16 @@ end
@test StatsBase.midpoints(range(0, stop = 1, length = 5)) == 0.125:0.25:0.875
end

@testset "Histogram autorange" begin

# check we still default to sturges
@test fit(Histogram,[1:100; 500:1000],closed=:right).weights == [100, 0, 0, 0, 1, 100, 100, 100, 100, 100]
@test fit(Histogram,[1:100; 500:1000],nbins=:sturges, closed=:right).weights == [100, 0, 0, 0, 1, 100, 100, 100, 100, 100]

@test fit(Histogram,[1:100; 500:1000],nbins=:scott,closed=:right).weights == [100, 0, 101, 200, 200]
@test fit(Histogram,[1:100; 500:1000],nbins=:fd,closed=:right).weights == [100, 0, 0, 0, 1, 100, 100, 100, 100, 100]

end


end # @testset "StatsBase.Histogram"