ergodic.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:nelpy 作者: nelpy 项目源码 文件源码
def steady_state(P):
    """
    Calculates the steady state probability vector for a regular Markov
    transition matrix P
    Parameters
    ----------
    P        : matrix (kxk)
               an ergodic Markov transition probability matrix
    Returns
    -------
    implicit : matrix (kx1)
               steady state distribution
    Examples
    --------
    Taken from Kemeny and Snell. [1]_ Land of Oz example where the states are
    Rain, Nice and Snow - so there is 25 percent chance that if it
    rained in Oz today, it will snow tomorrow, while if it snowed today in
    Oz there is a 50 percent chance of snow again tomorrow and a 25
    percent chance of a nice day (nice, like when the witch with the monkeys
    is melting).
    >>> import numpy as np
    >>> p=np.matrix([[.5, .25, .25],[.5,0,.5],[.25,.25,.5]])
    >>> steady_state(p)
    matrix([[ 0.4],
            [ 0.2],
            [ 0.4]])
    Thus, the long run distribution for Oz is to have 40 percent of the
    days classified as Rain, 20 percent as Nice, and 40 percent as Snow
    (states are mutually exclusive).
    """

    v,d=la.eig(np.transpose(P))

    # for a regular P maximum eigenvalue will be 1
    mv=max(v)
    # find its position
    i=v.tolist().index(mv)

    # normalize eigenvector corresponding to the eigenvalue 1
    return d[:,i]/sum(d[:,i])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号