giddy.ergodic.mfpt¶
- giddy.ergodic.mfpt(P, fill_empty_classes=False)[source]¶
Generalized function for calculating mean first passage times for an ergodic or non-ergodic transition probability matrix.
- Parameters:
- Parray
(k, k), an ergodic/non-ergodic Markov transition probability matrix.
- fill_empty_classes: bool, optional
If True, assign 1 to diagonal elements which fall in rows full of 0s to ensure the transition probability matrix is a stochastic one. Default is False.
- Returns:
- mfpt_allarray
(k, k), elements are the expected value for the number of intervals required for a chain starting in state i to first enter state j. If i=j then this is the recurrence time.
Examples
>>> import numpy as np >>> from giddy.ergodic import mfpt >>> np.set_printoptions(suppress=True) #prevent scientific format
Irreducible Markov chain
>>> p = np.array([[.5, .25, .25],[.5,0,.5],[.25,.25,.5]]) >>> fm = mfpt(p) >>> fm array([[2.5 , 4. , 3.33333333], [2.66666667, 5. , 2.66666667], [3.33333333, 4. , 2.5 ]])
Thus, if it is raining today in Oz we can expect a nice day to come along in another 4 days, on average, and snow to hit in 3.33 days. We can expect another rainy day in 2.5 days. If it is nice today in Oz, we would experience a change in the weather (either rain or snow) in 2.67 days from today.
Reducible Markov chain: two communicating classes (this is an artificial example)
>>> p = np.array([[.5, .5, 0],[.2,0.8,0],[0,0,1]]) >>> mfpt(p) array([[3.5, 2. , inf], [5. , 1.4, inf], [inf, inf, 1. ]])
Thus, if it is raining today in Oz we can expect a nice day to come along in another 2 days, on average, and should not expect snow to hit. We can expect another rainy day in 3.5 days. If it is nice today in Oz, we should expect a rainy day in 5 days.
>>> p = np.array([[.5, .5, 0],[.2,0.8,0],[0,0,0]]) >>> mfpt(p, fill_empty_classes=True) array([[3.5, 2. , inf], [5. , 1.4, inf], [inf, inf, 1. ]])
>>> p = np.array([[.5, .5, 0],[.2,0.8,0],[0,0,0]]) >>> mfpt(p, fill_empty_classes=False) Traceback (most recent call last): ... ValueError: Input transition probability matrix has 1 rows full of 0s. Please set fill_empty_classes=True to set diagonal elements for these rows to be 1 to make sure the matrix is stochastic.