%% Estimate Markov Chain Mixing Time
% Create a five-state transition matrix of empirical counts by generating a block diagonal matrix composed of discrete uniform draws.

m = 100; % Maximal count
rng(1); % For reproducibility
P = blkdiag(randi(100,2) + 1,randi(100,3) + 1)

%
% Create and plot a digraph of the Markov chain that is characterized by the transition matrix P.

mc = dtmc(P);

figure;
graphplot(mc)

%
% Determine the stationary distribution and mixing time of the Markov chain.

[xFix,tMix] = asymptotics(mc)

% Rows of xFix correspond to the stationary distributions of the two independent recurrent classes of mc.
%
% Create separate Markov chains representing the recurrent subchains of mc.

mc1 = subchain(mc,1);
mc2 = subchain(mc,3);

% mc1 and mc2 are dtmc objects.  mc1 is the recurrent class containing state 1, and mc2 is the recurrent class containing state 3.
%
% Compare the mixing times of the subchains.

[x1,t1] = asymptotics(mc1)
[x2,t2] = asymptotics(mc2)

% mc1 approaches its stationary distribution more quickly than mc2.
% Copyright 2012 The MathWorks, Inc.