Skip to content

Commit af24d40

Browse files
committed
change tabulate() signature; use default_weights()
1 parent fd3a44a commit af24d40

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/bivariate.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function default_bandwidth(data::(@compat Tuple{RealVector,RealVector}))
2424
end
2525

2626
# tabulate data for kde
27-
function tabulate(data::(@compat Tuple{RealVector, RealVector}), weights::Weights, midpoints::(@compat Tuple{Range, Range}))
27+
function tabulate(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@compat Tuple{Range, Range}), weights::Weights = default_weights(data))
2828
xdata, ydata = data
2929
ndata = length(xdata)
3030
length(ydata) == ndata || error("data vectors must be of same length")
@@ -55,10 +55,6 @@ function tabulate(data::(@compat Tuple{RealVector, RealVector}), weights::Weight
5555
BivariateKDE(xmid, ymid, grid)
5656
end
5757

58-
function tabulate(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@compat Tuple{Range, Range}))
59-
tabulate(data, default_weights(data), midpoints)
60-
end
61-
6258
# convolution with product distribution of two univariates distributions
6359
function conv(k::BivariateKDE, dist::(@compat Tuple{UnivariateDistribution,UnivariateDistribution}) )
6460
# Transform to Fourier basis
@@ -90,7 +86,7 @@ typealias BivariateDistribution @compat(Union{MultivariateDistribution,Tuple{Uni
9086
default_weights(data::(@compat Tuple{RealVector, RealVector})) = UniformWeights(length(data[1]))
9187

9288
function kde(data::(@compat Tuple{RealVector, RealVector}), weights::Weights, midpoints::(@compat Tuple{Range, Range}), dist::BivariateDistribution)
93-
k = tabulate(data, weights, midpoints)
89+
k = tabulate(data, midpoints, weights)
9490
conv(k,dist)
9591
end
9692

src/univariate.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function default_bandwidth(data::RealVector, alpha::Float64 = 0.9)
3737
return alpha * width * ndata^(-0.2)
3838
end
3939

40+
function default_weights(data::RealVector)
41+
UniformWeights(length(data))
42+
end
43+
4044

4145
# Roughly based on:
4246
# B. W. Silverman (1982) "Algorithm AS 176: Kernel Density Estimation Using
@@ -79,7 +83,7 @@ typealias Weights Union{UniformWeights, RealVector, WeightVec}
7983

8084

8185
# tabulate data for kde
82-
function tabulate(data::RealVector, weights::Weights, midpoints::Range)
86+
function tabulate(data::RealVector, midpoints::Range, weights::Weights=default_weights(data))
8387
npoints = length(midpoints)
8488
s = step(midpoints)
8589

@@ -101,10 +105,6 @@ function tabulate(data::RealVector, weights::Weights, midpoints::Range)
101105
UnivariateKDE(midpoints, grid)
102106
end
103107

104-
function tabulate(data::RealVector, midpoints::Range)
105-
tabulate(data, UniformWeights(length(data)), midpoints)
106-
end
107-
108108
# convolve raw KDE with kernel
109109
# TODO: use in-place fft
110110
function conv(k::UnivariateKDE, dist::UnivariateDistribution)
@@ -135,26 +135,26 @@ end
135135

136136
# main kde interface methods
137137
function kde(data::RealVector, weights::Weights, midpoints::Range, dist::UnivariateDistribution)
138-
k = tabulate(data, weights, midpoints)
138+
k = tabulate(data, midpoints, weights)
139139
conv(k,dist)
140140
end
141141

142142
function kde(data::RealVector, dist::UnivariateDistribution;
143-
boundary::(@compat Tuple{Real,Real})=kde_boundary(data,std(dist)), npoints::Int=2048, weights=UniformWeights(length(data)))
143+
boundary::(@compat Tuple{Real,Real})=kde_boundary(data,std(dist)), npoints::Int=2048, weights=default_weights(data))
144144

145145
midpoints = kde_range(boundary,npoints)
146146
kde(data,weights,midpoints,dist)
147147
end
148148

149149
function kde(data::RealVector, midpoints::Range;
150-
bandwidth=default_bandwidth(data), kernel=Normal, weights=UniformWeights(length(data)))
150+
bandwidth=default_bandwidth(data), kernel=Normal, weights=default_weights(data))
151151
bandwidth > 0.0 || error("Bandwidth must be positive")
152152
dist = kernel_dist(kernel,bandwidth)
153153
kde(data,weights,midpoints,dist)
154154
end
155155

156156
function kde(data::RealVector; bandwidth=default_bandwidth(data), kernel=Normal,
157-
npoints::Int=2048, boundary::(@compat Tuple{Real,Real})=kde_boundary(data,bandwidth), weights=UniformWeights(length(data)))
157+
npoints::Int=2048, boundary::(@compat Tuple{Real,Real})=kde_boundary(data,bandwidth), weights=default_weights(data))
158158
bandwidth > 0.0 || error("Bandwidth must be positive")
159159
dist = kernel_dist(kernel,bandwidth)
160160
kde(data,dist;boundary=boundary,npoints=npoints,weights=weights)
@@ -167,10 +167,11 @@ end
167167

168168
function kde_lscv(data::RealVector, midpoints::Range;
169169
kernel=Normal,
170-
bandwidth_range::(@compat Tuple{Real,Real})=(h=default_bandwidth(data); (0.25*h,1.5*h)))
170+
bandwidth_range::(@compat Tuple{Real,Real})=(h=default_bandwidth(data); (0.25*h,1.5*h)),
171+
weights=default_weights(data))
171172

172173
ndata = length(data)
173-
k = tabulate(data, midpoints)
174+
k = tabulate(data, midpoints, weights)
174175

175176
# the ft here is K/ba*sqrt(2pi) * u(s), it is K times the Yl in Silverman's book
176177
K = length(k.density)
@@ -209,8 +210,9 @@ function kde_lscv(data::RealVector;
209210
boundary::(@compat Tuple{Real,Real})=kde_boundary(data,default_bandwidth(data)),
210211
npoints::Int=2048,
211212
kernel=Normal,
212-
bandwidth_range::(@compat Tuple{Real,Real})=(h=default_bandwidth(data); (0.25*h,1.5*h)))
213+
bandwidth_range::(@compat Tuple{Real,Real})=(h=default_bandwidth(data); (0.25*h,1.5*h)),
214+
weights::Weights = default_weights(data))
213215

214216
midpoints = kde_range(boundary,npoints)
215-
kde_lscv(data,midpoints; kernel=kernel, bandwidth_range=bandwidth_range)
217+
kde_lscv(data,midpoints; kernel=kernel, bandwidth_range=bandwidth_range, weights=weights)
216218
end

0 commit comments

Comments
 (0)