function [Da,Ds]=repsnmd(w,sigma,mu,k,ss,n)

% REPSNMD     Repeated sampling from a normal mixture density.
%
%             Example: [Da,Ds]=repsnmd([0.75 0.25]',[1 1/3]',[0 3/2]','knorm',50,10);
% 
%             REPSNMD(W,SIGMA,MU,K,SS,N) takes N samples of size SS from the 
%             normal mixture density in W, SIGMA, MU.
%
%             For each sample a measure of the difference between the KDE of the
%             sample and the true density of the whole dataset is taken.  KDEs are 
%             based upon the kernel K.  The type of algorithm used for generating 
%             the smoothing parameter H is defined within the routine.
%
%             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;

xmin=min(mu-3.5*sigma);
xmax=max(mu+3.5*sigma);

xpop=linspace(xmin,xmax,inc);

ypop=nmdplot(xpop,w,sigma,mu,0)';

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

for i=1:n,

  s=snmd(ss,w,sigma,mu);

% H selection algorithm:

  h=hste(s,k);
%  h=hns(s,k);

  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;