// P12SimCat.sce
// Simulation of categorical model f(y(t)|u(t),y(t-1)) with y,u=1,2 
// ----------------------------------------------------------------
[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

nd=50;                                  // number of data
I_typU=1;

Sim.Cz.th=[.9 .1                        // model parameters
           .8 .2
           .3 .7
           .1 .9];

select I_typU                           // choice of input
case 1 then 
    ut=fix(2*rand(1,nd,'u'))+1;         // random
  case 2 then                           // two steps
    ut=[ones(1,fix(nd/2)), 2*ones(1,fix(nd/2))]; 
  case 3                                // regular 1 2 1 2 1 2 ...
    ut=ones(1,nd); ut(2:2:nd)=2;
end 
zt=ones(1,nd);

// time loop
th=Sim.Cz.th;
for t=2:nd                              // time loop
  j=psi2row([ut(t),zt(t-1)],[2,2]);     // row in parameter table
  zt(t)=sum(rand(1,1,'u')>cumsum(th(j,:)))+1;   
                                        // generation of y
end
Sim.Cz.zt=zt;
Sim.Cz.ut=ut;

// results
s=1:nd;                                 
plot(s,zt(s),'.:',s,ut(s),'o')
legend('output','input');
title('Simulated data')
set(gca(),'data_bounds',[1 nd .9 2.1])

save _data/dataT12.dat Sim

