function [h,hvec,score]=hboot(A,k,steps)

% HBOOT       Bootstrap cross-validation estimate of h.
%
%             Examples: load kddata;
%
%             (1) hboot(suicide,'knorm')
%
%             (2) [h,hvec,score]=hboot(maddiam,'knorm'); 
%                 plot(hvec,score)     
%
%             HBOOT(A,K,STEPS)
%
%             Christian C. Beardah 1995 

n=length(A);

if nargin==2,
  steps=100;
end;

H=hns(A,k);

hvec=linspace(0.01,2*H,steps);

M=A*ones(size(A'));

Y1=(M-M');
 
for i=1:steps,

  Y=-Y1.^2/(4*hvec(i)^2);

  T1=exp(Y/2);

  T1=T1-diag(diag(T1));

  T2=-4*exp(Y/1.5)/sqrt(3);

  T2=T2-diag(diag(T2));

  T3=sqrt(2)*exp(Y);

  T3=T3-diag(diag(T3));

  T=T1+T2+T3;

  T=sum(sum(T))+n*sqrt(2);

  score(i)=T/(2*n^2*hvec(i)*sqrt(2*pi));

end;

[L,I]=min(score);

h=hvec(I);
