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

Weights for Univariates #46

Merged
merged 5 commits into from
Sep 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/AverageShiftedHistograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module AverageShiftedHistograms

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


Expand Down
46 changes: 42 additions & 4 deletions src/univariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Base.show(io::IO, ::MIME"text/plain", o::Ash)
end

Base.push!(o::Ash, y::Real) = _histogram!(o::Ash, [y])
Base.push!(o::Ash, y::Real, weight::Real) = _weightedhistogram!(o::Ash, [y], [weight])

# add data to the histogram
function _histogram!(o::Ash, y)
Expand All @@ -40,6 +41,23 @@ function _histogram!(o::Ash, y)
return
end

# add data to the weighted histogram
function _weightedhistogram!(o::Ash, y, weight::AbstractWeights)
b = length(o.rng)
a = first(o.rng)
b == length(weight) || throw(DimensionMismatch("Length of weights should be same as the length of the range"))
δinv = inv(step(o.rng))
c = o.counts
map(y) do x
ki = floor(Int, (x - a) * δinv + 1.5)
if 1 <= ki <= b
c[ki] += weight[ki]
end
end
o.nobs += length(y)
return
end

# recalculate the ash density
function _ash!(o::Ash)
b = length(o.rng)
Expand All @@ -62,7 +80,13 @@ end
# Univariate Ash
ash(x; kw...)

Fit an average shifted histogram to data `x`. Keyword options are:
Fit an average shifted histogram to data `x`.

ash(x, weight::AbstractWeights; kw...)

Fit a weighted average shifted histogram to data `x`.

Keyword options are:

- `rng` : values over which the density will be estimated
- `m` : Number of adjacent histograms to smooth over
Expand All @@ -80,11 +104,12 @@ Fit a bivariate averaged shifted histogram to data vectors `x` and `y`. Keyword
- `kernely` : kernel in y direction

# Mutating an Ash object
Ash objectes can be updated with new data, smoothing parameter(s), or kernel(s). They cannot, however, change the ranges over which the density is estimated. It is therefore suggested to err on the side of caution when choosing data endpoints.
Ash objectes can be updated with new data, new weights(only for univariates), smoothing parameter(s), or kernel(s). They cannot, however, change the ranges over which the density is estimated. It is therefore suggested to err on the side of caution when choosing data endpoints.

# univariate
ash!(obj; kw...)
ash!(obj, newx, kw...)
ash!(obj, newx; kw...)
ash!(obj, newx, weight::AbstractWeights; kw...)

# bivariate
ash!(obj; kw...)
Expand All @@ -97,12 +122,19 @@ function ash(x; nbin=500, rng::AbstractRange = extendrange(x, nbin), m = ceil(In
_ash!(o)
end

function ash(x, weight::AbstractWeights; nbin=500, rng::AbstractRange = extendrange(x, nbin), m = ceil(Int, length(rng)/100), kernel = Kernels.biweight)
o = Ash(rng, kernel, m)
_weightedhistogram!(o, x, weight)
_ash!(o)
end


"""
ash!(o::Ash; kw...)
ash!(o::Ash, newdata; kw...)
ash!(o::Ash, newdata, weight::AbstractWeights; kw...)

Update an Ash estimate with new data, smoothing parameter (keyword `m`), or kernel (keyword `kernel`):
Update an Ash estimate with new data, new weight, smoothing parameter (keyword `m`), or kernel (keyword `kernel`):
"""
function ash!(o::Ash; m = o.m, kernel = o.kernel)
o.m = m
Expand All @@ -115,6 +147,12 @@ function ash!(o::Ash, y; m = o.m, kernel = o.kernel)
_histogram!(o, y)
_ash!(o)
end
function ash!(o::Ash, y, weight::AbstractWeights; m = o.m, kernel = o.kernel)
o.m = m
o.kernel = kernel
_weightedhistogram!(o, y, weight)
_ash!(o)
end

function Base.merge!(o::Ash, o2::Ash)
o.kernel == o2.kernel || error("Merge failed. Ash objects use different kernels.")
Expand Down
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,24 @@ end
xyz(o)
end

@testset "AshWeighted" begin
weight_funcs = (weights, aweights, fweights, pweights)

for f in weight_funcs
x = randn(10_000)
o = ash(x, f(ones(21)), rng = -1:0.1:1)
o2 = ash(x; rng = -1:0.1:1)
@test o == o2

y = rand(1000)
w = f(rand(1:10, 11))
o = ash(y; rng = 0:0.1:1)
ow = ash(y, w; rng = 0:0.1:1)
@test o.counts .* w == ow.counts

w = f(rand(1:10, 10))
@test_throws DimensionMismatch ash(y, w; rng = 0:0.1:1)
end
end

end #module