Likelihood cross validation estimation

5 views (last 30 days)
Yingtong Dai
Yingtong Dai on 8 Jul 2020
Edited: Yingtong Dai on 8 Jul 2020
Hi,
I'm trying to use likelihood cross-validation method to select the bandwidth for the kernel density estimator in Matlab.
I've done something like below:
[~,~,bw] = ksdensity(DATA_I_HAVE);
%the default bw from ksdensity function
loglik = @(xtr,xte) sum(log(ksdensity(xtr,xte,'width',h)));
v = @(h,data,cens,freq) -sum(crossval(loglik,data,'leaveout',1));
h_hat = mle(DATA_I_HAVE,'nloglf',v,'start',bw)
Can I ask whether there is a way to put those two steps (the sum of log pdf and crossval) together? Alternatively, is there a better way to obtain the optimal bw using likelihood cross-validation method in Matlab?
Thanks very much!
Hi,
I've made some changes to the code, it works now, but takes quite long time, I really appreciate if you can give some suggestions for improvement!
[~,~,bw0] = ksdensity(DATA_I_HAVE);
h_hat = mle(DATA_I_HAVE,'nloglf',@custnloglf,'start',bw0);
function val = custnloglf(h,data,cens,freq)
val = -sum(crossval(@loglik,data,'leaveout',1));
function ll = loglik(xtr,xte)
ll = sum(log(ksdensity(xtr,xte,'width',h)));
end
end
Thanks!

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!