%% Compare Theoretical and Realized Transitions
% Consider this theoretical, right-stochastic transition matrix of a stochastic process.
% P = \left[ {\begin{array}{*{20}{c}}
% 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
% \end{array}} \right].
% 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.
% Copyright 2012 The MathWorks, Inc.