%% Extract Recurrent Subchain from Markov Chain
% Consider this theoretical, right-stochastic transition matrix of a stochastic process.
% P = \left[ {\begin{array}{*{20}{c}}
% 0&1&0&0\\
% {0.5}&0&{0.5}&0\\
% 0&0&0.5&0.5\\
% 0&0&0.5&0.5
% \end{array}} \right].
% Create the Markov chain that is characterized by the transition matrix P.

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

%
% Plot a directed graph of the Markov chain. Visually identify the communicating class to which each state belongs by using node colors.

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

%
% Determine the stationary distribution of the Markov chain.

x = asymptotics(mc)

% The Markov chain eventually gets absorbed into states 3 and 4, and subsequent transitions are stochastic.
%
% Extract the recurrent subchain of the Markov chain by passing mc to subchain and specifying one of the states in the recurrent, aperiodic communicating class.

sc = subchain(mc,3);

% sc is a dtmc object.
%
% Plot a directed graph of the subchain.

figure;
graphplot(sc,'ColorNodes',true)

% Copyright 2012 The MathWorks, Inc.