Skip to content

Commit f7821fd

Browse files
committed
fix the bwlcv methods for multivariate kde
1 parent f776585 commit f7821fd

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/bandwidth.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function bwlcv(xdata::RealVector, kernel::Function)
144144
return Optim.minimizer(Optim.optimize(h->lcv(xdata,kernel,h,w,n), hlb, hub, iterations=200,abs_tol=h0/n^2))
145145
end
146146

147-
function lcv(xdata::RealMatrix, kernel::Array{Function, 1}, h::RealVector, w::Vector, n::Int)
147+
function lcv(xdata::RealMatrix, kernel::Vector, h::RealVector, w::Vector, n::Int)
148148
# -mean(kerneldensity(xdata,xdata,kernel,h)) + mean(map(kernel, xdata, xdata, h))
149149
if any(h .<= 0.0)
150150
return Inf
@@ -170,7 +170,7 @@ function lcv(xdata::RealMatrix, kernel::Array{Function, 1}, h::RealVector, w::Ve
170170
end
171171
-ll
172172
end
173-
function bwlcv(xdata::RealMatrix, kernel::Array{Function, 1})
173+
function bwlcv(xdata::RealMatrix, kernel::Vector)
174174
n, p = size(xdata)
175175
w = ones(n)
176176
h0 = zeros(p)
@@ -191,7 +191,12 @@ function bwlcv(xdata::RealMatrix, kernel::Array{Function, 1})
191191
hub[j] = h0[j]
192192
end
193193
end
194-
Optim.minimizer(Optim.optimize(h->lcv(xdata, kernel, h, w, n), h0))
194+
h = Optim.minimizer(Optim.optimize(h->lcv(xdata, kernel, h, w, n), h0))
195+
if all(hlb .<= h .<= hub)
196+
return h
197+
else
198+
return h0
199+
end
195200
end
196201

197202

src/density.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ function kerneldensity(xdata::RealVector; xeval::RealVector=xdata, lb::Real=-Inf
1515
return den
1616
end
1717

18-
function kerneldensity(xdata::RealMatrix; xeval::RealMatrix=xdata,
19-
kernel::Array{Function, 1}=[gaussiankernel for i in 1:size(xdata)[2]], h::RealVector=bwlcv(xdata, kernel))
20-
18+
function kerneldensity(xdata::RealMatrix; xeval::RealMatrix=xdata,
19+
kernel::Vector=[gaussiankernel for i in 1:size(xdata)[2]], h::RealVector=-Inf .* ones(size(xdata, 2)))
20+
2121
if any(h .<= 0)
22-
error("h < 0!")
22+
h = bwlcv(xdata, kernel)
23+
warn("The user are responsible for giving the bandwidth! The defaults may not work well.")
2324
end
2425
m, p=size(xeval)
2526
n, p1 = size(xdata)

test/testreg.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
##Univariate kerneldensity and regression
33
using Distributions
4+
srand(2017);
45
x=rand(Normal(10), 500)
56
xeval=linspace(minimum(x), maximum(x), 100)
67
h = bwlscv(x, gaussiankernel)
@@ -28,6 +29,11 @@ yfit1=npr(x, y, xeval=xeval, reg=locallinear)
2829
cb=bootstrapCB(x, y, xeval=xeval)
2930
@test mean(vec(cb[1,:]) .<= yfit1 .<= vec(cb[2,:])) > .8
3031

32+
#multivariate density estimation
33+
x = rand(Normal(10), 500, 3)
34+
denvalues = kerneldensity(x, h = [1.0, 1.0, 1.0])
35+
@test all(denvalues .>= 0)
36+
3137
#multivariate regression
3238
x = rand(Normal(10), 500, 3)
3339
y = x * ones(3) .+ x.^2 *ones(3)

0 commit comments

Comments
 (0)