function spdraw=mcdraw(H,sp)
% function spdraw=mcdraw(H,sp)
% provides a draw from the pdf p({S_t}|data_T)
% Francesco Bianchi found two errors in this code, which have
% been repaired 7/26/08
T=size(sp,1);
spdraw=zeros(T,1);
% u=rand(1,1);
p=sp(T,:);
u=rand(1,1);
cdf=cumsum(p);
if u==0
   spdraw(T)=1;
else
   spdraw(T)=find((u<=cdf) & (u> [0 cdf(1:end-1)]));
end
for it=(T-1):-1:1
   p= H(spdraw(it+1),:).*sp(it,:);
   if sum(p)==0
      %p=sp(it,:);
      p=ones(1,ns)/ns;
   else
      p=p/sum(p);
   end
   u=rand(1,1);
   cdf=cumsum(p);
   if u==0
      spdraw(it)=1;
   else
      spdraw(it)=find((u<=cdf) & (u> [0 cdf(1:end-1)]));
   end
end
