Skip to content

Commit

Permalink
allow Extrema to handle non-Numbers (e.g. Dates)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Jan 10, 2019
1 parent fff48ef commit e32e0e2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/stats/histograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ Ref: [http://www.jmlr.org/papers/volume11/ben-haim10a/ben-haim10a.pdf](http://ww
using Plots
plot(o)
"""
struct KHist{T} <: HistogramStat{Number}
struct KHist{T, E<:Extrema{T}} <: HistogramStat{Number}
bins::Vector{KHistBin{T}}
b::Int
ex::Extrema{T}
ex::E
end
KHist(b::Int, T::Type = Float64) = KHist(KHistBin{T}[], b, Extrema(T))

Expand Down
5 changes: 3 additions & 2 deletions src/stats/stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,13 @@ Maximum and minimum.
maximum(o)
minimum(o)
"""
mutable struct Extrema{T} <: OnlineStat{Number}
mutable struct Extrema{T,S} <: OnlineStat{S}
min::T
max::T
n::Int
end
Extrema(T::Type{<:Number} = Float64) = Extrema{T}(typemax.(T), typemin.(T), 0)
Extrema(T::Type{<:Number} = Float64) = Extrema{T,Number}(typemax.(T), typemin.(T), 0)
Extrema(T::Type) = Extrema{T,T}(typemax.(T), typemin.(T), 0)
Extrema(initmin::T, initmax::T) where {T} = Extrema{T}(initmin, initmax, 0)
function _fit!(o::Extrema, y)
o.min = min(o.min, y)
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ end
@test value(fit!(Extrema(Int), z)) == extrema(z)

@test ==(mergevals(Extrema(), y, y2)...)

o = fit!(Extrema(Date), Date(2010):Day(1):Date(2011))
@test minimum(o) == Date(2010)
@test maximum(o) == Date(2011)
end
#-----------------------------------------------------------------------# Fit[Dist]
@testset "Fit[Dist]" begin
Expand Down

0 comments on commit e32e0e2

Please sign in to comment.