Esempio n. 1
0
 def test_model_names(self):
     garch = GARCH(2, 0, 0)
     assert_equal(garch.name, 'ARCH')
     garch = GARCH(2, 0, 2)
     assert_equal(garch.name, 'GARCH')
     garch = GARCH(2, 2, 2)
     assert_equal(garch.name, 'GJR-GARCH')
     garch = GARCH(1, 0, 0, power=1.0)
     assert_equal(garch.name, 'AVARCH')
     garch = GARCH(1, 0, 1, power=1.0)
     assert_equal(garch.name, 'AVGARCH')
     garch = GARCH(1, 1, 1, power=1.0)
     assert_equal(garch.name, 'TARCH/ZARCH')
     garch = GARCH(3, 0, 0, power=1.5)
     assert_equal(garch.name, 'Power ARCH (power: 1.5)')
     assert 'Power' in garch.__str__()
     garch = GARCH(1, 2, 1, power=1.5)
     assert_equal(garch.name, 'Asym. Power GARCH (power: 1.5)')
     garch = GARCH(2, 0, 2, power=1.5)
     assert_equal(garch.name, 'Power GARCH (power: 1.5)')
Esempio n. 2
0
 def test_model_names(self):
     garch = GARCH(2, 0, 0)
     assert_equal(garch.name, 'ARCH')
     garch = GARCH(2, 0, 2)
     assert_equal(garch.name, 'GARCH')
     garch = GARCH(2, 2, 2)
     assert_equal(garch.name, 'GJR-GARCH')
     garch = GARCH(1, 0, 0, power=1.0)
     assert_equal(garch.name, 'AVARCH')
     garch = GARCH(1, 0, 1, power=1.0)
     assert_equal(garch.name, 'AVGARCH')
     garch = GARCH(1, 1, 1, power=1.0)
     assert_equal(garch.name, 'TARCH/ZARCH')
     garch = GARCH(3, 0, 0, power=1.5)
     assert_equal(garch.name, 'Power ARCH (power: 1.5)')
     assert_true('Power' in garch.__str__())
     garch = GARCH(1, 2, 1, power=1.5)
     assert_equal(garch.name, 'Asym. Power GARCH (power: 1.5)')
     garch = GARCH(2, 0, 2, power=1.5)
     assert_equal(garch.name, 'Power GARCH (power: 1.5)')
Esempio n. 3
0
    def test_garch(self):
        garch = GARCH()

        sv = garch.starting_values(self.resids)
        assert_equal(sv.shape[0], garch.num_params)

        bounds = garch.bounds(self.resids)
        assert_equal(bounds[0], (0.0, 10.0 * np.mean(self.resids ** 2.0)))
        assert_equal(bounds[1], (0.0, 1.0))
        assert_equal(bounds[2], (0.0, 1.0))
        backcast = garch.backcast(self.resids)
        w = 0.94 ** np.arange(75)
        assert_almost_equal(backcast,
                            np.sum((self.resids[:75] ** 2) * (w / w.sum())))
        var_bounds = garch.variance_bounds(self.resids)
        parameters = np.array([.1, .1, .8])
        garch.compute_variance(parameters, self.resids, self.sigma2,
                               backcast, var_bounds)
        cond_var_direct = np.zeros_like(self.sigma2)
        rec.garch_recursion(parameters,
                            self.resids ** 2.0,
                            np.sign(self.resids),
                            cond_var_direct,
                            1, 0, 1, self.T, backcast, var_bounds)
        assert_allclose(self.sigma2, cond_var_direct)

        a, b = garch.constraints()
        a_target = np.vstack((np.eye(3), np.array([[0, -1.0, -1.0]])))
        b_target = np.array([0.0, 0.0, 0.0, -1.0])
        assert_array_equal(a, a_target)
        assert_array_equal(b, b_target)
        state = np.random.get_state()
        rng = Normal()
        sim_data = garch.simulate(parameters, self.T, rng.simulate([]))
        np.random.set_state(state)
        e = np.random.standard_normal(self.T + 500)
        initial_value = 1.0
        sigma2 = np.zeros(self.T + 500)
        data = np.zeros(self.T + 500)
        for t in range(self.T + 500):
            sigma2[t] = parameters[0]
            shock = initial_value if t == 0 else data[t - 1] ** 2.0
            sigma2[t] += parameters[1] * shock
            lagged_value = initial_value if t == 0 else sigma2[t - 1]
            sigma2[t] += parameters[2] * lagged_value
            data[t] = e[t] * np.sqrt(sigma2[t])
        data = data[500:]
        sigma2 = sigma2[500:]
        assert_almost_equal(data / sim_data[0], np.ones_like(data))
        assert_almost_equal(sigma2 / sim_data[1], np.ones_like(sigma2))

        names = garch.parameter_names()
        names_target = ['omega', 'alpha[1]', 'beta[1]']
        assert_equal(names, names_target)

        assert isinstance(garch.__str__(), str)
        txt = garch.__repr__()
        assert str(hex(id(garch))) in txt

        assert_equal(garch.name, 'GARCH')
        assert_equal(garch.num_params, 3)
        assert_equal(garch.power, 2.0)
        assert_equal(garch.p, 1)
        assert_equal(garch.o, 0)
        assert_equal(garch.q, 1)
Esempio n. 4
0
    def test_garch(self):
        garch = GARCH()

        sv = garch.starting_values(self.resids)
        assert_equal(sv.shape[0], garch.num_params)

        bounds = garch.bounds(self.resids)
        assert_equal(bounds[0], (0.0, 10.0 * np.mean(self.resids ** 2.0)))
        assert_equal(bounds[1], (0.0, 1.0))
        assert_equal(bounds[2], (0.0, 1.0))
        backcast = garch.backcast(self.resids)
        w = 0.94 ** np.arange(75)
        assert_almost_equal(backcast,
                            np.sum((self.resids[:75] ** 2) * (w / w.sum())))
        var_bounds = garch.variance_bounds(self.resids)
        parameters = np.array([.1, .1, .8])
        garch.compute_variance(parameters, self.resids, self.sigma2,
                               backcast, var_bounds)
        cond_var_direct = np.zeros_like(self.sigma2)
        rec.garch_recursion(parameters,
                            self.resids ** 2.0,
                            np.sign(self.resids),
                            cond_var_direct,
                            1, 0, 1, self.T, backcast, var_bounds)
        assert_allclose(self.sigma2, cond_var_direct)

        A, b = garch.constraints()
        A_target = np.vstack((np.eye(3), np.array([[0, -1.0, -1.0]])))
        b_target = np.array([0.0, 0.0, 0.0, -1.0])
        assert_array_equal(A, A_target)
        assert_array_equal(b, b_target)
        state = np.random.get_state()
        rng = Normal()
        sim_data = garch.simulate(parameters, self.T, rng.simulate([]))
        np.random.set_state(state)
        e = np.random.standard_normal(self.T + 500)
        initial_value = 1.0
        sigma2 = np.zeros(self.T + 500)
        data = np.zeros(self.T + 500)
        for t in range(self.T + 500):
            sigma2[t] = parameters[0]
            shock = initial_value if t == 0 else data[t - 1] ** 2.0
            sigma2[t] += parameters[1] * shock
            lagged_value = initial_value if t == 0 else sigma2[t - 1]
            sigma2[t] += parameters[2] * lagged_value
            data[t] = e[t] * np.sqrt(sigma2[t])
        data = data[500:]
        sigma2 = sigma2[500:]
        assert_almost_equal(data / sim_data[0], np.ones_like(data))
        assert_almost_equal(sigma2 / sim_data[1], np.ones_like(sigma2))

        names = garch.parameter_names()
        names_target = ['omega', 'alpha[1]', 'beta[1]']
        assert_equal(names, names_target)

        assert_true(isinstance(garch.__str__(), str))
        repr = garch.__repr__()
        assert_true(str(hex(id(garch))) in repr)

        assert_equal(garch.name, 'GARCH')
        assert_equal(garch.num_params, 3)
        assert_equal(garch.power, 2.0)
        assert_equal(garch.p, 1)
        assert_equal(garch.o, 0)
        assert_equal(garch.q, 1)