The problem: Given an arbitrary matrix which represents the probability of transition from one state to another state in one step for a Markov chain, determine all closed sets of states and the transient state.
This small note describes an algorithm that I developed which solves this problem. The algorithm is recursive in nature.
A Matlab implementation is given below with an example run on 3 diﬀerent matrices and how to call the matlab function.
This algorithm describes how to ﬁnd all closed sets and the transient set (if any) given as input the matrix which contains the initial one step ﬁnite chain Markov probability transition from one state to another. The algorithm works directly on the matrix itself and does not require prior knowledge of which states are recurrent.
An implementation of the algorithm is provided (in Matlab) with a number of examples.
As an example of an input, let us consider the following simple Markov chain state diagram and its corresponding matrix and then we show the output generated from this algorithm. In this diagram, the numbers on the arrows between the states (the circles) is the probability of going from the source state to the end state.
The matrix for the above diagram is the following
The output of the algorithm below will be the following
closed sets: , transient set:
Once the closed sets and the transient set are known, generating the canonical form is a simple matter of formatting the matrix in the following form
Now the algorithm is described.
The basic idea of the algorithm is the following: For each state, we generate the path of states that can be travelled from that state. Next, we collect the states which has the same states on its path. Each set of states which has the same states on their path are one closed set. The remaining states, if any, would all have diﬀerent set of states on their path. These states make up the set of transient states.
The algorithm will terminate since the length of the longest path that could possibly be travelled from any state is ﬁnite (which is the number of states in the chain).
Since there are states, and for each state we need to examine other states at most, the algorithm is of order .
Here is a description of the algorithm
Input: A square by probability transition matrix (i.e. each row in the matrix sums to )
output: all sets which are closed and transient set (if it exists)
To illustrate how this algorithm works, we apply it to the example Markov chain shown above.
In step 1, we generate the LHS and the RHS lists from the input matrix.
The above is send to step 2. In step 2 we start by scanning the RHS list of each state listed in the LHS set. For state we see that there is only state in its RHS. and from the rule in we see that we do nothing and go to the next state in its RHS set. But this is the only state in this set. Hence we are done with state and we go to state in the LHS set.
We start again by looking at the set of states of the RHS of state , which are . we start with state which is the same as the state in the LHS, so we skip this per rule , and go to the next state which is . According to , we replace this by states of , where of state is seen to be which is the last RHS set in the last entry of the LHS set as shown in (1). Hence now the RHS of state becomes . Since there are no more states in the RHS of we have completed this state. Now we remove duplicated entries in the above list to obtain . We now go to state in the LHS set. and do the same. We do this until we processed all states in the LHS. At the end we have the following LHS and RHS generated
We now compare RHS sets in (1) with those in (2). Since they are not the same, we repeat step 2 again, but use the above RHS now as an input to step 2.
Again, we start by scanning the RHS list of each state in the LHS. We repeat the same thing as before, lets say we reached state in the LHS set now. We see that its RHS is , we start with state and replace that with states of which is . We go to state , and replace that with states of which is . Next we do state and replace that with states of which is , next we do state , and replace that with states of which is , and ﬁnally we do state and replace that with states of which is , now that we completed the RHS processing of state we remove all duplicated states in its RHS and we obtain . Next we go to state in the LHS set and process that similarly. We then do state in the LHS set. Now we have completed step 2 again and the ﬁnal result is the following
We see now that the RHS in (3) is the same as the RHS in hence we stop and go to step 4 in the algorithm.
Now we collect all states from the LHS which has the same list in its RHS. We see that state is its own class. And since the LHS of this class which is state is the same as the state in the RHS which is , then this class is closed set. Next we see that states has the same states on their RHS which is and since the LHS states is the same as the RHS states, then this is a close set class that contains states , next we see that state and state also has the same RHS set which is but now since , then the states make a class which is the transient states. Hence are transient and all other states are recurrent.
The following is a test run of the implementation showing in each case the matrix and the output of the algorithm.