giddy.markov.Markov¶
- class giddy.markov.Markov(class_ids, classes=None, fill_empty_classes=False, summary=True)[source]¶
Classic Markov Chain estimation.
- Parameters:
- class_idsarray
(n, t), one row per observation, one column recording the state of each observation, with as many columns as time periods.
- classesarray
(k, 1), all different classes (bins) of the matrix.
- fill_empty_classes: bool
If True, assign 1 to diagonal elements which fall in rows full of 0s to ensure p is a stochastic transition probability matrix (each row sums up to 1).
- summarybool
If True, print out the summary of the Markov Chain during initialization. Default is True.
Examples
>>> import numpy as np >>> from giddy.markov import Markov >>> c = [['b','a','c'],['c','c','a'],['c','b','c']] >>> c.extend([['a','a','b'], ['a','b','c']]) >>> c = np.array(c) >>> m = Markov(c) The Markov Chain is irreducible and is composed by: 1 Recurrent class (indices): [0 1 2] 0 Transient classes. The Markov Chain has 0 absorbing states. >>> m.classes.tolist() ['a', 'b', 'c'] >>> m.p array([[0.25 , 0.5 , 0.25 ], [0.33333333, 0. , 0.66666667], [0.33333333, 0.33333333, 0.33333333]]) >>> m.steady_state array([0.30769231, 0.28846154, 0.40384615])
Reducible Markov chain
>>> c = [['b','a','a'],['c','c','a'],['c','b','c']] >>> m = Markov(c) The Markov Chain is reducible and is composed by: 1 Recurrent class (indices): [0] 1 Transient class (indices): [1 2] The Markov Chain has 1 absorbing state (index): [0]
US nominal per capita income 48 states 81 years 1929-2009
>>> import libpysal >>> import mapclassify as mc >>> f = libpysal.io.open(libpysal.examples.get_path("usjoin.csv")) >>> pci = np.array([f.by_col[str(y)] for y in range(1929,2010)])
set classes to quintiles for each year
>>> q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose() >>> m = Markov(q5) The Markov Chain is irreducible and is composed by: 1 Recurrent class (indices): [0 1 2 3 4] 0 Transient classes. The Markov Chain has 0 absorbing states. >>> m.transitions array([[729., 71., 1., 0., 0.], [ 72., 567., 80., 3., 0.], [ 0., 81., 631., 86., 2.], [ 0., 3., 86., 573., 56.], [ 0., 0., 1., 57., 741.]]) >>> m.p array([[0.91011236, 0.0886392 , 0.00124844, 0. , 0. ], [0.09972299, 0.78531856, 0.11080332, 0.00415512, 0. ], [0. , 0.10125 , 0.78875 , 0.1075 , 0.0025 ], [0. , 0.00417827, 0.11977716, 0.79805014, 0.07799443], [0. , 0. , 0.00125156, 0.07133917, 0.92740926]]) >>> m.steady_state array([0.20774716, 0.18725774, 0.20740537, 0.18821787, 0.20937187])
Relative incomes
>>> pci = pci.transpose() >>> rpci = pci/(pci.mean(axis=0)) >>> rq = mc.Quantiles(rpci.flatten()).yb.reshape(pci.shape) >>> mq = Markov(rq) The Markov Chain is irreducible and is composed by: 1 Recurrent class (indices): [0 1 2 3 4] 0 Transient classes. The Markov Chain has 0 absorbing states. >>> mq.transitions array([[707., 58., 7., 1., 0.], [ 50., 629., 80., 1., 1.], [ 4., 79., 610., 73., 2.], [ 0., 7., 72., 650., 37.], [ 0., 0., 0., 48., 724.]]) >>> mq.steady_state array([0.17957376, 0.21631443, 0.21499942, 0.21134662, 0.17776576])
- Attributes:
- kint
Number of Markov states.
- parray
(k, k), transition probability matrix.
- num_cclassesint
Number of communicating classes.
- cclasses_indiceslist
List of indices within each communicating classes.
- num_rclassesint
Number of recurrent classes.
- rclasses_indiceslist
List of indices within each recurrent classes.
- num_astatesint
Number of absorbing states.
- astates_indiceslist
List of indices of absorbing states.
- steady_statearray
Steady state distributions. If the Markov chain only has one recurrent class (num_rclasses=1), it will converge to an unique distribution in the long run, and thus steady_state is of (k, ) dimension; if the Markov chain has multiple recurrent classes (num_rclasses>1), there will be (num_rclasses) steady state distributions and steady_state will be of (num_rclasses, k) dimension.
- transitionsarray
(k, k), count of transitions between each state i and j.
Methods
__init__
(class_ids[, classes, ...])Attributes
- property mfpt¶
- property sojourn_time¶
- property steady_state¶