@@ -24,7 +24,7 @@ function default_bandwidth(data::(@compat Tuple{RealVector,RealVector}))
24
24
end
25
25
26
26
# 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}) )
28
28
xdata, ydata = data
29
29
ndata = length (xdata)
30
30
length (ydata) == ndata || error (" data vectors must be of same length" )
@@ -35,24 +35,30 @@ function tabulate(data::(@compat Tuple{RealVector, RealVector}), midpoints::(@co
35
35
36
36
# Set up a grid for discretized data
37
37
grid = zeros (Float64, nx, ny)
38
- ainc = 1.0 / (ndata * (sx* sy)^ 2 )
38
+ ainc = 1.0 / (sum (weights) * (sx* sy)^ 2 )
39
39
40
40
# 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]
42
44
kx, ky = searchsortedfirst (xmid,x), searchsortedfirst (ymid,y)
43
45
jx, jy = kx- 1 , ky- 1
44
46
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]
49
51
end
50
52
end
51
53
52
54
# returns an un-convolved KDE
53
55
BivariateKDE (xmid, ymid, grid)
54
56
end
55
57
58
+ function tabulate (data:: (@compat Tuple{RealVector, RealVector}) , midpoints:: (@compat Tuple{Range, Range}) )
59
+ tabulate (data, default_weights (data), midpoints)
60
+ end
61
+
56
62
# convolution with product distribution of two univariates distributions
57
63
function conv (k:: BivariateKDE , dist:: (@compat Tuple{UnivariateDistribution,UnivariateDistribution}) )
58
64
# Transform to Fourier basis
81
87
82
88
typealias BivariateDistribution @compat (Union{MultivariateDistribution,Tuple{UnivariateDistribution,UnivariateDistribution}})
83
89
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)
86
94
conv (k,dist)
87
95
end
88
96
89
97
function kde (data:: (@compat Tuple{RealVector, RealVector}) , dist:: BivariateDistribution ;
90
98
boundary:: (@compat Tuple{(@compat Tuple{Real,Real}) ,(@compat Tuple{Real,Real})}) = (kde_boundary (data[1 ],std (dist[1 ])),
91
99
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))
93
102
94
103
xmid = kde_range (boundary[1 ],npoints[1 ])
95
104
ymid = kde_range (boundary[2 ],npoints[2 ])
96
105
97
- kde (data,(xmid,ymid),dist)
106
+ kde (data,weights, (xmid,ymid),dist)
98
107
end
99
108
100
109
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) )
102
111
103
112
dist = kernel_dist (kernel,bandwidth)
104
- kde (data,midpoints,dist)
113
+ kde (data,weights, midpoints,dist)
105
114
end
106
115
107
116
function kde (data:: (@compat Tuple{RealVector, RealVector}) ;
108
117
bandwidth= default_bandwidth (data),
109
118
kernel= Normal,
110
119
boundary:: (@compat Tuple{(@compat Tuple{Real,Real}) ,(@compat Tuple{Real,Real})}) = (kde_boundary (data[1 ],bandwidth[1 ]),
111
120
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))
113
123
114
124
dist = kernel_dist (kernel,bandwidth)
115
125
xmid = kde_range (boundary[1 ],npoints[1 ])
116
126
ymid = kde_range (boundary[2 ],npoints[2 ])
117
127
118
- kde (data,(xmid,ymid),dist)
128
+ kde (data,weights, (xmid,ymid),dist)
119
129
end
120
130
121
131
# matrix data
0 commit comments