Consider this theoretical, right-stochastic transition matrix of a stochastic process.
P=[001/21/41/400001/302/300000001/32/3000001/21/2000003/41/41/21/2000001/43/400000].
Create the Markov chain that is characterized by the transition matrix P.
P = [ 0 0 1/2 1/4 1/4 0 0 ;
0 0 1/3 0 2/3 0 0 ;
0 0 0 0 0 1/3 2/3;
0 0 0 0 0 1/2 1/2;
0 0 0 0 0 3/4 1/4;
1/2 1/2 0 0 0 0 0 ;
1/4 3/4 0 0 0 0 0 ];
mc = dtmc(P);
Generate 100 20-step random walks starting from state 1.
rng(1); % For reproducibility
numSteps = 20;
X0 = zeros(mc.NumStates,1);
X0(1) = 100; % 100 random walks starting from state 1 only
X = simulate(mc,numSteps,'X0',X0);
X is a 21-by-100 matrix.
Visualize the proportion of times that states transition to other states for all random walks by using a heatmap.
figure;
simplot(mc,X,'Type','transitions');
Rows and columns of the heatmap correspond to state numbers.
Compare the realized transition matrix to the theoretical transition matrix by using a heatmap.
figure;
imagesc(mc.P);
colormap(jet);
axis square;
colorbar;
The realized and theoretical transition matrices are similar.