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

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

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

n=length(A);

inc=512;

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

Y1=(M-M');

H=hns(A,k);

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

for i=1:length(hvec),

  [xa,y]=kdfft1(A,k,inc,hvec(i),0);

  delta=xa(2)-xa(1);

  L1=delta*sum(y.^2);

  Y=Y1/hvec(i);

  L2=sum(sum(feval(k,Y)))/(hvec(i)*(n-1))-n*feval(k,0)/(hvec(i)*(n-1));

  score(i)=L1-2*L2/n;

end;

[L,I]=min(score);

h=hvec(I);