本文整理匯總了Python中birth_death_chain.BirthDeathChain.transition_matrix_sparse方法的典型用法代碼示例。如果您正苦於以下問題:Python BirthDeathChain.transition_matrix_sparse方法的具體用法?Python BirthDeathChain.transition_matrix_sparse怎麽用?Python BirthDeathChain.transition_matrix_sparse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類birth_death_chain.BirthDeathChain
的用法示例。
在下文中一共展示了BirthDeathChain.transition_matrix_sparse方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestCommittorSparse
# 需要導入模塊: from birth_death_chain import BirthDeathChain [as 別名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix_sparse [as 別名]
class TestCommittorSparse(unittest.TestCase):
def setUp(self):
p = np.zeros(100)
q = np.zeros(100)
p[0:-1] = 0.5
q[1:] = 0.5
p[49] = 0.01
q[51] = 0.1
self.bdc = BirthDeathChain(q, p)
def tearDown(self):
pass
def test_forward_comittor(self):
P = self.bdc.transition_matrix_sparse()
un = committor(P, range(10), range(90, 100), forward=True)
u = self.bdc.committor_forward(9, 90)
assert_allclose(un, u)
def test_backward_comittor(self):
P = self.bdc.transition_matrix_sparse()
un = committor(P, range(10), range(90, 100), forward=False)
u = self.bdc.committor_backward(9, 90)
assert_allclose(un, u)
示例2: TestCorrelationSparse
# 需要導入模塊: from birth_death_chain import BirthDeathChain [as 別名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix_sparse [as 別名]
class TestCorrelationSparse(unittest.TestCase):
def setUp(self):
self.k=4
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_sparse()
R, D, L=rdl_decomposition(self.T, k=self.k)
self.L=L
self.R=R
self.ts=timescales(self.T, k=self.k)
self.times=np.array([1, 5, 10, 20, 100])
ev=np.diagonal(D)
self.ev_t=ev[np.newaxis,:]**self.times[:,np.newaxis]
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"""
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, k=self.k, times=self.times)
self.assertTrue(np.allclose(acorrn, acorr))
"""Cross-correlation"""
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, k=self.k, times=self.times)
self.assertTrue(np.allclose(corrn, corr))
示例3: TestRelaxationSparse
# 需要導入模塊: from birth_death_chain import BirthDeathChain [as 別名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix_sparse [as 別名]
class TestRelaxationSparse(unittest.TestCase):
def setUp(self):
self.k=4
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_sparse()
"""Test matrix-vector product against spectral decomposition"""
R, D, L=rdl_decomposition(self.T, k=self.k)
self.L=L
self.R=R
self.ts=timescales(self.T, k=self.k)
self.times=np.array([1, 5, 10, 20, 100])
ev=np.diagonal(D)
self.ev_t=ev[np.newaxis,:]**self.times[:,np.newaxis]
"""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):
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, k=self.k, times=self.times)
self.assertTrue(np.allclose(relaxn, relax))
示例4: TestDecomposition
# 需要導入模塊: from birth_death_chain import BirthDeathChain [as 別名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix_sparse [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"""
#.........這裏部分代碼省略.........
示例5: TestFingerprintSparse
# 需要導入模塊: from birth_death_chain import BirthDeathChain [as 別名]
# 或者: from birth_death_chain.BirthDeathChain import transition_matrix_sparse [as 別名]
class TestFingerprintSparse(unittest.TestCase):
def setUp(self):
self.k=4
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_sparse()
R, D, L=rdl_decomposition(self.T, k=self.k)
self.L=L
self.R=R
self.ts=timescales(self.T, k=self.k)
self.times=np.array([1, 5, 10, 20])
ev=np.diagonal(D)
self.ev_t=ev[np.newaxis,:]**self.times[:,np.newaxis]
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=4, tau=1"""
k=self.k
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, k=k)
self.assertTrue(np.allclose(tsn, self.ts))
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))
self.assertTrue(np.allclose(acorr_ampn, acorr_amp))
"""Cross-correlation"""
"""k=4, tau=1"""
k=self.k
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, k=k)
self.assertTrue(np.allclose(tsn, self.ts))
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))
self.assertTrue(np.allclose(corr_ampn, corr_amp))
def test_fingerprint_relaxation(self):
one_vec=np.ones(self.T.shape[0])
relax_amp=np.dot(self.p0, self.R)*np.dot(self.L, self.obs1)
tsn, relax_ampn=fingerprint_relaxation(self.T, self.p0, self.obs1, k=self.k)
self.assertTrue(np.allclose(tsn, self.ts))
self.assertTrue(np.allclose(relax_ampn, relax_amp))