giddy.util.fill_empty_diagonals

giddy.util.fill_empty_diagonals(p)[source]

Assign 1 to diagonal elements which fall in rows full of 0s to ensure the transition probability matrix is a stochastic one. Currently implemented for two- and three-dimensional transition probability matrices.

Parameters:
parray

(k, k), an ergodic/non-ergodic Markov transition probability matrix.

Returns:
p_temparray

Matrix without rows full of 0 transition probabilities. (stochastic matrix)

Examples

>>> import numpy as np
>>> from giddy.util import fill_empty_diagonals
>>> p2 = np.array([[.5, .5, 0], [.3, .7, 0], [0, 0, 0]])
>>> fill_empty_diagonals(p2)
array([[0.5, 0.5, 0. ],
       [0.3, 0.7, 0. ],
       [0. , 0. , 1. ]])
>>> p3 = np.array([[[0.5, 0.5, 0. ], [0.3, 0.7, 0. ], [0. , 0. , 0. ]],
...  [[0. , 0. , 0. ], [0.3, 0.7, 0. ], [0. , 0. , 0. ]]])
>>> p_new = fill_empty_diagonals(p3)
>>> p_new[1]
array([[1. , 0. , 0. ],
       [0.3, 0.7, 0. ],
       [0. , 0. , 1. ]])