Skip to content

Commit fd3a44a

Browse files
committed
add bivariate weights
1 parent 0af7754 commit fd3a44a

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/bivariate.jl

Lines changed: 25 additions & 15 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}), midpoints::(@compat Tuple{Range, Range}))
27+
function tabulate(data::(@compat Tuple{RealVector, RealVector}), weights::Weights, midpoints::(@compat Tuple{Range, Range}))
2828
xdata, ydata = data
2929
ndata = length(xdata)
3030
length(ydata) == ndata || error("data vectors must be of same length")
@@ -35,24 +35,30 @@ function tabulate(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@co
3535

3636
# Set up a grid for discretized data
3737
grid = zeros(Float64, nx, ny)
38-
ainc = 1.0 / (ndata*(sx*sy)^2)
38+
ainc = 1.0 / (sum(weights)*(sx*sy)^2)
3939

4040
# weighted discretization (cf. Jones and Lotwick)
41-
for (x, y) in zip(xdata,ydata)
41+
for i in 1:length(xdata)
42+
x = xdata[i]
43+
y = ydata[i]
4244
kx, ky = searchsortedfirst(xmid,x), searchsortedfirst(ymid,y)
4345
jx, jy = kx-1, ky-1
4446
if 1 <= jx <= nx-1 && 1 <= jy <= ny-1
45-
grid[jx,jy] += (xmid[kx]-x)*(ymid[ky]-y)*ainc
46-
grid[kx,jy] += (x-xmid[jx])*(ymid[ky]-y)*ainc
47-
grid[jx,ky] += (xmid[kx]-x)*(y-ymid[jy])*ainc
48-
grid[kx,ky] += (x-xmid[jx])*(y-ymid[jy])*ainc
47+
grid[jx,jy] += (xmid[kx]-x)*(ymid[ky]-y)*ainc*weights[i]
48+
grid[kx,jy] += (x-xmid[jx])*(ymid[ky]-y)*ainc*weights[i]
49+
grid[jx,ky] += (xmid[kx]-x)*(y-ymid[jy])*ainc*weights[i]
50+
grid[kx,ky] += (x-xmid[jx])*(y-ymid[jy])*ainc*weights[i]
4951
end
5052
end
5153

5254
# returns an un-convolved KDE
5355
BivariateKDE(xmid, ymid, grid)
5456
end
5557

58+
function tabulate(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@compat Tuple{Range, Range}))
59+
tabulate(data, default_weights(data), midpoints)
60+
end
61+
5662
# convolution with product distribution of two univariates distributions
5763
function conv(k::BivariateKDE, dist::(@compat Tuple{UnivariateDistribution,UnivariateDistribution}) )
5864
# Transform to Fourier basis
@@ -81,41 +87,45 @@ end
8187

8288
typealias BivariateDistribution @compat(Union{MultivariateDistribution,Tuple{UnivariateDistribution,UnivariateDistribution}})
8389

84-
function kde(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@compat Tuple{Range, Range}), dist::BivariateDistribution)
85-
k = tabulate(data,midpoints)
90+
default_weights(data::(@compat Tuple{RealVector, RealVector})) = UniformWeights(length(data[1]))
91+
92+
function kde(data::(@compat Tuple{RealVector, RealVector}), weights::Weights, midpoints::(@compat Tuple{Range, Range}), dist::BivariateDistribution)
93+
k = tabulate(data, weights, midpoints)
8694
conv(k,dist)
8795
end
8896

8997
function kde(data::(@compat Tuple{RealVector, RealVector}), dist::BivariateDistribution;
9098
boundary::(@compat Tuple{(@compat Tuple{Real,Real}),(@compat Tuple{Real,Real})}) = (kde_boundary(data[1],std(dist[1])),
9199
kde_boundary(data[2],std(dist[2]))),
92-
npoints::(@compat Tuple{Int,Int})=(256,256))
100+
npoints::(@compat Tuple{Int,Int})=(256,256),
101+
weights::Weights = default_weights(data))
93102

94103
xmid = kde_range(boundary[1],npoints[1])
95104
ymid = kde_range(boundary[2],npoints[2])
96105

97-
kde(data,(xmid,ymid),dist)
106+
kde(data,weights,(xmid,ymid),dist)
98107
end
99108

100109
function kde(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@compat Tuple{Range, Range});
101-
bandwidth=default_bandwidth(data), kernel=Normal)
110+
bandwidth=default_bandwidth(data), kernel=Normal, weights::Weights = default_weights(data))
102111

103112
dist = kernel_dist(kernel,bandwidth)
104-
kde(data,midpoints,dist)
113+
kde(data,weights,midpoints,dist)
105114
end
106115

107116
function kde(data::(@compat Tuple{RealVector, RealVector});
108117
bandwidth=default_bandwidth(data),
109118
kernel=Normal,
110119
boundary::(@compat Tuple{(@compat Tuple{Real,Real}),(@compat Tuple{Real,Real})}) = (kde_boundary(data[1],bandwidth[1]),
111120
kde_boundary(data[2],bandwidth[2])),
112-
npoints::(@compat Tuple{Int,Int})=(256,256))
121+
npoints::(@compat Tuple{Int,Int})=(256,256),
122+
weights::Weights = default_weights(data))
113123

114124
dist = kernel_dist(kernel,bandwidth)
115125
xmid = kde_range(boundary[1],npoints[1])
116126
ymid = kde_range(boundary[2],npoints[2])
117127

118-
kde(data,(xmid,ymid),dist)
128+
kde(data,weights,(xmid,ymid),dist)
119129
end
120130

121131
# matrix data

0 commit comments

Comments
 (0)