%% Create Markov Chain from Stochastic Transition Matrix
% This example shows how to create a Markov chain object to model a hypothetical economic cycle using a stochastic transition matrix.
% Suppose that the dynamic behavior of the real US gross domestic product (GDP) switches between four models:
% Regime 1: An autoregressive model with a low mean and low volatility
% Regime 2: An autoregressive model with a low mean and high volatility
% Regime 3: An autoregressive model with a high mean and low volatility
% Regime 4: An autoregressive model with a high mean and high volatility
% Consider this right-stochastic transition matrix containing the probabilities of state transitions between time steps t and t + 1, for all t.
% P = \left[ {\begin{array}{*{20}{c}}
% {0.5}&{0.5}&{0}&0\\
% 0.5&{0}&{0.5}&0\\
% {0}&0&{0}&{1}\\
% {0}&0&1&{0}
% \end{array}} \right]
% For example, P_{23} = 0.5 means the probability that real GDP transitions from Regime 2 to Regime 3 in the next time step is 0.5.
% Set the transition matrix to a variable.

P = [0.5 0.5 0.0 0.0;
     0.5 0.0 0.5 0.0;
     0.0 0.0 0.0 1.0;
     0.0 0.0 1.0 0.0];

%
% Create a Markov chain object characterized by the transition matrix P.

mc = dtmc(P)

% mc is a dtmc object. MATLAB® displays properties and corresponding values of mc at the command line.
% The association of names to the states enhances displays of function output and plots of the chain. When you create the Markov chain object, you can associate names to the states by using the 'StateNames' name-value pair argument. After you create the object, you can associate names to the states by setting the StateNames property using dot notation.
%
% Associate names to the states in mc by using dot notation. Elements of the string vector containing the names correspond to the rows and columns of P.

stateNames = ["Regime 1" "Regime 2" "Regime 3" "Regime 4"];
mc.StateNames = stateNames;

%
% Plot a digraph of the Markov chain.

figure;
graphplot(mc);

% After creating and plotting the Markov chain, you can determine characteristics of the chain, such as its stationary distribution by using asymptotics or its state distribution evolution by using redistribution.
% Copyright 2012 The MathWorks, Inc.