


%%%%%%%%%%%%%%% Gambler's ruin computations %%%%%%%%%%%%%%%%


0001 %%%%%%%%%%%%%%%% Gambler's ruin computations %%%%%%%%%%%%%%%% 0002 n=5; % n+1 possible states, numbered 1 to n+1, corresp to 0 to n 0003 P=zeros(n+1,n+1); % blank matrix to work with 0004 for i=2:n, 0005 P(i,i-1)=0.5; % transitions from transient states 0006 P(i,i+1)=0.5; 0007 end 0008 P(1,1)=1; % transitions for absorbing states 0009 P(n+1,n+1)=1; 0010 t=2:n; % index vector of transient states, to pull out Q 0011 Q=P(t,t); % SLICK! Pulls out columns and rows 2 to n of P 0012 RT=inv(eye(n-1)-Q); % matrix telling expected number of visits 0013 r=[1,n+1]; % index vector of recurrent states, to pull out L 0014 L=P(t,r); % SLICK! Pulls out rows 2 to n and columns 1 and n+1 of P