Skip to content

Commit 903070f

Browse files
committed
finish
1 parent ce9ca33 commit 903070f

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/univariate.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function tabulate(data::RealVector, weights::RealVector, midpoints::Range)
7676
ainc = 1.0 / (sum(weights)*s*s)
7777

7878
# weighted discretization (cf. Jones and Lotwick)
79-
for x in data
79+
for (i,x) in enumerate(data)
8080
k = searchsortedfirst(midpoints,x)
8181
j = k-1
8282
if 1 <= j <= npoints-1
@@ -122,31 +122,36 @@ function conv(k::UnivariateKDE, dist::UnivariateDistribution)
122122
UnivariateKDE(k.x, dens)
123123
end
124124

125+
function uniformweights(data)
126+
n = length(data)
127+
fill(1/n, n)
128+
end
129+
125130
# main kde interface methods
126-
function kde(data::RealVector, midpoints::Range, dist::UnivariateDistribution)
127-
k = tabulate(data, midpoints)
131+
function kde(data::RealVector, weights::RealVector, midpoints::Range, dist::UnivariateDistribution)
132+
k = tabulate(data, weights, midpoints)
128133
conv(k,dist)
129134
end
130135

131136
function kde(data::RealVector, dist::UnivariateDistribution;
132-
boundary::(@compat Tuple{Real,Real})=kde_boundary(data,std(dist)), npoints::Int=2048)
137+
boundary::(@compat Tuple{Real,Real})=kde_boundary(data,std(dist)), npoints::Int=2048, weights=uniformweights(data))
133138

134139
midpoints = kde_range(boundary,npoints)
135-
kde(data,midpoints,dist)
140+
kde(data,weights,midpoints,dist)
136141
end
137142

138143
function kde(data::RealVector, midpoints::Range;
139-
bandwidth=default_bandwidth(data), kernel=Normal)
144+
bandwidth=default_bandwidth(data), kernel=Normal, weights=uniformweights(data))
140145
bandwidth > 0.0 || error("Bandwidth must be positive")
141146
dist = kernel_dist(kernel,bandwidth)
142-
kde(data,midpoints,dist)
147+
kde(data,weights,midpoints,dist)
143148
end
144149

145150
function kde(data::RealVector; bandwidth=default_bandwidth(data), kernel=Normal,
146-
npoints::Int=2048, boundary::(@compat Tuple{Real,Real})=kde_boundary(data,bandwidth))
151+
npoints::Int=2048, boundary::(@compat Tuple{Real,Real})=kde_boundary(data,bandwidth), weights=uniformweights(data))
147152
bandwidth > 0.0 || error("Bandwidth must be positive")
148153
dist = kernel_dist(kernel,bandwidth)
149-
kde(data,dist;boundary=boundary,npoints=npoints)
154+
kde(data,dist;boundary=boundary,npoints=npoints,weights=weights)
150155
end
151156

152157
# Select bandwidth using least-squares cross validation, from:

0 commit comments

Comments
 (0)