


simulate_chain.m simulates a Markov chain on {1, 2, ..., n} given an
initial distribution and a transition matrix.

0001 % simulate_chain.m simulates a Markov chain on {1, 2, ..., n} given an 0002 % initial distribution and a transition matrix. 0003 0004 % Program to simulate a Markov chain 0005 % Simply modify the initial distribution and transition matrix 0006 % The program assumes that the states are labeled 1, 2, ... 0007 0008 % Below is a sample, but you might want to use another .m file to 0009 % first set up mu and P, then run this program with the following 0010 % lines commented out. 0011 % mu=[1 0 0]; % initial distribution 0012 % P=[[.6 .3 .1]; [.3 .3 .4]; [.4 .1 .5]]; % transition matrix 0013 0014 n=80; % number of time steps to take 0015 0016 num=10; % number of realizations to show 0017 for v=1:num, 0018 subplot(num,1,v); 0019 0020 x=zeros(1,n+1); % clear out any old values 0021 t=0:n; % time indices 0022 0023 x(1)=rando(mu); % generate first x value (time 0, not time 1) 0024 0025 for i=1:n, 0026 x(i+1) = rando(P(x(i),:)); 0027 end 0028 0029 plot(t, x, 'o'); 0030 axis([0 n 0 (length(mu)+1)]); 0031 end