// P32PreCatN.sce
// Prediction np-step with categorical model
// - the old outputs are substituted by point estimates
// ------------------------------------------------------
[u,t,n]=file();                         // find working directory
chdir(dirname(n(2)));                   // set working directory
clear("u","t","n")                      // clear auxiliary data
exec("ScIntro.sce",-1),mode(0)          // intro to sesion                 

load _data/dataT12.dat Sim         // results from task T12SimCat.sce
zt=Sim.Cz.zt;                      //   from simulation, the data 
ut=Sim.Cz.ut;                      //   zt, ut and parameters th
th=Sim.Cz.th;                      //   are extracted
load _data/dataT22.dat Est         // results from task T22EstCat.sce
thE=Est.Cz.th;

np=3;     // numb. of prediction steps (1=prediction from the model)

nd=length(zt);
yp=zeros(1,nd);
for t=2:(nd-np+1)                  // time loop
  yi=zt(1:(t-1));                  // old data (at time t)
  for k=0:(np-1)                   // loop of prediction
    j=psi2row([ut(t+k),yi(t+k-1)],[2,2]);          // row of the table
    yi(t+k)=sum(rand(1,1,'u')>cumsum(thE(j,:)))+1; // point prediction
  end
  yp(t+np-1)=yi(t+np-1);           // final prediction at time t
end
Pre.Cz.np=np;
Pre.Cz.yp=yp;

// results
s=(fix(nd/2)):(nd-np);
printf(' %d wrong predictions from %d\n',sum(zt(s)~=yp(s)),length(s))

plot(s,zt(s),'b:o',s,yp(s),'r:.')
legend('output','prediction');

save _data/dataT32.dat Sim Est Pre
