%% Return Expected First Hitting Times
% Consider this theoretical, right-stochastic transition matrix of a stochastic process.
% P = \left[ {\begin{array}{*{20}{c}}
% 1&0&0&0\\
% {1/2}&0&{1/2}&0\\
% 0&0&0&1\\
% 0&0&1/2&1/2
% \end{array}} \right].
% Create the Markov chain that is characterized by the transition matrix P.

P = [1 0 0 0; 1/2 0 1/2 0; 0 0 0 1; 0 0 1/2 1/2];
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);

%
% Compute the expected first hitting time for state 3, beginning from each state in the Markov chain.

ht = hittime(mc,3)

% Because state 3 is unreachable from state 1, state 1 is a remote state  with respect to state 3, with an expected first hitting time of Inf.
% State 3 is reachable from state 2, but state 2 has a positive probability of transitioning to state 1, which is an absorbing state. Therefore, state 2 is a remote-reachable state with respect to state 3, with an expected first hitting time of Inf.
% Because state 3 is the target, its expected first hitting time for itself is 0.
% The expected first hitting time for state 3 starting from state 4 is 2 time steps.
% Copyright 2019 The MathWorks, Inc.
%