Skip to content

Commit ce9ca33

Browse files
committed
extend tabulate, wrap with weights=ones(data)
1 parent 48cfa6a commit ce9ca33

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/univariate.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,33 @@ function kde_range(boundary::(@compat Tuple{Real,Real}), npoints::Int)
6767
end
6868

6969
# tabulate data for kde
70-
function tabulate(data::RealVector, midpoints::Range)
71-
ndata = length(data)
70+
function tabulate(data::RealVector, weights::RealVector, midpoints::Range)
7271
npoints = length(midpoints)
7372
s = step(midpoints)
7473

7574
# Set up a grid for discretized data
7675
grid = zeros(Float64, npoints)
77-
ainc = 1.0 / (ndata*s*s)
76+
ainc = 1.0 / (sum(weights)*s*s)
7877

7978
# weighted discretization (cf. Jones and Lotwick)
8079
for x in data
8180
k = searchsortedfirst(midpoints,x)
8281
j = k-1
8382
if 1 <= j <= npoints-1
84-
grid[j] += (midpoints[k]-x)*ainc
85-
grid[k] += (x-midpoints[j])*ainc
83+
grid[j] += (midpoints[k]-x)*ainc*weights[i]
84+
grid[k] += (x-midpoints[j])*ainc*weights[i]
8685
end
8786
end
8887

8988
# returns an un-convolved KDE
9089
UnivariateKDE(midpoints, grid)
9190
end
9291

92+
function tabulate(data::RealVector, midpoints::Range)
93+
weights = ones(data)
94+
tabulate(data, weights, midpoints)
95+
end
96+
9397
# convolve raw KDE with kernel
9498
# TODO: use in-place fft
9599
function conv(k::UnivariateKDE, dist::UnivariateDistribution)

0 commit comments

Comments
 (0)