function [Da, Ds]=repsamp(A,k,h,ss,n,r)

% REPSAMP     Repeated sampling from a population.
%
%             Example: [Da,Ds]=repsamp(suicide,'knorm',0,50,10,0);
% 
%             REPSAMP(A,K,H,SS,N,R) takes N samples of size SS from the data
%             in A.  If R=0 then sampling is without replacement.  If R<>0 then
%             sampling is with replacement.
%
%             For each sample a measure of the difference between the KDE of the
%             sample and of the whole dataset is taken.  KDEs are based upon the 
%             kernel K and smoothing parameter H.
%
%             If H=0 then each KDE will use an 'optimum' H value for that sample.
%             If H is specified then the same H value is used for all KDEs.
%
%             Christian C. Beardah 1996

inc=300;

[xpop, ypop]=kdfft1(A,k,inc,h,0);

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

for i=1:n,

  s=sample(A,ss,r);
  ysam=kdest1d(xpop,s,k,h)';

% Absolute difference

  Da(i)=delta*(sum(abs(ysam-ypop)));

% Squared difference

  Ds(i)=delta*(sum((ysam-ypop).^2));

end;
