﻿// P22EstCat.sce
// Estimation of categorical model f(y(t)|u(t),y(t-1)) with y,u=1,2    
// --- on-line update of statistics, off-line 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

I_dataSim=1;                            // get data: 1=simulated (in T12SimCat.sce)
                                        //           2=real (from file dataCat.dat)
if I_dataSim==1
  load _data/dataT12.dat Sim            // load of simulated data
  zt=Sim.Cz.zt;
  ut=Sim.Cz.ut;
  th=Sim.Cz.th;
else 
  load _data/dataCat.dat sp co ga mo
  zt=mo;                                // volba diskrétních
  ut=ga;                                //   veličin (tady mo, ga)
end

nd=length(zt);                          // number of data
V=zeros(4,2);                           // statistics

for t=2:nd                              // time loop
  j=psi2row([ut(t),zt(t-1)],[2,2]);     // row in parameter table
  V(j,zt(t))=V(j,zt(t))+1;              // generation of y
end
thE=fnorm(V,2);                         // point estimates
Est.Cz.V=V;
Est.Cz.th=thE;

// results
if I_dataSim==1
  bar([th(:,1) thE(:,1)]) 
  title('Simulated and estimated parameters (first column of the model table)')
  legend('simulated','estimated');
else 
  bar(thE(:,1)) 
  title('Estimated parameters - real data (first column)')
end

save _data/dataT22.dat Est
