function y=bkdest1d(x,A,h)

% BKDEST1D    Uni-variate Boundary Kernel Density Estimator.
%
%             Example: load kddata
%                      x=0:0.1:5;
%                      bkdest1d(x,glass(:,5),0);
%
%             BKDEST1D(X,A,H) evaluates a uni-variate boundary corrected 
%             Kernel Density Estimate based upon the normal kernel.  
%             (Ref: Simple Boundary correction for kernel density estimation,
%              M.C. Jones, Stats and Computing, 1993.)
%             
%             X: The vector of points for which the density estimate 
%                is required.  
%             A: The uni-variate data points are stored row by row in A.
%             H: The parameter H represents the window width.  (If H=0 
%                an 'optimal' value is used - calculated using the STE rule.)
%
%             Christian C. Beardah 1994

if nargin==2,
  h=0;
end;

[n d]=size(A); % Establish dimensions of A,
               % n=number of data points, 
               % d=1 for uni-variate data.

if h==0, % If no value of h has been specified then calculate one
%  h=hns(A,'knorm');  
  h=hste(A,'knorm'); % Calculate an 'optimal' value of h.
end;

x1=kdfft1(A,'knorm',128,h,0);

hold on;

inc=-min(x1)/5;

x1=min(x1):inc:-min(x1);
y1=kdest1d(x1,A,'knorm',h);

x2=x1(6:11);
y2(1)=2*y1(6);
for i=1:5,
  y2(i+1)=y1(6+i)+y1(6-i);
end;

plot(x2,y2,'w:');

store=zeros(n,length(x));

for j=1:length(x),
  store(:,j)=feval('kn_bound',(x(j)*ones(size(A))-A)/h,x(j)/h)'; 
end;

y=sum(store)/(n*h^d); % y holds the k.d. estimate values corresponding 
                      % to the points in x. 

plot(x,y,'w--');

title('Dotted - Reflected, Dashed - Boundary');