当前位置: 首页>>代码示例>>Python>>正文


Python BirthDeathChain.transition_matrix方法代码示例

本文整理汇总了Python中birth_death_chain.BirthDeathChain.transition_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python BirthDeathChain.transition_matrix方法的具体用法?Python BirthDeathChain.transition_matrix怎么用?Python BirthDeathChain.transition_matrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在birth_death_chain.BirthDeathChain的用法示例。


在下文中一共展示了BirthDeathChain.transition_matrix方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestCommittorDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestCommittorDense(unittest.TestCase):
    def setUp(self):
        p = np.zeros(10)
        q = np.zeros(10)
        p[0:-1] = 0.5
        q[1:] = 0.5
        p[4] = 0.01
        q[6] = 0.1

        self.bdc = BirthDeathChain(q, p)

    def tearDown(self):
        pass

    def test_forward_comittor(self):
        P = self.bdc.transition_matrix()
        un = committor(P, [0, 1], [8, 9], forward=True)
        u = self.bdc.committor_forward(1, 8)
        assert_allclose(un, u)

    def test_backward_comittor(self):
        P = self.bdc.transition_matrix()
        un = committor(P, [0, 1], [8, 9], forward=False)
        u = self.bdc.committor_backward(1, 8)
        assert_allclose(un, u)
开发者ID:ismaelresp,项目名称:PyEMMA,代码行数:27,代码来源:test_committor.py

示例2: setUp

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
    def setUp(self):
        """Store state of the rng"""
        self.state = np.random.mtrand.get_state()

        """Reseed the rng to enforce 'deterministic' behavior"""
        np.random.mtrand.seed(42)
    
        """Meta-stable birth-death chain"""
        b = 2
        q = np.zeros(7)
        p = np.zeros(7)
        q[1:] = 0.5
        p[0:-1] = 0.5
        q[2] = 1.0-10**(-b)
        q[4] = 10**(-b)
        p[2] = 10**(-b)
        p[4] = 1.0-10**(-b)

        bdc = BirthDeathChain(q, p)
        P = bdc.transition_matrix()
        self.dtrajs = [generate_traj(P, 10000, start=0)]
        self.tau = 1

        """Estimate MSM"""
        self.C_MSM = cmatrix(self.dtrajs, self.tau, sliding=True).toarray()
        self.lcc_MSM = largest_connected_set(self.C_MSM)
        self.Ccc_MSM = connected_cmatrix(self.C_MSM, lcc=self.lcc_MSM)
        self.P_MSM = tmatrix(self.Ccc_MSM, reversible=True)
        self.mu_MSM = statdist(self.P_MSM)
        self.k = 3
        self.ts = timescales(self.P_MSM, k=self.k+1, tau=self.tau)[1:]
开发者ID:ismaelresp,项目名称:PyEMMA,代码行数:33,代码来源:msm_test.py

示例3: TestRelaxationDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestRelaxationDense(unittest.TestCase):
    def setUp(self):
        p=np.zeros(10)
        q=np.zeros(10)
        p[0:-1]=0.5
        q[1:]=0.5
        p[4]=0.01
        q[6]=0.1

        self.bdc=BirthDeathChain(q, p)
        
        self.mu = self.bdc.stationary_distribution()
        self.T = self.bdc.transition_matrix()

        """Test matrix-vector product against spectral decomposition"""        
        R, D, L=rdl_decomposition(self.T)
        self.L=L
        self.R=R
        self.ts=timescales(self.T)
        self.times=np.array([1, 5, 10, 20, 100])

        ev=np.diagonal(D)
        self.ev_t=ev[np.newaxis,:]**self.times[:,np.newaxis]

        self.k=4

        """Observable"""
        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        self.obs=obs1

        """Initial distribution"""
        w0=np.zeros(10)
        w0[0:4]=0.25
        self.p0=w0     

    def test_relaxation(self):        
        """k=None"""
        relax_amp=np.dot(self.p0, self.R)*np.dot(self.L, self.obs)
        relax=np.dot(self.ev_t, relax_amp)        
        relaxn=relaxation(self.T, self.p0, self.obs, times=self.times)        
        self.assertTrue(np.allclose(relaxn, relax))        
        
        """k=4"""
        k=self.k
        relax_amp=np.dot(self.p0, self.R[:,0:k])*np.dot(self.L[0:k,:], self.obs)
        relax=np.dot(self.ev_t[:,0:k], relax_amp)        
        relaxn=relaxation(self.T, self.p0, self.obs, k=k, times=self.times) 
        self.assertTrue(np.allclose(relaxn, relax))
开发者ID:greglever,项目名称:PyEMMA,代码行数:52,代码来源:test_fingerprints.py

示例4: TestAssessmentDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestAssessmentDense(unittest.TestCase):
    def setUp(self):
        p = np.zeros(10)
        q = np.zeros(10)
        p[0:-1] = 0.5
        q[1:] = 0.5
        p[4] = 0.01
        q[6] = 0.1

        self.bdc = BirthDeathChain(q, p)
        self.T = self.bdc.transition_matrix()
        self.mu = self.bdc.stationary_distribution()

        self.A = create_rate_matrix()

    def test_IsRateMatrix(self):
        self.assert_(is_rate_matrix(self.A), 'A should be a rate matrix')

        # manipulate matrix so it isn't a rate matrix any more
        self.A[0][0] = 3
        self.assertFalse(is_rate_matrix(self.A), 'matrix is not a rate matrix')

    def test_IsReversible(self):
        # create a reversible matrix
        self.assertTrue(is_reversible(self.T, self.mu),
                        "T should be reversible")

    def test_is_transition_matrix(self):
        self.assertTrue(is_transition_matrix(self.T))

        """Larger test-case to prevent too restrictive tolerance settings"""
        X = np.random.random((2000, 2000))
        Tlarge = X / X.sum(axis=1)[:, np.newaxis]
        self.assertTrue(is_transition_matrix(Tlarge))

    def test_is_connected(self):
        self.assertTrue(is_connected(self.T))
        self.assertTrue(is_connected(self.T, directed=False))
开发者ID:ismaelresp,项目名称:PyEMMA,代码行数:40,代码来源:test_assessment.py

示例5: TestExpectation

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestExpectation(unittest.TestCase):
    def setUp(self):
        p=np.zeros(10)
        q=np.zeros(10)
        p[0:-1]=0.5
        q[1:]=0.5
        p[4]=0.01
        q[6]=0.1

        self.bdc=BirthDeathChain(q, p)
        
        self.mu = self.bdc.stationary_distribution()
        self.T = self.bdc.transition_matrix()

        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1

        self.obs1=obs1

    def test_expectation(self):
        exp=np.dot(self.mu, self.obs1)
        expn=expectation(self.T, self.obs1)
        self.assertTrue(np.allclose(exp, expn)) 
开发者ID:greglever,项目名称:PyEMMA,代码行数:26,代码来源:test_fingerprints.py

示例6: TestAssessmentDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestAssessmentDense(unittest.TestCase):
    
    def setUp(self):
        p = np.zeros(10)
        q = np.zeros(10)
        p[0:-1] = 0.5
        q[1:] = 0.5
        p[4] = 0.01
        q[6] = 0.1

        self.bdc = BirthDeathChain(q, p)
        self.T = self.bdc.transition_matrix()
        self.mu = self.bdc.stationary_distribution()

        self.A = create_rate_matrix()

    def test_IsRateMatrix(self):
        self.assert_(is_rate_matrix(self.A), \
                     'A should be a rate matrix')
        
        # manipulate matrix so it isn't a rate matrix any more
        self.A[0][0] = 3
        self.assertFalse(is_rate_matrix(self.A), \
                        'matrix is not a rate matrix')
    
    def test_IsReversible(self):
        # create a reversible matrix
        self.assertTrue(is_reversible(self.T, self.mu),
                        "T should be reversible")

    def test_is_transition_matrix(self):
        self.assertTrue(is_transition_matrix(self.T))

    def test_is_connected(self):
        self.assertTrue(is_connected(self.T))
        self.assertTrue(is_connected(self.T, directed=False))
开发者ID:greglever,项目名称:PyEMMA,代码行数:38,代码来源:test_assessment.py

示例7: TestDecomposition

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestDecomposition(unittest.TestCase):
    def setUp(self):
        self.dim=100
        self.k=10
        self.ncv=40
        
        """Set up meta-stable birth-death chain"""
        p=np.zeros(self.dim)
        p[0:-1]=0.5
        
        q=np.zeros(self.dim)
        q[1:]=0.5

        p[self.dim/2-1]=0.001
        q[self.dim/2+1]=0.001
        
        self.bdc=BirthDeathChain(q, p)

    def test_statdist_decomposition(self):
        P=self.bdc.transition_matrix_sparse()
        mu=self.bdc.stationary_distribution()
        mun=stationary_distribution_from_eigenvector(P, ncv=self.ncv)
        self.assertTrue(np.allclose(mu, mun))

    def test_statdist_iteration(self):
        P=self.bdc.transition_matrix_sparse()
        mu=self.bdc.stationary_distribution()
        mun=stationary_distribution_from_backward_iteration(P)
        self.assertTrue(np.allclose(mu, mun))

    def test_eigenvalues(self):
        P=self.bdc.transition_matrix()
        P_dense=self.bdc.transition_matrix()
        ev=eigvals(P_dense)
        """Sort with decreasing magnitude"""
        ev=ev[np.argsort(np.abs(ev))[::-1]]
        
        """k=None"""
        with self.assertRaises(ValueError):
            evn=eigenvalues(P)
        
        """k is not None"""
        evn=eigenvalues(P, k=self.k)
        self.assertTrue(np.allclose(ev[0:self.k], evn))

        """k is not None and ncv is not None"""
        evn=eigenvalues(P, k=self.k, ncv=self.ncv)
        self.assertTrue(np.allclose(ev[0:self.k], evn))

    def test_eigenvectors(self):
        P_dense=self.bdc.transition_matrix()
        P=self.bdc.transition_matrix_sparse()
        ev, L, R=eig(P_dense, left=True, right=True)
        ind=np.argsort(np.abs(ev))[::-1]
        ev=ev[ind]
        R=R[:,ind]
        L=L[:,ind]        
        vals=ev[0:self.k]

        """k=None"""
        with self.assertRaises(ValueError):
            Rn=eigenvectors(P)

        with self.assertRaises(ValueError):
            Ln=eigenvectors(P, right=False)

        """k is not None"""
        Rn=eigenvectors(P, k=self.k)        
        self.assertTrue(np.allclose(vals[np.newaxis,:]*Rn, P.dot(Rn)))

        Ln=eigenvectors(P, right=False, k=self.k)
        self.assertTrue(np.allclose(P.transpose().dot(Ln), vals[np.newaxis,:]*Ln))

        """k is not None and ncv is not None"""
        Rn=eigenvectors(P, k=self.k, ncv=self.ncv)        
        self.assertTrue(np.allclose(vals[np.newaxis,:]*Rn, P.dot(Rn)))

        Ln=eigenvectors(P, right=False, k=self.k, ncv=self.ncv)
        self.assertTrue(np.allclose(P.transpose().dot(Ln), vals[np.newaxis,:]*Ln))

    def test_rdl_decomposition(self):
        P=self.bdc.transition_matrix_sparse()
        mu=self.bdc.stationary_distribution()

        """Non-reversible"""

        """k=None"""
        with self.assertRaises(ValueError):
            Rn, Dn, Ln=rdl_decomposition(P)        

        """k is not None"""
        Rn, Dn, Ln=rdl_decomposition(P, k=self.k)        
        Xn=np.dot(Ln, Rn)
        """Right-eigenvectors"""
        self.assertTrue(np.allclose(P.dot(Rn), np.dot(Rn, Dn)))    
        """Left-eigenvectors"""
        self.assertTrue(np.allclose(P.transpose().dot(Ln.transpose()).transpose(), np.dot(Dn, Ln)))               
        """Orthonormality"""
        self.assertTrue(np.allclose(Xn, np.eye(self.k)))
        """Probability vector"""
#.........这里部分代码省略.........
开发者ID:greglever,项目名称:PyEMMA,代码行数:103,代码来源:decomposition_test.py

示例8: TestFingerprintDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestFingerprintDense(unittest.TestCase):
    def setUp(self):
        p=np.zeros(10)
        q=np.zeros(10)
        p[0:-1]=0.5
        q[1:]=0.5
        p[4]=0.01
        q[6]=0.1

        self.bdc=BirthDeathChain(q, p)
        
        self.mu = self.bdc.stationary_distribution()
        self.T = self.bdc.transition_matrix()
        R, D, L=rdl_decomposition(self.T)
        self.L=L
        self.R=R
        self.ts=timescales(self.T)
        self.times=np.array([1, 5, 10, 20])

        ev=np.diagonal(D)
        self.ev_t=ev[np.newaxis,:]**self.times[:,np.newaxis]

        self.k=4
        self.tau=7.5

        """Observables"""
        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        obs2[9] = 1

        self.obs1=obs1
        self.obs2=obs2

        """Initial vector for relaxation"""
        w0=np.zeros(10)
        w0[0:4]=0.25
        self.p0=w0     

    def test_fingerprint_correlation(self):
        """Autocorrelation"""

        """k=None, tau=1"""
        acorr_amp=np.dot(self.mu*self.obs1, self.R)*np.dot(self.L,self.obs1)
        tsn, acorr_ampn=fingerprint_correlation(self.T, self.obs1)
        self.assertTrue(np.allclose(tsn, self.ts))
        self.assertTrue(np.allclose(acorr_ampn, acorr_amp))

        """k=None, tau=7.5"""
        tau=self.tau
        tsn, acorr_ampn=fingerprint_correlation(self.T, self.obs1, tau=tau)
        self.assertTrue(np.allclose(tsn, tau*self.ts))
        self.assertTrue(np.allclose(acorr_ampn, acorr_amp))

        """k=4, tau=1"""
        k=self.k
        acorr_amp=np.dot(self.mu*self.obs1, self.R[:,0:k])*np.dot(self.L[0:k,:],self.obs1)
        tsn, acorr_ampn=fingerprint_correlation(self.T, self.obs1, k=k)
        self.assertTrue(np.allclose(tsn, self.ts[0:k]))
        self.assertTrue(np.allclose(acorr_ampn, acorr_amp))

        """k=4, tau=7.5"""
        tau=self.tau
        tsn, acorr_ampn=fingerprint_correlation(self.T, self.obs1, k=k, tau=tau)
        self.assertTrue(np.allclose(tsn, tau*self.ts[0:k]))
        self.assertTrue(np.allclose(acorr_ampn, acorr_amp))

        """Cross-correlation"""

        """k=None, tau=1"""
        corr_amp=np.dot(self.mu*self.obs1, self.R)*np.dot(self.L,self.obs2)
        tsn, corr_ampn=fingerprint_correlation(self.T, self.obs1, obs2=self.obs2)
        self.assertTrue(np.allclose(tsn, self.ts))
        self.assertTrue(np.allclose(corr_ampn, corr_amp))

        """k=None, tau=7.5"""
        tau=self.tau
        tsn, corr_ampn=fingerprint_correlation(self.T, self.obs1, obs2=self.obs2, tau=tau)
        self.assertTrue(np.allclose(tsn, tau*self.ts))
        self.assertTrue(np.allclose(corr_ampn, corr_amp))

        """k=4, tau=1"""
        k=self.k
        corr_amp=np.dot(self.mu*self.obs1, self.R[:,0:k])*np.dot(self.L[0:k,:],self.obs2)
        tsn, corr_ampn=fingerprint_correlation(self.T, self.obs1, obs2=self.obs2, k=k)
        self.assertTrue(np.allclose(tsn, self.ts[0:k]))
        self.assertTrue(np.allclose(corr_ampn, corr_amp))

        """k=4, tau=7.5"""
        tau=self.tau
        tsn, corr_ampn=fingerprint_correlation(self.T, self.obs1, obs2=self.obs2, k=k, tau=tau)
        self.assertTrue(np.allclose(tsn, tau*self.ts[0:k]))
        self.assertTrue(np.allclose(corr_ampn, corr_amp))

    def test_fingerprint_relaxation(self):
        one_vec=np.ones(self.T.shape[0])

        """k=None"""
#.........这里部分代码省略.........
开发者ID:greglever,项目名称:PyEMMA,代码行数:103,代码来源:test_fingerprints.py

示例9: TestCorrelationDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestCorrelationDense(unittest.TestCase):
    def setUp(self):
        p=np.zeros(10)
        q=np.zeros(10)
        p[0:-1]=0.5
        q[1:]=0.5
        p[4]=0.01
        q[6]=0.1

        self.bdc=BirthDeathChain(q, p)
        
        self.mu = self.bdc.stationary_distribution()
        self.T = self.bdc.transition_matrix()
        R, D, L=rdl_decomposition(self.T, norm='reversible')
        self.L=L
        self.R=R
        self.ts=timescales(self.T)
        self.times=np.array([1, 5, 10, 20, 100])

        ev=np.diagonal(D)
        self.ev_t=ev[np.newaxis,:]**self.times[:,np.newaxis]

        self.k=4

        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        obs2[9] = 1

        self.obs1=obs1
        self.obs2=obs2
        self.one_vec=np.ones(10)

    def test_correlation(self):
        """Auto-correlation"""

        """k=None"""
        acorr_amp=np.dot(self.mu*self.obs1, self.R)*np.dot(self.L, self.obs1)
        acorr=np.dot(self.ev_t, acorr_amp)
        acorrn=correlation(self.T, self.obs1, times=self.times)
        self.assertTrue(np.allclose(acorrn, acorr))

        """k=4"""
        k=self.k
        acorr_amp=np.dot(self.mu*self.obs1, self.R[:,0:k])*np.dot(self.L[0:k,:], self.obs1)
        acorr=np.dot(self.ev_t[:,0:k], acorr_amp)
        acorrn=correlation(self.T, self.obs1, times=self.times, k=k)
        self.assertTrue(np.allclose(acorrn, acorr))              
    
        """Cross-correlation"""

        """k=None"""
        corr_amp=np.dot(self.mu*self.obs1, self.R)*np.dot(self.L, self.obs2)
        corr=np.dot(self.ev_t, corr_amp)    
        corrn=correlation(self.T, self.obs1, obs2=self.obs2, times=self.times)
        self.assertTrue(np.allclose(corrn, corr))

        """k=4"""
        k=self.k
        corr_amp=np.dot(self.mu*self.obs1, self.R[:,0:k])*np.dot(self.L[0:k,:], self.obs2)
        corr=np.dot(self.ev_t[:,0:k], corr_amp)
        corrn=correlation(self.T, self.obs1, obs2=self.obs2, times=self.times, k=k)
        self.assertTrue(np.allclose(corrn, corr))   
开发者ID:greglever,项目名称:PyEMMA,代码行数:67,代码来源:test_fingerprints.py

示例10: TestDecompositionDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestDecompositionDense(unittest.TestCase):
    def setUp(self):
        self.dim = 100
        self.k = 10

        """Set up meta-stable birth-death chain"""
        p = np.zeros(self.dim)
        p[0:-1] = 0.5

        q = np.zeros(self.dim)
        q[1:] = 0.5

        p[self.dim / 2 - 1] = 0.001
        q[self.dim / 2 + 1] = 0.001

        self.bdc = BirthDeathChain(q, p)

    def test_statdist(self):
        P = self.bdc.transition_matrix()
        mu = self.bdc.stationary_distribution()
        mun = stationary_distribution(P)
        assert_allclose(mu, mun)

    def test_eigenvalues(self):
        P = self.bdc.transition_matrix()
        ev = eigvals(P)
        """Sort with decreasing magnitude"""
        ev = ev[np.argsort(np.abs(ev))[::-1]]

        """k=None"""
        evn = eigenvalues(P)
        assert_allclose(ev, evn)

        """k is not None"""
        evn = eigenvalues(P, k=self.k)
        assert_allclose(ev[0:self.k], evn)

    def test_eigenvectors(self):
        P = self.bdc.transition_matrix()

        # k==None
        ev = eigvals(P)
        ev = ev[np.argsort(np.abs(ev))[::-1]]
        Dn = np.diag(ev)

        # right eigenvectors
        Rn = eigenvectors(P)
        assert_allclose(np.dot(P,Rn),np.dot(Rn,Dn))
        # left eigenvectors
        Ln = eigenvectors(P, right=False)
        assert_allclose(np.dot(Ln.T,P),np.dot(Dn,Ln.T))
        # orthogonality
        Xn = np.dot(Ln.T, Rn)
        di = np.diag_indices(Xn.shape[0])
        Xn[di] = 0.0
        assert_allclose(Xn,0)

        # k!=None
        Dnk = Dn[:,0:self.k][0:self.k,:]
        # right eigenvectors
        Rn = eigenvectors(P, k=self.k)
        assert_allclose(np.dot(P,Rn),np.dot(Rn,Dnk))
        # left eigenvectors
        Ln = eigenvectors(P, right=False, k=self.k)
        assert_allclose(np.dot(Ln.T,P),np.dot(Dnk,Ln.T))
        # orthogonality
        Xn = np.dot(Ln.T, Rn)
        di = np.diag_indices(self.k)
        Xn[di] = 0.0
        assert_allclose(Xn,0)

    def test_rdl_decomposition(self):
        P = self.bdc.transition_matrix()
        mu = self.bdc.stationary_distribution()

        """Non-reversible"""

        """k=None"""
        Rn, Dn, Ln = rdl_decomposition(P)
        Xn = np.dot(Ln, Rn)
        """Right-eigenvectors"""
        assert_allclose(np.dot(P, Rn), np.dot(Rn, Dn))
        """Left-eigenvectors"""
        assert_allclose(np.dot(Ln, P), np.dot(Dn, Ln))
        """Orthonormality"""
        assert_allclose(Xn, np.eye(self.dim))
        """Probability vector"""
        assert_allclose(np.sum(Ln[0, :]), 1.0)

        """k is not None"""
        Rn, Dn, Ln = rdl_decomposition(P, k=self.k)
        Xn = np.dot(Ln, Rn)
        """Right-eigenvectors"""
        assert_allclose(np.dot(P, Rn), np.dot(Rn, Dn))
        """Left-eigenvectors"""
        assert_allclose(np.dot(Ln, P), np.dot(Dn, Ln))
        """Orthonormality"""
        assert_allclose(Xn, np.eye(self.k))
        """Probability vector"""
        assert_allclose(np.sum(Ln[0, :]), 1.0)
#.........这里部分代码省略.........
开发者ID:ismaelresp,项目名称:PyEMMA,代码行数:103,代码来源:test_decomposition.py

示例11: TestDecompositionDense

# 需要导入模块: from birth_death_chain import BirthDeathChain [as 别名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix [as 别名]
class TestDecompositionDense(unittest.TestCase):
    def setUp(self):
        self.dim=100
        self.k=10
        
        """Set up meta-stable birth-death chain"""
        p=np.zeros(self.dim)
        p[0:-1]=0.5
        
        q=np.zeros(self.dim)
        q[1:]=0.5

        p[self.dim/2-1]=0.001
        q[self.dim/2+1]=0.001
        
        self.bdc=BirthDeathChain(q, p)

    def test_statdist(self):
        P=self.bdc.transition_matrix()
        mu=self.bdc.stationary_distribution()
        mun=stationary_distribution(P)
        self.assertTrue(np.allclose(mu, mun))

    def test_eigenvalues(self):
        P=self.bdc.transition_matrix()
        ev=eigvals(P)
        """Sort with decreasing magnitude"""
        ev=ev[np.argsort(np.abs(ev))[::-1]]
        
        """k=None"""
        evn=eigenvalues(P)
        self.assertTrue(np.allclose(ev, evn))
        
        """k is not None"""
        evn=eigenvalues(P, k=self.k)
        self.assertTrue(np.allclose(ev[0:self.k], evn))

    def test_eigenvectors(self):
        P=self.bdc.transition_matrix()
        ev, L, R=eig(P, left=True, right=True)
        ind=np.argsort(np.abs(ev))[::-1]
        R=R[:,ind]
        L=L[:,ind]        

        """k=None"""
        Rn=eigenvectors(P)
        self.assertTrue(np.allclose(R, Rn))

        Ln=eigenvectors(P, right=False)
        self.assertTrue(np.allclose(L, Ln))

        """k is not None"""
        Rn=eigenvectors(P, k=self.k)
        self.assertTrue(np.allclose(R[:,0:self.k], Rn))

        Ln=eigenvectors(P, right=False, k=self.k)
        self.assertTrue(np.allclose(L[:,0:self.k], Ln))

    def test_rdl_decomposition(self):
        P=self.bdc.transition_matrix()
        mu=self.bdc.stationary_distribution()

        """Non-reversible"""

        """k=None"""
        Rn, Dn, Ln=rdl_decomposition(P)        
        Xn=np.dot(Ln, Rn)
        """Right-eigenvectors"""
        self.assertTrue(np.allclose(np.dot(P, Rn), np.dot(Rn, Dn)))
        """Left-eigenvectors"""
        self.assertTrue(np.allclose(np.dot(Ln, P), np.dot(Dn, Ln)))
        """Orthonormality"""
        self.assertTrue(np.allclose(Xn, np.eye(self.dim)))
        """Probability vector"""
        self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0))

        """k is not None"""
        Rn, Dn, Ln=rdl_decomposition(P, k=self.k)        
        Xn=np.dot(Ln, Rn)               
        """Right-eigenvectors"""
        self.assertTrue(np.allclose(np.dot(P, Rn), np.dot(Rn, Dn)))
        """Left-eigenvectors"""
        self.assertTrue(np.allclose(np.dot(Ln, P), np.dot(Dn, Ln)))
        """Orthonormality"""
        self.assertTrue(np.allclose(Xn, np.eye(self.k)))
        """Probability vector"""
        self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0))

        """Reversible"""

        """k=None"""
        Rn, Dn, Ln=rdl_decomposition(P, norm='reversible')        
        Xn=np.dot(Ln, Rn)
        """Right-eigenvectors"""
        self.assertTrue(np.allclose(np.dot(P, Rn), np.dot(Rn, Dn)))
        """Left-eigenvectors"""
        self.assertTrue(np.allclose(np.dot(Ln, P), np.dot(Dn, Ln)))
        """Orthonormality"""
        self.assertTrue(np.allclose(Xn, np.eye(self.dim)))
        """Probability vector"""
#.........这里部分代码省略.........
开发者ID:greglever,项目名称:PyEMMA,代码行数:103,代码来源:test_decomposition.py


注:本文中的birth_death_chain.BirthDeathChain.transition_matrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。