Skip to content

Commit

Permalink
Merge pull request #114 from chriselrod/nostatsbasealiases
Browse files Browse the repository at this point in the history
Remove use of StatsBase aliases
  • Loading branch information
andreasnoack authored Apr 26, 2023
2 parents 785a5c0 + ffbb170 commit 176a6fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KernelDensity"
uuid = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b"
authors = ["Simon Byrne and various contributors"]
version = "0.6.5"
version = "0.6.6"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
1 change: 0 additions & 1 deletion src/KernelDensity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ using StatsBase
using Distributions
using Interpolations

import StatsBase: RealVector, RealMatrix
import Distributions: twoπ, pdf
import FFTW: rfft, irfft

Expand Down
16 changes: 8 additions & 8 deletions src/bivariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ function kernel_dist(d::Tuple{DataTypeOrUnionAll, DataTypeOrUnionAll}, w::Tuple{
end

# TODO: there are probably better choices.
function default_bandwidth(data::Tuple{RealVector,RealVector})
function default_bandwidth(data::Tuple{AbstractVector{<:Real},AbstractVector{<:Real}})
default_bandwidth(data[1]), default_bandwidth(data[2])
end

# tabulate data for kde
function tabulate(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Rx, Ry},
function tabulate(data::Tuple{AbstractVector{<:Real}, AbstractVector{<:Real}}, midpoints::Tuple{Rx, Ry},
weights::Weights = default_weights(data)) where {Rx<:AbstractRange,Ry<:AbstractRange}
xdata, ydata = data
ndata = length(xdata)
Expand Down Expand Up @@ -103,15 +103,15 @@ end

const BivariateDistribution = Union{MultivariateDistribution,Tuple{UnivariateDistribution,UnivariateDistribution}}

default_weights(data::Tuple{RealVector, RealVector}) = UniformWeights(length(data[1]))
default_weights(data::Tuple{AbstractVector{<:Real}, AbstractVector{<:Real}}) = UniformWeights(length(data[1]))

function kde(data::Tuple{RealVector, RealVector}, weights::Weights, midpoints::Tuple{Rx, Ry},
function kde(data::Tuple{AbstractVector{<:Real}, AbstractVector{<:Real}}, weights::Weights, midpoints::Tuple{Rx, Ry},
dist::BivariateDistribution) where {Rx<:AbstractRange,Ry<:AbstractRange}
k = tabulate(data, midpoints, weights)
conv(k,dist)
end

function kde(data::Tuple{RealVector, RealVector}, dist::BivariateDistribution;
function kde(data::Tuple{AbstractVector{<:Real}, AbstractVector{<:Real}}, dist::BivariateDistribution;
boundary::Tuple{Tuple{Real,Real}, Tuple{Real,Real}} = (kde_boundary(data[1],std(dist[1])),
kde_boundary(data[2],std(dist[2]))),
npoints::Tuple{Int,Int}=(256,256),
Expand All @@ -123,15 +123,15 @@ function kde(data::Tuple{RealVector, RealVector}, dist::BivariateDistribution;
kde(data,weights,(xmid,ymid),dist)
end

function kde(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Rx, Ry};
function kde(data::Tuple{AbstractVector{<:Real}, AbstractVector{<:Real}}, midpoints::Tuple{Rx, Ry};
bandwidth=default_bandwidth(data), kernel=Normal,
weights::Weights = default_weights(data)) where {Rx<:AbstractRange,Ry<:AbstractRange}

dist = kernel_dist(kernel,bandwidth)
kde(data,weights,midpoints,dist)
end

function kde(data::Tuple{RealVector, RealVector};
function kde(data::Tuple{AbstractVector{<:Real}, AbstractVector{<:Real}};
bandwidth=default_bandwidth(data),
kernel=Normal,
boundary::Tuple{Tuple{Real,Real}, Tuple{Real,Real}} = (kde_boundary(data[1],bandwidth[1]),
Expand All @@ -147,7 +147,7 @@ function kde(data::Tuple{RealVector, RealVector};
end

# matrix data
function kde(data::RealMatrix,args...;kwargs...)
function kde(data::AbstractMatrix{<:Real},args...;kwargs...)
size(data,2) == 2 || error("Can only construct KDE from matrices with 2 columns.")
kde((data[:,1],data[:,2]),args...;kwargs...)
end
22 changes: 11 additions & 11 deletions src/univariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kernel_dist(::Type{D},w::Real) where {D} = (s = w/std(D(0.0,1.0)); D(0.0,s))


# Silverman's rule of thumb for KDE bandwidth selection
function default_bandwidth(data::RealVector, alpha::Float64 = 0.9)
function default_bandwidth(data::AbstractVector{<:Real}, alpha::Float64 = 0.9)
# Determine length of data
ndata = length(data)
ndata <= 1 && return alpha
Expand All @@ -53,7 +53,7 @@ function default_bandwidth(data::RealVector, alpha::Float64 = 0.9)
return alpha * width * ndata^(-0.2)
end

function default_weights(data::RealVector)
function default_weights(data::AbstractVector{<:Real})
UniformWeights(length(data))
end

Expand All @@ -72,7 +72,7 @@ end

# default kde range
# Should extend enough beyond the data range to avoid cyclic correlation from the FFT
function kde_boundary(data::RealVector, bandwidth::Real)
function kde_boundary(data::AbstractVector{<:Real}, bandwidth::Real)
lo, hi = extrema(data)
lo - 4.0*bandwidth, hi + 4.0*bandwidth
end
Expand All @@ -92,11 +92,11 @@ UniformWeights(n) = UniformWeights{n}()
Base.sum(x::UniformWeights) = 1.
Base.getindex(x::UniformWeights{N}, i) where {N} = 1/N

const Weights = Union{UniformWeights, RealVector, StatsBase.Weights}
const Weights = Union{UniformWeights, AbstractVector{<:Real}, StatsBase.Weights}


# tabulate data for kde
function tabulate(data::RealVector, midpoints::R, weights::Weights=default_weights(data)) where R<:AbstractRange
function tabulate(data::AbstractVector{<:Real}, midpoints::R, weights::Weights=default_weights(data)) where R<:AbstractRange
npoints = length(midpoints)
s = step(midpoints)

Expand Down Expand Up @@ -147,26 +147,26 @@ function conv(k::UnivariateKDE, dist::UnivariateDistribution)
end

# main kde interface methods
function kde(data::RealVector, weights::Weights, midpoints::R, dist::UnivariateDistribution) where R<:AbstractRange
function kde(data::AbstractVector{<:Real}, weights::Weights, midpoints::R, dist::UnivariateDistribution) where R<:AbstractRange
k = tabulate(data, midpoints, weights)
conv(k,dist)
end

function kde(data::RealVector, dist::UnivariateDistribution;
function kde(data::AbstractVector{<:Real}, dist::UnivariateDistribution;
boundary::Tuple{Real,Real}=kde_boundary(data,std(dist)), npoints::Int=2048, weights=default_weights(data))

midpoints = kde_range(boundary,npoints)
kde(data,weights,midpoints,dist)
end

function kde(data::RealVector, midpoints::R;
function kde(data::AbstractVector{<:Real}, midpoints::R;
bandwidth=default_bandwidth(data), kernel=Normal, weights=default_weights(data)) where R<:AbstractRange
bandwidth > 0.0 || error("Bandwidth must be positive")
dist = kernel_dist(kernel,bandwidth)
kde(data,weights,midpoints,dist)
end

function kde(data::RealVector; bandwidth=default_bandwidth(data), kernel=Normal,
function kde(data::AbstractVector{<:Real}; bandwidth=default_bandwidth(data), kernel=Normal,
npoints::Int=2048, boundary::Tuple{Real,Real}=kde_boundary(data,bandwidth), weights=default_weights(data))
bandwidth > 0.0 || error("Bandwidth must be positive")
dist = kernel_dist(kernel,bandwidth)
Expand Down Expand Up @@ -248,7 +248,7 @@ end
# B. W. Silverman (1986)
# sections 3.4.3 (pp. 48-52) and 3.5 (pp. 61-66)

function kde_lscv(data::RealVector, midpoints::R;
function kde_lscv(data::AbstractVector{<:Real}, midpoints::R;
kernel=Normal,
bandwidth_range::Tuple{Real,Real}=(h=default_bandwidth(data); (0.25*h,1.5*h)),
weights=default_weights(data)) where R<:AbstractRange
Expand Down Expand Up @@ -289,7 +289,7 @@ function kde_lscv(data::RealVector, midpoints::R;
UnivariateKDE(k.x, dens)
end

function kde_lscv(data::RealVector;
function kde_lscv(data::AbstractVector{<:Real};
boundary::Tuple{Real,Real}=kde_boundary(data,default_bandwidth(data)),
npoints::Int=2048,
kernel=Normal,
Expand Down

2 comments on commit 176a6fe

@andreasnoack
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82388

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.6 -m "<description of version>" 176a6fee4cf8481cc9fad9366116b6f1ce9a4ffc
git push origin v0.6.6

Please sign in to comment.