pysal.explore.giddy.markov.Markov

class pysal.explore.giddy.markov.Markov(class_ids, classes=None)[source]

Classic Markov transition matrices.

Parameters:
class_ids : array

(n, t), one row per observation, one column recording the state of each observation, with as many columns as time periods.

classes : array

(k, 1), all different classes (bins) of the matrix.

Examples

>>> import numpy as np
>>> from pysal.explore.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)
>>> 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])

US nominal per capita income 48 states 81 years 1929-2009

>>> import pysal.lib
>>> import pysal.viz.mapclassify as mc
>>> f = pysal.lib.io.open(pysal.lib.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)
>>> 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)
>>> 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:
p : array

(k, k), transition probability matrix.

steady_state : array

(k, ), ergodic distribution.

transitions : array

(k, k), count of transitions between each state i and j.

__init__(class_ids, classes=None)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(class_ids[, classes]) Initialize self.

Attributes

steady_state