@@ -29,7 +29,7 @@ kernel_dist(::Type{D},w::Real) where {D} = (s = w/std(D(0.0,1.0)); D(0.0,s))
2929
3030
3131# Silverman's rule of thumb for KDE bandwidth selection
32- function default_bandwidth (data:: RealVector , alpha:: Float64 = 0.9 )
32+ function default_bandwidth (data:: AbstractVector{<:Real} , alpha:: Float64 = 0.9 )
3333 # Determine length of data
3434 ndata = length (data)
3535 ndata <= 1 && return alpha
@@ -53,7 +53,7 @@ function default_bandwidth(data::RealVector, alpha::Float64 = 0.9)
5353 return alpha * width * ndata^ (- 0.2 )
5454end
5555
56- function default_weights (data:: RealVector )
56+ function default_weights (data:: AbstractVector{<:Real} )
5757 UniformWeights (length (data))
5858end
5959
7272
7373# default kde range
7474# Should extend enough beyond the data range to avoid cyclic correlation from the FFT
75- function kde_boundary (data:: RealVector , bandwidth:: Real )
75+ function kde_boundary (data:: AbstractVector{<:Real} , bandwidth:: Real )
7676 lo, hi = extrema (data)
7777 lo - 4.0 * bandwidth, hi + 4.0 * bandwidth
7878end
@@ -92,11 +92,11 @@ UniformWeights(n) = UniformWeights{n}()
9292Base. sum (x:: UniformWeights ) = 1.
9393Base. getindex (x:: UniformWeights{N} , i) where {N} = 1 / N
9494
95- const Weights = Union{UniformWeights, RealVector , StatsBase. Weights}
95+ const Weights = Union{UniformWeights, AbstractVector{ <: Real } , StatsBase. Weights}
9696
9797
9898# tabulate data for kde
99- function tabulate (data:: RealVector , midpoints:: R , weights:: Weights = default_weights (data)) where R<: AbstractRange
99+ function tabulate (data:: AbstractVector{<:Real} , midpoints:: R , weights:: Weights = default_weights (data)) where R<: AbstractRange
100100 npoints = length (midpoints)
101101 s = step (midpoints)
102102
@@ -147,26 +147,26 @@ function conv(k::UnivariateKDE, dist::UnivariateDistribution)
147147end
148148
149149# main kde interface methods
150- function kde (data:: RealVector , weights:: Weights , midpoints:: R , dist:: UnivariateDistribution ) where R<: AbstractRange
150+ function kde (data:: AbstractVector{<:Real} , weights:: Weights , midpoints:: R , dist:: UnivariateDistribution ) where R<: AbstractRange
151151 k = tabulate (data, midpoints, weights)
152152 conv (k,dist)
153153end
154154
155- function kde (data:: RealVector , dist:: UnivariateDistribution ;
155+ function kde (data:: AbstractVector{<:Real} , dist:: UnivariateDistribution ;
156156 boundary:: Tuple{Real,Real} = kde_boundary (data,std (dist)), npoints:: Int = 2048 , weights= default_weights (data))
157157
158158 midpoints = kde_range (boundary,npoints)
159159 kde (data,weights,midpoints,dist)
160160end
161161
162- function kde (data:: RealVector , midpoints:: R ;
162+ function kde (data:: AbstractVector{<:Real} , midpoints:: R ;
163163 bandwidth= default_bandwidth (data), kernel= Normal, weights= default_weights (data)) where R<: AbstractRange
164164 bandwidth > 0.0 || error (" Bandwidth must be positive" )
165165 dist = kernel_dist (kernel,bandwidth)
166166 kde (data,weights,midpoints,dist)
167167end
168168
169- function kde (data:: RealVector ; bandwidth= default_bandwidth (data), kernel= Normal,
169+ function kde (data:: AbstractVector{<:Real} ; bandwidth= default_bandwidth (data), kernel= Normal,
170170 npoints:: Int = 2048 , boundary:: Tuple{Real,Real} = kde_boundary (data,bandwidth), weights= default_weights (data))
171171 bandwidth > 0.0 || error (" Bandwidth must be positive" )
172172 dist = kernel_dist (kernel,bandwidth)
248248# B. W. Silverman (1986)
249249# sections 3.4.3 (pp. 48-52) and 3.5 (pp. 61-66)
250250
251- function kde_lscv (data:: RealVector , midpoints:: R ;
251+ function kde_lscv (data:: AbstractVector{<:Real} , midpoints:: R ;
252252 kernel= Normal,
253253 bandwidth_range:: Tuple{Real,Real} = (h= default_bandwidth (data); (0.25 * h,1.5 * h)),
254254 weights= default_weights (data)) where R<: AbstractRange
@@ -289,7 +289,7 @@ function kde_lscv(data::RealVector, midpoints::R;
289289 UnivariateKDE (k. x, dens)
290290end
291291
292- function kde_lscv (data:: RealVector ;
292+ function kde_lscv (data:: AbstractVector{<:Real} ;
293293 boundary:: Tuple{Real,Real} = kde_boundary (data,default_bandwidth (data)),
294294 npoints:: Int = 2048 ,
295295 kernel= Normal,
0 commit comments