%% Specify Initial Distribution for State Redistribution
% 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);

%
% Compute a 20-step redistribution of the Markov chain using random initial values.

rng(1); % For reproducibility
x0 = rand(mc.NumStates,1);
rd = redistribute(mc,20,'X0',x0);

%
% Plot the redistribution.

figure;
distplot(mc,rd);

% The redistribution suggests that the chain is periodic with a period of three.
%
% Remove periodicity by creating a lazy version of the Markov chain.

lc = lazy(mc);

%
% Compute a 20-step redistribution of the lazy chain using random initial values. Plot the redistribution.

x0 = rand(mc.NumStates,1);
lrd1 = redistribute(lc,20,'X0',x0);

figure;
distplot(lc,lrd1);

% The redistribution appears to settle after several steps.
% Copyright 2012 The MathWorks, Inc.