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) assert_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) assert_allclose(corrn, corr)
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))
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) assert_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) assert_allclose(relaxn, relax)
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))
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))
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))
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))
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) assert_allclose(exp, expn)
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))
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[int(self.dim / 2 - 1)] = 0.001 q[int(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).T 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).T 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_eigenvalues_reversible(self): P = self.bdc.transition_matrix() ev = eigvals(P) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] """reversible without given mu""" evn = eigenvalues(P, reversible=True) assert_allclose(ev, evn) """reversible with given mu""" evn = eigenvalues(P, reversible=True, mu=self.bdc.stationary_distribution()) assert_allclose(ev, evn) def test_eigenvectors_reversible(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, reversible=True) assert_allclose(np.dot(P,Rn),np.dot(Rn,Dn)) # left eigenvectors Ln = eigenvectors(P, right=False, reversible=True).T 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, reversible=True) assert_allclose(np.dot(P,Rn),np.dot(Rn,Dnk)) # left eigenvectors Ln = eigenvectors(P, right=False, k=self.k, reversible=True).T 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) """Reversible""" """k=None""" Rn, Dn, Ln = rdl_decomposition(P, norm='reversible') 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) """k is not None""" Rn, Dn, Ln = rdl_decomposition(P, norm='reversible', 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) def test_rdl_decomposition_rev(self): P = self.bdc.transition_matrix() mu = self.bdc.stationary_distribution() """norm='standard'""" """k=None""" Rn, Dn, Ln = rdl_decomposition(P, reversible=True, norm='standard') 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) """Standard l2-normalization of right eigenvectors except dominant one""" Yn = np.dot(Rn.T, Rn) assert_allclose(np.diag(Yn)[1:], 1.0) """k is not None""" Rn, Dn, Ln = rdl_decomposition(P, k=self.k, reversible=True, norm='standard') 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) """Standard l2-normalization of right eigenvectors except dominant one""" Yn = np.dot(Rn.T, Rn) assert_allclose(np.diag(Yn)[1:], 1.0) """norm='reversible'""" """k=None""" Rn, Dn, Ln = rdl_decomposition(P, reversible=True, norm='reversible') 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) """k is not None""" Rn, Dn, Ln = rdl_decomposition(P, reversible=True, norm='reversible', 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) def test_timescales(self): P = self.bdc.transition_matrix() ev = eigvals(P) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] ts = -1.0 / np.log(np.abs(ev)) """k=None""" tsn = timescales(P) assert_allclose(ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k) assert_allclose(ts[1:self.k], tsn[1:]) """tau=7""" """k=None""" tsn = timescales(P, tau=7) assert_allclose(7 * ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k, tau=7) assert_allclose(7 * ts[1:self.k], tsn[1:]) def test_timescales_rev(self): P_dense = self.bdc.transition_matrix() P = self.bdc.transition_matrix() mu = self.bdc.stationary_distribution() ev = eigvals(P_dense) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] ts = -1.0 / np.log(np.abs(ev)) tsn = timescales(P, reversible=True) assert_allclose(ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k, reversible=True) assert_allclose(ts[1:self.k], tsn[1:]) """k is not None, mu is not None""" tsn = timescales(P, k=self.k, reversible=True, mu=mu) assert_allclose(ts[1:self.k], tsn[1:]) """tau=7""" """k is not None""" tsn = timescales(P, k=self.k, tau=7, reversible=True) assert_allclose(7 * ts[1:self.k], tsn[1:])
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))
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""" self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0)) """Reversibility""" self.assertTrue(np.allclose(Ln.transpose(), mu[:,np.newaxis]*Rn)) """k is not None""" Rn, Dn, Ln=rdl_decomposition(P, norm='reversible', 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)) """Reversibility""" self.assertTrue(np.allclose(Ln.transpose(), mu[:,np.newaxis]*Rn)) def test_timescales(self): P=self.bdc.transition_matrix() ev=eigvals(P) """Sort with decreasing magnitude""" ev=ev[np.argsort(np.abs(ev))[::-1]] ts=-1.0/np.log(np.abs(ev)) """k=None""" tsn=timescales(P) self.assertTrue(np.allclose(ts[1:], tsn[1:])) """k is not None""" tsn=timescales(P, k=self.k) self.assertTrue(np.allclose(ts[1:self.k], tsn[1:])) """tau=7""" """k=None""" tsn=timescales(P, tau=7) self.assertTrue(np.allclose(7*ts[1:], tsn[1:])) """k is not None""" tsn=timescales(P, k=self.k, tau=7) self.assertTrue(np.allclose(7*ts[1:self.k], tsn[1:]))
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) assert_allclose(tsn, self.ts) assert_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) assert_allclose(tsn, tau * self.ts) assert_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) assert_allclose(tsn, self.ts) assert_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) assert_allclose(tsn, tau * self.ts) assert_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) assert_allclose(tsn, self.ts) assert_allclose(relax_ampn, relax_amp)
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) assert_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) assert_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) assert_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) assert_allclose(corrn, corr)
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) """Reversible""" """k=None""" Rn, Dn, Ln = rdl_decomposition(P, norm='reversible') 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) """k is not None""" Rn, Dn, Ln = rdl_decomposition(P, norm='reversible', 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) def test_timescales(self): P = self.bdc.transition_matrix() ev = eigvals(P) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] ts = -1.0 / np.log(np.abs(ev)) """k=None""" tsn = timescales(P) assert_allclose(ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k) assert_allclose(ts[1:self.k], tsn[1:]) """tau=7""" """k=None""" tsn = timescales(P, tau=7) assert_allclose(7 * ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k, tau=7) assert_allclose(7 * ts[1:self.k], tsn[1:])
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) assert_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) assert_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) assert_allclose(ev[0:self.k], evn) """k is not None and ncv is not None""" evn = eigenvalues(P, k=self.k, ncv=self.ncv) assert_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) assert_allclose(vals[np.newaxis, :] * Rn, P.dot(Rn)) Ln = eigenvectors(P, right=False, k=self.k) assert_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) assert_allclose(vals[np.newaxis, :] * Rn, P.dot(Rn)) Ln = eigenvectors(P, right=False, k=self.k, ncv=self.ncv) assert_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""" assert_allclose(P.dot(Rn), np.dot(Rn, Dn)) """Left-eigenvectors""" assert_allclose(P.transpose().dot(Ln.transpose()).transpose(), np.dot(Dn, Ln)) """Orthonormality""" assert_allclose(Xn, np.eye(self.k)) """Probability vector""" assert_allclose(np.sum(Ln[0, :]), 1.0) """k is not None, ncv is not None""" Rn, Dn, Ln = rdl_decomposition(P, k=self.k, ncv=self.ncv) Xn = np.dot(Ln, Rn) """Right-eigenvectors""" assert_allclose(P.dot(Rn), np.dot(Rn, Dn)) """Left-eigenvectors""" assert_allclose(P.transpose().dot(Ln.transpose()).transpose(), np.dot(Dn, Ln)) """Orthonormality""" assert_allclose(Xn, np.eye(self.k)) """Probability vector""" assert_allclose(np.sum(Ln[0, :]), 1.0) """Reversible""" """k=None""" with self.assertRaises(ValueError): Rn, Dn, Ln = rdl_decomposition(P, norm='reversible') """k is not None""" Rn, Dn, Ln = rdl_decomposition(P, k=self.k, norm='reversible') Xn = np.dot(Ln, Rn) """Right-eigenvectors""" assert_allclose(P.dot(Rn), np.dot(Rn, Dn)) """Left-eigenvectors""" assert_allclose(P.transpose().dot(Ln.transpose()).transpose(), np.dot(Dn, Ln)) """Orthonormality""" assert_allclose(Xn, np.eye(self.k)) """Probability vector""" assert_allclose(np.sum(Ln[0, :]), 1.0) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) """k is not None ncv is not None""" Rn, Dn, Ln = rdl_decomposition(P, k=self.k, norm='reversible', ncv=self.ncv) Xn = np.dot(Ln, Rn) """Right-eigenvectors""" assert_allclose(P.dot(Rn), np.dot(Rn, Dn)) """Left-eigenvectors""" assert_allclose(P.transpose().dot(Ln.transpose()).transpose(), np.dot(Dn, Ln)) """Orthonormality""" assert_allclose(Xn, np.eye(self.k)) """Probability vector""" assert_allclose(np.sum(Ln[0, :]), 1.0) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) def test_timescales(self): P_dense = self.bdc.transition_matrix() P = self.bdc.transition_matrix_sparse() ev = eigvals(P_dense) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] ts = -1.0 / np.log(np.abs(ev)) """k=None""" with self.assertRaises(ValueError): tsn = timescales(P) """k is not None""" tsn = timescales(P, k=self.k) assert_allclose(ts[1:self.k], tsn[1:]) """k is not None, ncv is not None""" tsn = timescales(P, k=self.k, ncv=self.ncv) assert_allclose(ts[1:self.k], tsn[1:]) """tau=7""" """k is not None""" tsn = timescales(P, k=self.k, tau=7) assert_allclose(7 * ts[1:self.k], tsn[1:])
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""" self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0)) """k is not None, ncv is not None""" Rn, Dn, Ln=rdl_decomposition(P, k=self.k, ncv=self.ncv) 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""" self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0)) """Reversible""" """k=None""" with self.assertRaises(ValueError): Rn, Dn, Ln=rdl_decomposition(P, norm='reversible') """k is not None""" Rn, Dn, Ln=rdl_decomposition(P, k=self.k, norm='reversible') 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""" self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0)) """Reversibility""" self.assertTrue(np.allclose(Ln.transpose(), mu[:,np.newaxis]*Rn)) """k is not None ncv is not None""" Rn, Dn, Ln=rdl_decomposition(P, k=self.k, norm='reversible', ncv=self.ncv) 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""" self.assertTrue(np.allclose(np.sum(Ln[0,:]), 1.0)) """Reversibility""" self.assertTrue(np.allclose(Ln.transpose(), mu[:,np.newaxis]*Rn)) def test_timescales(self): P_dense=self.bdc.transition_matrix() P=self.bdc.transition_matrix_sparse() ev=eigvals(P_dense) """Sort with decreasing magnitude""" ev=ev[np.argsort(np.abs(ev))[::-1]] ts=-1.0/np.log(np.abs(ev)) """k=None""" with self.assertRaises(ValueError): tsn=timescales(P) """k is not None""" tsn=timescales(P, k=self.k) self.assertTrue(np.allclose(ts[1:self.k], tsn[1:])) """k is not None, ncv is not None""" tsn=timescales(P, k=self.k, ncv=self.ncv) self.assertTrue(np.allclose(ts[1:self.k], tsn[1:])) """tau=7""" """k is not None""" tsn=timescales(P, k=self.k, tau=7) self.assertTrue(np.allclose(7*ts[1:self.k], tsn[1:]))
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() 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) assert_allclose(R, Rn) Ln = eigenvectors(P, right=False) assert_allclose(L, Ln) """k is not None""" Rn = eigenvectors(P, k=self.k) assert_allclose(R[:, 0:self.k], Rn) Ln = eigenvectors(P, right=False, k=self.k) assert_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""" 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) """Reversible""" """k=None""" Rn, Dn, Ln = rdl_decomposition(P, norm='reversible') 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) """k is not None""" Rn, Dn, Ln = rdl_decomposition(P, norm='reversible', 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) """Reversibility""" assert_allclose(Ln.transpose(), mu[:, np.newaxis] * Rn) def test_timescales(self): P = self.bdc.transition_matrix() ev = eigvals(P) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] ts = -1.0 / np.log(np.abs(ev)) """k=None""" tsn = timescales(P) assert_allclose(ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k) assert_allclose(ts[1:self.k], tsn[1:]) """tau=7""" """k=None""" tsn = timescales(P, tau=7) assert_allclose(7 * ts[1:], tsn[1:]) """k is not None""" tsn = timescales(P, k=self.k, tau=7) assert_allclose(7 * ts[1:self.k], tsn[1:])