Skip to content

Commit

Permalink
updates for 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Aug 7, 2018
1 parent a3a81e0 commit aeaa3c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 38 deletions.
5 changes: 3 additions & 2 deletions src/AverageShiftedHistograms.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module AverageShiftedHistograms

import UnicodePlots, Distributions, RecipesBase
import UnicodePlots, RecipesBase
using LinearAlgebra, Statistics
import StatsBase: nobs, fweights
export ash, ash!, extendrange, xy, xyz, nout, nobs, Kernels

Expand All @@ -15,7 +16,7 @@ Create a `LinSpace` of length `n` starting at `s` standard deviations below
"""
function extendrange(y::AbstractVector, s = 0.5, n = 500)
σ = std(y)
linspace(minimum(y) - s * σ, maximum(y) + s * σ, n)
range(minimum(y) - s * σ, stop = maximum(y) + s * σ, length = n)
end

include("kernels.jl")
Expand Down
28 changes: 14 additions & 14 deletions src/bivariate.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mutable struct Ash2{R1 <: Range, R2 <: Range, F1 <: Function, F2 <: Function}
mutable struct Ash2{R1 <: AbstractRange, R2 <: AbstractRange, F1 <: Function, F2 <: Function}
kernelx::F1
kernely::F2
rngx::R1 # rng for x
Expand All @@ -10,7 +10,7 @@ mutable struct Ash2{R1 <: Range, R2 <: Range, F1 <: Function, F2 <: Function}
nobs::Int64

function Ash2(kx::F1, ky::F2, rngx::R1, rngy::R2, mx::Int, my::Int) where
{F1 <: Function, F2 <: Function, R1 <: Range, R2 <: Range}
{F1 <: Function, F2 <: Function, R1 <: AbstractRange, R2 <: AbstractRange}

v = zeros(Int, length(rngy), length(rngx))
z = zeros(Float64, length(rngy), length(rngx))
Expand All @@ -19,12 +19,12 @@ mutable struct Ash2{R1 <: Range, R2 <: Range, F1 <: Function, F2 <: Function}
end
function Base.show(io::IO, o::Ash2)
println(io, "Ash2")
f, l, s = round.((first(o.rngx), last(o.rngx), step(o.rngx)), 4)
f, l, s = round.((first(o.rngx), last(o.rngx), step(o.rngx)), digits=4)
print(io, "X:")
println(io, " > edges | $f : $s : $l")
println(io, " > kernel | $(o.kernelx)")
println(io, " > m | $(o.mx)")
f, l, s = round.((first(o.rngy), last(o.rngy), step(o.rngy)), 4)
f, l, s = round.((first(o.rngy), last(o.rngy), step(o.rngy)), digits=4)
print(io, "Y:")
println(io, " > edges | $f : $s : $l")
println(io, " > kernel | $(o.kernely)")
Expand All @@ -33,8 +33,8 @@ function Base.show(io::IO, o::Ash2)
end

function Base.:(==)(o::Ash2, o2::Ash2)
fns = fieldnames(o)
all(getfield.(o, fns) .== getfield.(o2, fns))
fns = fieldnames(typeof(o))
all(getfield.(Ref(o), fns) .== getfield.(Ref(o2), fns))
end

function _histogram!(o::Ash2, x::AbstractVector, y::AbstractVector)
Expand Down Expand Up @@ -87,7 +87,7 @@ end


function ash(x::AbstractVector, y::AbstractVector;
rngx::Range = extendrange(x), rngy::Range = extendrange(y),
rngx::AbstractRange = extendrange(x), rngy::AbstractRange = extendrange(y),
mx = 5, my = 5, kernelx = Kernels.biweight, kernely = Kernels.biweight)
o = Ash2(kernelx, kernely, rngx, rngy, mx, my)
_histogram!(o, x, y)
Expand All @@ -111,16 +111,16 @@ end
xyz(o::Ash2) = (o.rngx, o.rngy, copy(o.z))
nobs(o::Ash2) = o.nobs
nout(o::Ash2) = nobs(o) - sum(o.v)
function Base.mean(o::Ash2)
meanx = mean(o.rngx, fweights(vec(sum(o.v, 1))))
meany = mean(o.rngy, fweights(vec(sum(o.v, 2))))
function Statistics.mean(o::Ash2)
meanx = mean(o.rngx, fweights(vec(sum(o.v, dims=1))))
meany = mean(o.rngy, fweights(vec(sum(o.v, dims=2))))
[meanx; meany]
end
function Base.var(o::Ash2)
varx = var(o.rngx, fweights(vec(sum(o.v, 1))); corrected=true)
vary = var(o.rngy, fweights(vec(sum(o.v, 2))); corrected=true)
function Statistics.var(o::Ash2)
varx = var(o.rngx, fweights(vec(sum(o.v, dims=1))); corrected=true)
vary = var(o.rngy, fweights(vec(sum(o.v, dims=2))); corrected=true)
[varx; vary]
end
Base.std(o::Ash2) = sqrt.(var(o))
Statistics.std(o::Ash2) = sqrt.(var(o))

RecipesBase.@recipe f(o::Ash2) = o.rngx, o.rngy, o.z
22 changes: 11 additions & 11 deletions src/univariate.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
mutable struct Ash{R <: Range, F <: Function}
mutable struct Ash{R <: AbstractRange, F <: Function}
density::Vector{Float64} # ash estimate
rng::R # range of x values
counts::Vector{Int} # histogram estimate
kernel::F # kernel function
m::Int # smoothing parameter
nobs::Int # number of observations
function Ash(rng::R, kernel::F, m::Int) where {R<:Range, F<:Function}
function Ash(rng::R, kernel::F, m::Int) where {R<:AbstractRange, F<:Function}
m > 0 || throw(ArgumentError("Smoothing parameter must be > 0"))
new{R, F}(zeros(Float64, length(rng)), rng, zeros(Int, length(rng)), kernel, m, 0)
end
end
function Base.show(io::IO, o::Ash)
println(io, "Ash")
f, l, s = round.((first(o.rng), last(o.rng), step(o.rng)), 4)
f, l, s = round.((first(o.rng), last(o.rng), step(o.rng)), digits=4)
println(io, " > edges | $f : $s : $l")
println(io, " > kernel | $(o.kernel)")
println(io, " > m | $(o.m)")
println(io, " > nobs | $(o.nobs)")
x, y = xy(o)
inds = find(y)
inds = findall(x -> x != 0, y)
print(io, UnicodePlots.lineplot(x[inds], y[inds]; grid = false))
end

Expand Down Expand Up @@ -91,7 +91,7 @@ Ash objectes can be updated with new data, smoothing parameter(s), or kernel(s).
ash!(obj, newx, newy; kw...)
"""
function ash(x::AbstractArray; rng::Range = extendrange(x), m = ceil(Int, length(rng)/100), kernel = Kernels.biweight)
function ash(x::AbstractArray; rng::AbstractRange = extendrange(x), m = ceil(Int, length(rng)/100), kernel = Kernels.biweight)
o = Ash(rng, kernel, m)
_histogram!(o, x)
_ash!(o)
Expand Down Expand Up @@ -128,8 +128,8 @@ end
Base.merge(o::Ash, o2::Ash) = merge!(copy(o), o2)
Base.copy(o::Ash) = deepcopy(o)
function Base.:(==)(o::Ash, o2::Ash)
fns = fieldnames(o)
all(getfield.(o, fns) .== getfield.(o2, fns))
fns = fieldnames(typeof(o))
all(getfield.(Ref(o), fns) .== getfield.(Ref(o2), fns))
end

"return the range and density of a univariate ASH"
Expand All @@ -150,8 +150,8 @@ Statistics.quantile(o::Ash, p = [0, .25, .5, .75, 1]) = quantile(o.rng, fweights
Statistics.std(o::Ash) = sqrt(var(o))

function Base.extrema(o::Ash)
imin = findfirst(x -> x>0, o.counts)
imax = findlast(x -> x>0, o.counts)
imin = findnext(x -> x > 0, o.counts, 1)
imax = findlast(x -> x > 0, o.counts)
o.rng[imin], o.rng[imax]
end

Expand All @@ -160,7 +160,7 @@ end
Return the estimated density at the point `x`.
"""
function Distributions.pdf(o::Ash, x::Real)
function pdf(o::Ash, x::Real)
rng = o.rng
y = o.density
i = searchsortedlast(rng, x)
Expand All @@ -176,7 +176,7 @@ end
Return the estimated cumulative density at the point `x`.
"""
function Distributions.cdf(o::Ash, x::Real)
function cdf(o::Ash, x::Real)
cdf = cumsum(o.density) * step(o.rng)
i = searchsortedlast(o.rng, x)
if i == 0
Expand Down
22 changes: 11 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Tests
using AverageShiftedHistograms, StatsBase, Base.Test, Distributions
using AverageShiftedHistograms, Test, StatsBase, Statistics, LinearAlgebra

info("Messy Output")
@info("Messy Output")
show(ash(randn(1000)))
show(ash(randn(100), randn(100)))
println("\n\n")
info("Begin Tests")
@info("Begin Tests")


@testset "Kernels" begin
Expand Down Expand Up @@ -33,20 +33,20 @@ end
@test f(o) f(x) atol=.1
end
AverageShiftedHistograms.histdensity(o)
@test quantile(o, 0) o.rng[findfirst(o.counts)]
@test quantile(o, 0) o.rng[findnext(x -> x != 0, o.counts, 1)]

# check that histogram is correct
h = fit(Histogram, x, (-4:.1:4.1)-.05; closed = :left)
h = fit(Histogram, x, (-4:.1:4.1) .- .05; closed = :left)
@test h.weights == o.counts

ash!(o, x)
@test nobs(o) == 20_000
@test pdf(o, -5) == 0
@test pdf(o, 5) == 0
@test pdf(o, 0) > 0
@test cdf(o, -5) == 0
@test cdf(o, 0) .5 atol=.05
@test cdf(o, 5) 1
@test AverageShiftedHistograms.pdf(o, -5) == 0
@test AverageShiftedHistograms.pdf(o, 5) == 0
@test AverageShiftedHistograms.pdf(o, 0) > 0
@test AverageShiftedHistograms.cdf(o, -5) == 0
@test AverageShiftedHistograms.cdf(o, 0) .5 atol=.05
@test AverageShiftedHistograms.cdf(o, 5) 1

o = ash([.1, .1]; rng = -1:.1:1)
@test nout(o) == 0
Expand Down

0 comments on commit aeaa3c0

Please sign in to comment.