def sojourn_time(p):
p = np.asarray(p)<EOL>pii = p.diagonal()<EOL>if not (<NUM_LIT:1> - pii).all():<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>return <NUM_LIT:1> / (<NUM_LIT:1> - pii)<EOL>
Calculate sojourn time based on a given transition probability matrix.

Parameters
----------
p        : array
           (k, k), a Markov transition probability matrix.

Returns
-------
         : array
           (k, ), sojourn times. Each element is the expected time a Markov
           chain spends in each states before leaving that state.

Notes
-----
Refer to :cite:`Ibe2009` for more details on sojourn times for Markov
chains.

Examples
--------
>>> from giddy.markov import sojourn_time
>>> import numpy as np
>>> p = np.array([[.5, .25, .25], [.5, 0, .5], [.25, .25, .5]])
>>> sojourn_time(p)
array([2., 1., 2.])