Esempio n. 1
0
    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
Esempio n. 2
0
    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)
Esempio n. 3
0
class TestRelaxation(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_decomp(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_decomp(self.T, self.p0, self.obs, k=self.k, times=self.times)        
        self.assertTrue(np.allclose(relaxn, relax))               

    def test_relaxation_matvec(self):  
        times=self.times
        P=self.T.toarray()
        relax=np.zeros(len(times))
        for i in range(len(times)):
            P_t=np.linalg.matrix_power(P, times[i])
            relax[i]=np.dot(self.p0, np.dot(P_t, self.obs))              
        relaxn=relaxation_matvec(self.T, self.p0, self.obs, times=self.times)
        self.assertTrue(np.allclose(relaxn, relax))      

    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))               
Esempio n. 4
0
    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()
Esempio n. 5
0
    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)
Esempio n. 6
0
    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     
Esempio n. 7
0
class ReversibleTest(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()
    
    def testIsReversible(self):
        # create a reversible matrix
        self.assertTrue(assessment.is_reversible(self.T, self.mu), "T should be reversible")
Esempio n. 8
0
class ReversibleTest(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()

    def testIsReversible(self):
        # create a reversible matrix
        self.assertTrue(assessment.is_reversible(self.T, self.mu),
                        "T should be reversible")
Esempio n. 9
0
    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()
Esempio n. 10
0
    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
Esempio n. 11
0
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))
Esempio n. 12
0
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)
Esempio n. 13
0
    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     
Esempio n. 14
0
class TestCorrelation(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_decomp(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_decomp(self.T,
                                    self.obs1,
                                    k=self.k,
                                    times=self.times)
        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_decomp(self.T,
                                   self.obs1,
                                   obs2=self.obs2,
                                   k=self.k,
                                   times=self.times)
        assert_allclose(corrn, corr)

    def test_correlation_matvec(self):
        """Auto-correlation"""
        times = self.times
        P = self.T.toarray()
        acorr = np.zeros(len(times))
        for i in range(len(times)):
            P_t = np.linalg.matrix_power(P, times[i])
            acorr[i] = np.dot(self.mu * self.obs1, np.dot(P_t, self.obs1))
        acorrn = correlation_matvec(self.T, self.obs1, times=self.times)
        assert_allclose(acorrn, acorr)
        """Cross-correlation"""
        corr = np.zeros(len(times))
        for i in range(len(times)):
            P_t = np.linalg.matrix_power(P, times[i])
            corr[i] = np.dot(self.mu * self.obs1, np.dot(P_t, self.obs2))
        corrn = correlation_matvec(self.T,
                                   self.obs1,
                                   obs2=self.obs2,
                                   times=self.times)
        assert_allclose(corrn, corr)

    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)
Esempio n. 15
0
class TestCorrelation(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_decomp(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_decomp(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_decomp(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_decomp(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_decomp(self.T, self.obs1, obs2=self.obs2, times=self.times, k=k)
        self.assertTrue(np.allclose(corrn, corr))

    def test_correlation_matvec(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_matvec(self.T, self.obs1, 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_matvec(self.T, self.obs1, obs2=self.obs2, times=self.times)
        self.assertTrue(np.allclose(corrn, corr))

    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))   
Esempio n. 16
0
class TestCorrelation(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_decomp(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_decomp(self.T, self.obs1, k=self.k, times=self.times)
        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_decomp(self.T, self.obs1, obs2=self.obs2, k=self.k, times=self.times)
        self.assertTrue(np.allclose(corrn, corr))        

    def test_correlation_matvec(self):
        """Auto-correlation"""
        times=self.times
        P=self.T.toarray()
        acorr=np.zeros(len(times))
        for i in range(len(times)):
            P_t=np.linalg.matrix_power(P, times[i])
            acorr[i]=np.dot(self.mu*self.obs1, np.dot(P_t, self.obs1))              
        acorrn=correlation_matvec(self.T, self.obs1, times=self.times)
        self.assertTrue(np.allclose(acorrn, acorr))
    
        """Cross-correlation"""
        corr=np.zeros(len(times))
        for i in range(len(times)):
            P_t=np.linalg.matrix_power(P, times[i])
            corr[i]=np.dot(self.mu*self.obs1, np.dot(P_t, self.obs2))        
        corrn=correlation_matvec(self.T, self.obs1, obs2=self.obs2, times=self.times)
        self.assertTrue(np.allclose(corrn, corr))

    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))
Esempio n. 17
0
class TestCorrelations(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()

    def test_time_correlation(self):
        """
        since we have no overlap between observations and do not propagate the
        operator, the correlation is zero.
        P^0 = diag(1)
        """
        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        obs2[9] = 1
        time = 0
        corr = correlations.time_correlation_direct_by_mtx_vec_prod(
            self.T, self.mu, obs1, obs2, time)
        self.assertEqual(corr, 0)

        time = 100
        corr = correlations.time_correlation_direct_by_mtx_vec_prod(
            self.T, self.mu, obs1, obs2, time)
        self.assertGreater(corr, 0.0, "correlation should be > 0.")

    @unittest.SkipTest
    def test_time_auto_correlation(self):
        #TODO:
        """test with obs2 = obs1, to test autocorrelation"""
        obs1 = np.zeros(10)
        obs1[0] = 1
        time = 100
        print correlations.time_correlation_direct_by_mtx_vec_prod(self.T,
                                                                   self.mu,
                                                                   obs1,
                                                                   time=time)

    @unittest.SkipTest
    def test_time_corr2(self):
        obs1 = np.zeros(10)
        obs1[5:] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        time = 2
        print correlations.time_correlation_direct_by_mtx_vec_prod(self.T,
                                                                   self.mu,
                                                                   obs1,
                                                                   obs2,
                                                                   time=time)

    def test_time_correlations(self):
        """
        tests whether the outcome of the wrapper time_correlations_direct
        is equivalent to calls to time_correlation_direct_by_mtx_vec_prod with same time set.
        """
        obs1 = np.zeros(10)
        obs1[3:5] = 1
        obs2 = np.zeros(10)
        obs2[4:8] = 1
        times = [1, 2, 20, 40, 100, 200, 1000]
        # calculate without wrapper
        corr_expected = np.empty(len(times))
        i = 0
        for t in times:
            corr_expected[
                i] = correlations.time_correlation_direct_by_mtx_vec_prod(
                    self.T, self.mu, obs1, obs2, t)
            i += 1
        # calculate with wrapper
        corr_actual = correlations.time_correlations_direct(
            self.T, self.mu, obs1, obs2, times)

        self.assertTrue(
            np.allclose(corr_expected, corr_actual),
            "correlations differ:\n%s\n%s" % (corr_expected, corr_actual))

    def test_time_relaxation_stat(self):
        """
            start with stationary distribution, so increasing time should 
            not change relaxation any more.
        """
        obs = np.zeros(10)
        obs[9] = 1
        p0 = self.mu
        c1 = correlations.time_relaxation_direct_by_mtx_vec_prod(self.T,
                                                                 p0,
                                                                 obs,
                                                                 time=1)
        c1000 = correlations.time_relaxation_direct_by_mtx_vec_prod(self.T,
                                                                    p0,
                                                                    obs,
                                                                    time=1000)
        self.assertAlmostEqual(
            c1,
            c1000,
            msg="relaxation should be same, since we start in equilibrium.")

    def test_time_relaxation(self):
        obs = np.zeros(10)
        obs[9] = 1

        p0 = np.zeros(10) * 1. / 10

        # compute by hand
        # p0 P^k obs
        P1000 = np.linalg.matrix_power(self.T, 1000)
        expected = np.dot(np.dot(p0, P1000), obs)
        result = correlations.time_relaxation_direct_by_mtx_vec_prod(self.T,
                                                                     p0,
                                                                     obs,
                                                                     time=1000)
        self.assertAlmostEqual(expected, result)

    def test_time_relaxations(self):
        obs = np.zeros(10)
        obs[9] = 1

        p0 = np.zeros(10) * 1. / 10
        times = [1, 100, 1000]
        expected = []
        for t in times:
            expected.append(
                correlations.time_relaxation_direct_by_mtx_vec_prod(
                    self.T, p0, obs, t))

        result = correlations.time_relaxations_direct(self.T, p0, obs, times)

        assert_allclose(expected, result)
Esempio n. 18
0
class TestCorrelations(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()

    def test_time_correlation(self):
        """
        since we have no overlap between observations and do not propagate the
        operator, the correlation is zero.
        P^0 = diag(1)
        """
        obs1 = np.zeros(10)
        obs1[0] = 1
        obs1[1] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        obs2[9] = 1
        time = 0
        corr = correlations.time_correlation_direct_by_mtx_vec_prod(self.T, self.mu, obs1, obs2, time)
        self.assertEqual(corr, 0)

        time = 100
        corr = correlations.time_correlation_direct_by_mtx_vec_prod(self.T, self.mu, obs1, obs2, time)
        self.assertGreater(corr, 0.0, "correlation should be > 0.")

    @unittest.SkipTest
    def test_time_auto_correlation(self):
        """test with obs2 = obs1, to test autocorrelation"""
        obs1 = np.zeros(10)
        obs1[0] = 1
        time = 100
        print correlations.time_correlation_direct_by_mtx_vec_prod(self.T, self.mu, obs1, time=time)

    @unittest.SkipTest
    def test_time_corr2(self):
        obs1 = np.zeros(10)
        obs1[5:] = 1
        obs2 = np.zeros(10)
        obs2[8] = 1
        time = 2
        print correlations.time_correlation_direct_by_mtx_vec_prod(self.T, self.mu, obs1, obs2, time=time)

    def test_time_correlations(self):
        """
        tests whether the outcome of the wrapper time_correlations_direct
        is equivalent to calls to time_correlation_direct_by_mtx_vec_prod with same time set.
        """
        obs1 = np.zeros(10)
        obs1[3:5] = 1
        obs2 = np.zeros(10)
        obs2[4:8] = 1
        times = [1, 2, 20, 40, 100, 200, 1000]
        # calculate without wrapper
        corr_expected = np.empty(len(times))
        i = 0
        for t in times:
            corr_expected[i] = correlations.time_correlation_direct_by_mtx_vec_prod(self.T, self.mu, obs1, obs2, t)
            i += 1
        # calculate with wrapper
        corr_actual = correlations.time_correlations_direct(self.T, self.mu, obs1, obs2, times)

        self.assertTrue(np.allclose(corr_expected, corr_actual),
                        "correlations differ:\n%s\n%s" % (corr_expected, corr_actual))

    def test_time_relaxation_stat(self):
        """
            start with stationary distribution, so increasing time should 
            not change relaxation any more.
        """
        obs = np.zeros(10)
        obs[9] = 1
        p0 = self.mu
        c1 = correlations.time_relaxation_direct_by_mtx_vec_prod(self.T, p0, obs, time=1)
        c1000 = correlations.time_relaxation_direct_by_mtx_vec_prod(self.T, p0, obs, time=1000)
        self.assertAlmostEqual(c1, c1000,
                               msg="relaxation should be same, since we start in equilibrium.")

    def test_time_relaxation(self):
        obs = np.zeros(10)
        obs[9] = 1

        p0 = np.zeros(10) * 1. / 10

        # compute by hand
        # p0 P^k obs
        P1000 = np.linalg.matrix_power(self.T, 1000)
        expected = np.dot(np.dot(p0, P1000), obs)
        result = correlations.time_relaxation_direct_by_mtx_vec_prod(self.T, p0, obs, time=1000)
        self.assertAlmostEqual(expected, result)

    def test_time_relaxations(self):
        obs = np.zeros(10)
        obs[9] = 1

        p0 = np.zeros(10) * 1. / 10
        times = [1, 100, 1000]
        expected = []
        for t in times:
            expected.append(correlations.time_relaxation_direct_by_mtx_vec_prod(self.T, p0, obs, t))

        result = correlations.time_relaxations_direct(self.T, p0, obs, times)

        assert_allclose(expected, result)
Esempio n. 19
0
class TestRelaxation(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_decomp(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_decomp(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_decomp(self.T,
                                   self.p0,
                                   self.obs,
                                   k=k,
                                   times=self.times)
        assert_allclose(relaxn, relax)

    def test_relaxation_matvec(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_matvec(self.T, self.p0, self.obs, times=self.times)
        assert_allclose(relaxn, relax)

    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)
Esempio n. 20
0
class TestFingerprint(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)
        assert_allclose(tsn, self.ts)
        assert_allclose(acorr_ampn, acorr_amp)
        """k=None, tau=7.5"""
        tau = self.tau
        tsn, acorr_ampn = fingerprint_correlation(self.T, self.obs1, tau=tau)
        assert_allclose(tsn, tau * self.ts)
        assert_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)
        assert_allclose(tsn, self.ts[0:k])
        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[0:k])
        assert_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)
        assert_allclose(tsn, self.ts)
        assert_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)
        assert_allclose(tsn, tau * self.ts)
        assert_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)
        assert_allclose(tsn, self.ts[0:k])
        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[0:k])
        assert_allclose(corr_ampn, corr_amp)

    def test_fingerprint_relaxation(self):
        one_vec = np.ones(self.T.shape[0])
        """k=None"""
        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)
        assert_allclose(tsn, self.ts)
        assert_allclose(relax_ampn, relax_amp)
        """k=4"""
        k = self.k
        relax_amp = np.dot(self.p0, self.R[:, 0:k]) * np.dot(
            self.L[0:k, :], self.obs1)
        tsn, relax_ampn = fingerprint_relaxation(self.T,
                                                 self.p0,
                                                 self.obs1,
                                                 k=k)
        assert_allclose(tsn, self.ts[0:k])
        assert_allclose(relax_ampn, relax_amp)

    def test_fingerprint(self):
        """k=None"""
        amp = np.dot(self.p0 * self.obs1, self.R) * np.dot(self.L, self.obs2)
        tsn, ampn = fingerprint(self.T, self.obs1, obs2=self.obs2, p0=self.p0)
        assert_allclose(tsn, self.ts)
        assert_allclose(ampn, amp)
        """k=4"""
        k = self.k
        amp = np.dot(self.p0 * self.obs1, self.R[:, 0:k]) * np.dot(
            self.L[0:k, :], self.obs2)
        tsn, ampn = fingerprint(self.T,
                                self.obs1,
                                obs2=self.obs2,
                                p0=self.p0,
                                k=k)
        assert_allclose(tsn, self.ts[0:k])
        assert_allclose(ampn, amp)
Esempio n. 21
0
class TestFingerprint(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"""
        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)        
        self.assertTrue(np.allclose(tsn, self.ts))
        self.assertTrue(np.allclose(relax_ampn, relax_amp))        

        """k=4"""
        k=self.k
        relax_amp=np.dot(self.p0, self.R[:,0:k])*np.dot(self.L[0:k,:], self.obs1)
        tsn, relax_ampn=fingerprint_relaxation(self.T, self.p0, self.obs1, k=k)        
        self.assertTrue(np.allclose(tsn, self.ts[0:k]))        
        self.assertTrue(np.allclose(relax_ampn, relax_amp))        

    def test_fingerprint(self):
        """k=None"""
        amp=np.dot(self.p0*self.obs1, self.R)*np.dot(self.L, self.obs2)
        tsn, ampn=fingerprint(self.T, self.obs1, obs2=self.obs2, p0=self.p0)
        self.assertTrue(np.allclose(tsn, self.ts))
        self.assertTrue(np.allclose(ampn, amp))        

        """k=4"""
        k=self.k
        amp=np.dot(self.p0*self.obs1, self.R[:,0:k])*np.dot(self.L[0:k,:], self.obs2)
        tsn, ampn=fingerprint(self.T, self.obs1, obs2=self.obs2, p0=self.p0, k=k)
        self.assertTrue(np.allclose(tsn, self.ts[0:k]))
        self.assertTrue(np.allclose(ampn, amp))  
Esempio n. 22
0
class TestRelaxation(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_decomp(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_decomp(self.T,
                                   self.p0,
                                   self.obs,
                                   k=self.k,
                                   times=self.times)
        assert_allclose(relaxn, relax)

    def test_relaxation_matvec(self):
        times = self.times
        P = self.T.toarray()
        relax = np.zeros(len(times))
        for i in range(len(times)):
            P_t = np.linalg.matrix_power(P, times[i])
            relax[i] = np.dot(self.p0, np.dot(P_t, self.obs))
        relaxn = relaxation_matvec(self.T, self.p0, self.obs, times=self.times)
        assert_allclose(relaxn, relax)

    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)
        assert_allclose(relaxn, relax)
Esempio n. 23
0
class TestRelaxation(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_decomp(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_decomp(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_decomp(self.T, self.p0, self.obs, k=k, times=self.times)
        assert_allclose(relaxn, relax)

    def test_relaxation_matvec(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_matvec(self.T, self.p0, self.obs, times=self.times)
        assert_allclose(relaxn, relax)

    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)
Esempio n. 24
0
class TestCorrelation(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_decomp(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_decomp(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_decomp(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_decomp(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_decomp(self.T,
                                   self.obs1,
                                   obs2=self.obs2,
                                   times=self.times,
                                   k=k)
        assert_allclose(corrn, corr)

    def test_correlation_matvec(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_matvec(self.T, self.obs1, 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_matvec(self.T,
                                   self.obs1,
                                   obs2=self.obs2,
                                   times=self.times)
        assert_allclose(corrn, corr)

    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)