![Hands-On Markov Models with Python](https://wfqqreader-1252317822.image.myqcloud.com/cover/405/36699405/b_36699405.jpg)
Transience and recurrence
Given that we start at state i, it is called transient if there is a non-zero probability that we will never return to state i. To define this in more formal terms, let's consider a random variable Ti as the first return time to state i:
![](https://epubservercos.yuewen.com/EB2BC1/19470388308856506/epubprivate/OEBPS/Images/04d20803-cef5-45c5-9b34-dc573ecd193a.png?sign=1739031301-9tENFCbKUZxgFyKHIE5IXKz10LWIYGg3-0-1b7042b2e75e6cf43dd3eeaa22360136)
Let's now define another term, , as the probability of the system returns to state i after n steps:
![](https://epubservercos.yuewen.com/EB2BC1/19470388308856506/epubprivate/OEBPS/Images/270650b0-6e5f-44da-ae58-7dd809c8bc7c.png?sign=1739031301-xwG4RjkGjEqQ73duq6m3qFj5SaWvn0nD-0-c345188bf76bad776a615cba9ef680ea)
Now we can define that any given state i is transient if the following condition is met:
![](https://epubservercos.yuewen.com/EB2BC1/19470388308856506/epubprivate/OEBPS/Images/2f7cf787-a6e8-4932-80c9-222ebe3bf2e5.png?sign=1739031301-l51NUEzmmt9F9Rt5QtG7z1cWruRDK8kI-0-dcba8dcc5512d41c79f7fe28c3517a2b)
In the preceding equation, we are basically checking whether the total sum of probabilities of returning to state i in step sizes less than is less than 1. If the total sum is less than 1, it would mean that the probability of Ti to be
is greater than 0 which would mean that the state i is transient. The given state i is called recurrent if it is not transient:
![](https://epubservercos.yuewen.com/EB2BC1/19470388308856506/epubprivate/OEBPS/Images/e52def1e-c33f-4ac5-890c-54ae185acdab.png?sign=1739031301-JUsv55o9vz7enPyTSnNfIYyDv7D1BpbX-0-ac08731707dcef959d0246a837dadb77)
In the preceding example, we can see that states A and B are transient because A doesn't have any incoming edge. B does have an incoming edge, but it's incoming from another transient state and therefore it is also transient. Hence, once the system leaves state A or B, it won't be able to come back.
It is really simple to check whether a given state is transient or not. We can simply check whether there are any incoming edges from other states or not. If not, the state is transient. Let's write a simple method to check this for our MarkovChain class:
def is_transient(self, state):
"""
Checks if a state is transient or not.
Parameters
----------
state: str
The state for which the transient property needs to be checked.
"""
if all(self.transition_matrix[~self.index_dict[state], self.index_dict[state]] == 0):
return True
else:
return False
Now we can use this method in our example in Figure 1.6 to check which nodes are transient:
>>> transient_matrix = [[0, 0.5, 0.5, 0],
[0, 0, 0.25, 0.75],
[0, 0, 0, 1],
[0, 0, 0.5, 0.5]]
>>> transient_markov = MarkovChain(transition_matrix=transient_matrix,
states=['A', 'B', 'C', 'D'])
>>> transient_markov.is_transient('A')
True
>>> transient_markov.is_transient('B')
True
>>> transient_markov.is_transient('C')
False
In the following subsections, we will talk about the statistical properties of the random variable Ti.