%% Simulate Random Walk Through Markov Chain
% 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);

%
% Plot a directed graph of the Markov chain. Indicate the probability of transition by using edge colors.

figure;
graphplot(mc,'ColorEdges',true);

%
% Simulate a 20-step random walk that starts from a random state.

rng(1); % For reproducibility
numSteps = 20;
X = simulate(mc,numSteps)

% X is a 21-by-1 matrix. Rows correspond to steps in the random walk. Because X(1) is 3, the random walk begins at state 3.
%
% Visualize the random walk.

figure;
simplot(mc,X);

% Copyright 2012 The MathWorks, Inc.