%% Create Lazy Markov Chain
% Consider this three-state transition matrix.
% P = \left[ {\begin{array}{*{20}{c}}
% 0&1&0\\
% 0&0&1\\
% 1&0&0
% \end{array}} \right].
% Create the irreducible and periodic Markov chain that is characterized by the transition matrix P.

P = [0 1 0; 0 0 1; 1 0 0];
mc = dtmc(P);

% At time t = 1,..., T, mc is forced to move to another state deterministically.
%
% Determine the stationary distribution of the Markov chain and whether it is ergodic.

xFix = asymptotics(mc)
isergodic(mc)

% mc is irreducible and not ergodic. As a result, mc has a stationary distribution, but it is not a limiting distribution for all initial distributions.
%
% Show why xFix is not a limiting distribution for all initial distributions.

x0 = [1 0 0];
x1 = x0*P
x2 = x1*P
x3 = x2*P
sum(x3 == x0) == mc.NumStates

% The initial distribution is reached again after several steps, which implies that the subsequent state distributions cycle through the same sets of distributions indefinitely. Therefore, mc does not have a limiting distribution.
%
% Create a lazy version of the Markov chain mc.

lc = lazy(mc)
lc.P

% lc is a dtmc object. At time t = 1,..., T, lc "flips a fair coin". It remains in its current state if the "coin shows heads" and transitions to another state if the "coin shows tails".
%
% Determine the stationary distribution of the lazy chain and whether it is ergodic.

lcxFix = asymptotics(lc)
isergodic(lc)

% lc and mc have the same stationary distributions, but only lc is ergodic. Therefore, the limiting distribution of lc exists and is equal to its stationary distribution.
% Copyright 2012 The MathWorks, Inc.