// P31PreReg1.sce
// Prediction np-steps with scalar regression model of order n 
// ------------------------------------------------------------
[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                 

// definition of regression vector
deff('ps=genpsi(t,n,y,u)','ps=[y(t-(1:n)),u(t-(0:n)) 1]')

load _data/dataT11.dat Sim    // results from simulation T11SimReg1.sce
yt=Sim.Cy.yt;
ut=Sim.Cy.ut;
th=Sim.Cy.th;
cv=Sim.Cy.cv;
ord=Sim.Cy.ord;
load _data/dataT21.dat Est    // results from estimation T21EstReg1.sce
thE=Est.Cy.th;
ordE=Est.Cy.ord;

np=5;     // no of prediction steps (1=prediction from the model)

nd=length(yt);
yp=zeros(1,nd);
for t=(ordE+1):(nd-np+1)      // time loop
  yi=yt(1:(t-1));             // old data (at time t)
  for j=0:(np-1)              // loop of prediction
    ps=genpsi(t+j,ordE,yi,ut);// construction of regression vector
    yi(t+j)=ps*thE;           // auxiliary prediction
  end
  yp(t+np-1)=yi(t+np-1);      // final prediction at t
end
Pre.Cy.np=np;
Pre.Cy.yp=yp;

// Results
s=(ordE+1):(nd-np);
plot(s,yt(s),s,yp(s))
title('Output and its prediction')
legend('output','prediction');

save _data/dataT31.dat Sim Est Pre
