% Generate Markov transition probability matrix from AR(1) process
% pr(n,n)=markov(n,rho), where
%   n = number of states and
%   rho = ar(1) coefficient of the process

function pr = markov(n,rho);

pr=zeros(n,n);
p1=pr;p2=pr;p3=pr;p4=pr;
p=0.5*(1+rho);
pr(1:2,1:2)=[p 1-p;1-p p];

for i = 3:n;
    p1(1:i-1,1:i-1)=pr(1:i-1,1:i-1);
    p2(1:i-1,2:i  )=pr(1:i-1,1:i-1);
    p3(2:i  ,1:i-1)=pr(1:i-1,1:i-1);
    p4(2:i  ,2:i  )=pr(1:i-1,1:i-1);
    pr=p*(p1+p4)+(1-p)*(p2+p3);
    pr(2:i-1,1:i)=0.5*pr(2:i-1,1:i);
end;
