Beispiel #1
0
class TestLinearStateSpace(unittest.TestCase):

    def setUp(self):
        # Initial Values
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss = LinearStateSpace(A, C, G, mu_0=mu_0)

    def tearDown(self):
        del self.ss

    def test_stationarity(self):
        vals = self.ss.stationary_distributions(max_iter=1000, tol=1e-9)
        ssmux, ssmuy, sssigx, sssigy = vals

        self.assertTrue(abs(ssmux - ssmuy) < 2e-8)
        self.assertTrue(abs(sssigx - sssigy) < 2e-8)
        self.assertTrue(abs(ssmux) < 2e-8)
        self.assertTrue(abs(sssigx - self.ss.C**2/(1 - self.ss.A**2)) < 2e-8)

    def test_replicate(self):
        xval, yval = self.ss.replicate(T=100, num_reps=5000)

        assert_allclose(xval, yval)
        self.assertEqual(xval.size, 5000)
        self.assertLessEqual(abs(np.mean(xval)), .05)
Beispiel #2
0
    def setUp(self):
        # Initial Values
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss = LinearStateSpace(A, C, G, mu_0=mu_0)
Beispiel #3
0
class TestLinearStateSpace(unittest.TestCase):

    def setUp(self):
        # Initial Values
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss = LinearStateSpace(A, C, G, mu_0=mu_0)

    def tearDown(self):
        del self.ss

    def test_stationarity(self):
        vals = self.ss.stationary_distributions(max_iter=1000, tol=1e-9)
        ssmux, ssmuy, sssigx, sssigy = vals

        self.assertTrue(abs(ssmux - ssmuy) < 2e-8)
        self.assertTrue(abs(sssigx - sssigy) < 2e-8)
        self.assertTrue(abs(ssmux) < 2e-8)
        self.assertTrue(abs(sssigx - self.ss.C**2/(1 - self.ss.A**2)) < 2e-8)

    def test_simulate(self):
        ss = self.ss

        sim = ss.simulate(ts_length=250)
        for arr in sim:
            self.assertTrue(len(arr[0])==250)

    def test_simulate_with_seed(self):
        ss = self.ss

        xval, yval = ss.simulate(ts_length=5, random_state=5)
        expected_output = np.array([0.75 , 0.73456137, 0.6812898, 0.76876387,
                                    .71772107])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)

    def test_replicate(self):
        xval, yval = self.ss.replicate(T=100, num_reps=5000)

        assert_allclose(xval, yval)
        self.assertEqual(xval.size, 5000)
        self.assertLessEqual(abs(np.mean(xval)), .05)

    def test_replicate_with_seed(self):
        xval, yval = self.ss.replicate(T=100, num_reps=5, random_state=5)
        expected_output = np.array([0.06871204, 0.06937119, -0.1478022,
                                    0.23841252, -0.06823762])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)
Beispiel #4
0
class TestLinearStateSpace(unittest.TestCase):

    def setUp(self):
        # Initial Values
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss = LinearStateSpace(A, C, G, mu_0=mu_0)

    def tearDown(self):
        del self.ss

    def test_stationarity(self):
        vals = self.ss.stationary_distributions(max_iter=1000, tol=1e-9)
        ssmux, ssmuy, sssigx, sssigy = vals

        self.assertTrue(abs(ssmux - ssmuy) < 2e-8)
        self.assertTrue(abs(sssigx - sssigy) < 2e-8)
        self.assertTrue(abs(ssmux) < 2e-8)
        self.assertTrue(abs(sssigx - self.ss.C**2/(1 - self.ss.A**2)) < 2e-8)

    def test_simulate(self):
        ss = self.ss

        sim = ss.simulate(ts_length=250)
        for arr in sim:
            self.assertTrue(len(arr[0])==250)

    def test_simulate_with_seed(self):
        ss = self.ss

        xval, yval = ss.simulate(ts_length=5, random_state=5)
        expected_output = np.array([0.75 , 0.73456137, 0.6812898, 0.76876387,
                                    .71772107])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)

    def test_replicate(self):
        xval, yval = self.ss.replicate(T=100, num_reps=5000)

        assert_allclose(xval, yval)
        self.assertEqual(xval.size, 5000)
        self.assertLessEqual(abs(np.mean(xval)), .05)

    def test_replicate_with_seed(self):
        xval, yval = self.ss.replicate(T=100, num_reps=5, random_state=5)
        expected_output = np.array([0.06871204, 0.06937119, -0.1478022,
                                    0.23841252, -0.06823762])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)
Beispiel #5
0
    def setUp(self):
        # Initial Values
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss = LinearStateSpace(A, C, G, mu_0=mu_0)
Beispiel #6
0
    def setUp(self):
        # Example 1
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss1 = LinearStateSpace(A, C, G, mu_0=mu_0)

        # Example 2
        ρ1 = 0.5
        ρ2 = 0.3
        α = 0.5

        A = np.array([[ρ1, ρ2, α], [1, 0, 0], [0, 0, 1]])
        C = np.array([[1], [0], [0]])
        G = np.array([[1, 0, 0]])
        mu_0 = [0.5, 0.5, 1]

        self.ss2 = LinearStateSpace(A, C, G, mu_0=mu_0)
Beispiel #7
0
    def setUp(self):
        # Initial Values
        self.A = np.array([[.95, 0], [0., .95]])
        self.C = np.eye(2) * np.sqrt(0.5)
        self.G = np.eye(2) * .5
        self.H = np.eye(2) * np.sqrt(0.2)

        self.Q = np.dot(self.C, self.C.T)
        self.R = np.dot(self.H, self.H.T)

        ss = LinearStateSpace(self.A, self.C, self.G, self.H)

        self.kf = Kalman(ss)
Beispiel #8
0
def test_non_square_A():
    A = np.zeros((1, 2))
    C = np.zeros((1, 1))
    G = np.zeros((1, 1))

    LinearStateSpace(A, C, G)
Beispiel #9
0
class TestLinearStateSpace(unittest.TestCase):

    def setUp(self):
        # Example 1
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss1 = LinearStateSpace(A, C, G, mu_0=mu_0)

        # Example 2
        ρ1 = 0.5
        ρ2 = 0.3
        α = 0.5

        A = np.array([[ρ1, ρ2, α], [1, 0, 0], [0, 0, 1]])
        C = np.array([[1], [0], [0]])
        G = np.array([[1, 0, 0]])
        mu_0 = [0.5, 0.5, 1]

        self.ss2 = LinearStateSpace(A, C, G, mu_0=mu_0)

    def tearDown(self):
        del self.ss1
        del self.ss2

    def test_stationarity(self):
        vals = self.ss1.stationary_distributions()
        ssmux, ssmuy, sssigx, sssigy, sssigyx = vals

        self.assertTrue(abs(ssmux - ssmuy) < 2e-8)
        self.assertTrue(abs(sssigx - sssigy) < 2e-8)
        self.assertTrue(abs(ssmux) < 2e-8)
        self.assertTrue(abs(sssigx - self.ss1.C**2/(1 - self.ss1.A**2)) < 2e-8)
        self.assertTrue(abs(sssigyx - self.ss1.G @ sssigx) < 2e-8)

        vals = self.ss2.stationary_distributions()
        ssmux, ssmuy, sssigx, sssigy, sssigyx = vals

        assert_allclose(ssmux.flatten(), np.array([2.5, 2.5, 1]))
        assert_allclose(ssmuy.flatten(), np.array([2.5]))
        assert_allclose(sssigx, self.ss2.A @ sssigx @ self.ss2.A.T + self.ss2.C @ self.ss2.C.T)
        assert_allclose(sssigy, self.ss2.G @ sssigx @ self.ss2.G.T)
        assert_allclose(sssigyx, self.ss2.G @ sssigx)

    def test_simulate(self):
        ss = self.ss1

        sim = ss.simulate(ts_length=250)
        for arr in sim:
            self.assertTrue(len(arr[0])==250)

    def test_simulate_with_seed(self):
        ss = self.ss1

        xval, yval = ss.simulate(ts_length=5, random_state=5)
        expected_output = np.array([0.75 , 0.73456137, 0.6812898, 0.76876387,
                                    .71772107])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)

    def test_replicate(self):
        xval, yval = self.ss1.replicate(T=100, num_reps=5000)

        assert_allclose(xval, yval)
        self.assertEqual(xval.size, 5000)
        self.assertLessEqual(abs(np.mean(xval)), .05)

    def test_replicate_with_seed(self):
        xval, yval = self.ss1.replicate(T=100, num_reps=5, random_state=5)
        expected_output = np.array([0.06871204, 0.06937119, -0.1478022,
                                    0.23841252, -0.06823762])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)
Beispiel #10
0
    def whitener_lss(self):
        r"""
        This function takes the linear state space system
        that is an input to the Kalman class and it converts
        that system to the time-invariant whitener represenation
        given by

        .. math::

            \tilde{x}_{t+1}^* = \tilde{A} \tilde{x} + \tilde{C} v
            a = \tilde{G} \tilde{x}

        where

        .. math::

            \tilde{x}_t = [x+{t}, \hat{x}_{t}, v_{t}]

        and

        .. math::

            \tilde{A} =
            \begin{bmatrix}
            A  & 0    & 0  \\
            KG & A-KG & KH \\
            0  & 0    & 0 \\
            \end{bmatrix}

        .. math::

            \tilde{C} =
            \begin{bmatrix}
            C & 0 \\
            0 & 0 \\
            0 & I \\
            \end{bmatrix}

        .. math::

            \tilde{G} =
            \begin{bmatrix}
            G & -G & H \\
            \end{bmatrix}

        with :math:`A, C, G, H` coming from the linear state space system
        that defines the Kalman instance

        Returns
        -------
        whitened_lss : LinearStateSpace
            This is the linear state space system that represents
            the whitened system
        """
        K = self.K_infinity

        # Get the matrix sizes
        n, k, m, l = self.ss.n, self.ss.k, self.ss.m, self.ss.l
        A, C, G, H = self.ss.A, self.ss.C, self.ss.G, self.ss.H

        Atil = np.vstack([np.hstack([A, np.zeros((n, n)), np.zeros((n, l))]),
                          np.hstack([np.dot(K, G),
                                     A-np.dot(K, G),
                                     np.dot(K, H)]),
                          np.zeros((l, 2*n + l))])

        Ctil = np.vstack([np.hstack([C, np.zeros((n, l))]),
                          np.zeros((n, m+l)),
                          np.hstack([np.zeros((l, m)), np.eye(l)])])

        Gtil = np.hstack([G, -G, H])

        whitened_lss = LinearStateSpace(Atil, Ctil, Gtil)
        self.whitened_lss = whitened_lss

        return whitened_lss
Beispiel #11
0
class TestLinearStateSpace:

    def setup(self):
        # Example 1
        A = .95
        C = .05
        G = 1.
        mu_0 = .75

        self.ss1 = LinearStateSpace(A, C, G, mu_0=mu_0)

        # Example 2
        ρ1 = 0.5
        ρ2 = 0.3
        α = 0.5

        A = np.array([[ρ1, ρ2, α], [1, 0, 0], [0, 0, 1]])
        C = np.array([[1], [0], [0]])
        G = np.array([[1, 0, 0]])
        mu_0 = [0.5, 0.5, 1]

        self.ss2 = LinearStateSpace(A, C, G, mu_0=mu_0)

    def tearDown(self):
        del self.ss1
        del self.ss2

    def test_stationarity(self):
        vals = self.ss1.stationary_distributions()
        ssmux, ssmuy, sssigx, sssigy, sssigyx = vals

        assert_(abs(ssmux - ssmuy) < 2e-8)
        assert_(abs(sssigx - sssigy) < 2e-8)
        assert_(abs(ssmux) < 2e-8)
        assert_(abs(sssigx - self.ss1.C**2/(1 - self.ss1.A**2)) < 2e-8)
        assert_(abs(sssigyx - self.ss1.G @ sssigx) < 2e-8)

        vals = self.ss2.stationary_distributions()
        ssmux, ssmuy, sssigx, sssigy, sssigyx = vals

        assert_allclose(ssmux.flatten(), np.array([2.5, 2.5, 1]))
        assert_allclose(ssmuy.flatten(), np.array([2.5]))
        assert_allclose(
            sssigx,
            self.ss2.A @ sssigx @ self.ss2.A.T + self.ss2.C @ self.ss2.C.T
        )
        assert_allclose(sssigy, self.ss2.G @ sssigx @ self.ss2.G.T)
        assert_allclose(sssigyx, self.ss2.G @ sssigx)

    def test_simulate(self):
        ss = self.ss1

        sim = ss.simulate(ts_length=250)
        for arr in sim:
            assert_(len(arr[0]) == 250)

    def test_simulate_with_seed(self):
        ss = self.ss1

        xval, yval = ss.simulate(ts_length=5, random_state=5)
        expected_output = np.array([0.75, 0.69595649, 0.78269723, 0.73095776,
                                    0.69989036])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)

    def test_replicate(self):
        xval, yval = self.ss1.replicate(T=100, num_reps=5000)

        assert_allclose(xval, yval)
        assert_(xval.size == 5000)
        assert_(abs(np.mean(xval)) <= .05)

    def test_replicate_with_seed(self):
        xval, yval = self.ss1.replicate(T=100, num_reps=5, random_state=5)
        expected_output = np.array([0.10498898, 0.02892168, 0.04915998,
                                    0.18568489, 0.04541764])

        assert_allclose(xval[0], expected_output)
        assert_allclose(yval[0], expected_output)
Beispiel #12
0
    def whitener_lss(self):
        r"""
        This function takes the linear state space system
        that is an input to the Kalman class and it converts
        that system to the time-invariant whitener represenation
        given by

            \tilde{x}_{t+1}^* = \tilde{A} \tilde{x} + \tilde{C} v
            a = \tilde{G} \tilde{x}

        where

            \tilde{x}_t = [x+{t}, \hat{x}_{t}, v_{t}]

        and

            \tilde{A} = [A  0    0
                         KG A-KG KH
                         0  0    0]

            \tilde{C} = [C 0
                         0 0
                         0 I]

            \tilde{G} = [G -G H]

        with A, C, G, H coming from the linear state space system 
        that defines the Kalman instance


        Returns
        -------
        whitened_lss : LinearStateSpace
            This is the linear state space system that represents
            the whitened system
        """
        # Check for steady state Sigma and K
        if self.K_infinity is None:
            Sig, K = self.stationary_values()
            self.Sigma_infinity = Sig
            self.K_infinity = K
        else:
            K = self.K_infinity

        # Get the matrix sizes
        n, k, m, l = self.ss.n, self.ss.k, self.ss.m, self.ss.l
        A, C, G, H = self.ss.A, self.ss.C, self.ss.G, self.ss.H

        Atil = np.vstack([
            np.hstack([A, np.zeros((n, n)),
                       np.zeros((n, l))]),
            np.hstack([dot(K, G), A - dot(K, G),
                       dot(K, H)]),
            np.zeros((l, 2 * n + l))
        ])

        Ctil = np.vstack([
            np.hstack([C, np.zeros((n, l))]),
            np.zeros((n, m + l)),
            np.hstack([np.zeros((l, m)), np.eye(l)])
        ])

        Gtil = np.hstack([G, -G, H])

        whitened_lss = LinearStateSpace(Atil, Ctil, Gtil)
        self.whitened_lss = whitened_lss

        return whitened_lss