def test_ewma(self): ewma = EWMAVariance() sv = ewma.starting_values(self.resids) assert_equal(sv.shape[0], ewma.num_params) bounds = ewma.bounds(self.resids) assert_equal(len(bounds), 0) var_bounds = ewma.variance_bounds(self.resids) backcast = ewma.backcast(self.resids) parameters = np.array([]) names = ewma.parameter_names() names_target = [] assert_equal(names, names_target) ewma.compute_variance(parameters, self.resids, self.sigma2, backcast, var_bounds) cond_var_direct = np.zeros_like(self.sigma2) parameters = np.array([0.0, 0.06, 0.94]) rec.garch_recursion(parameters, self.resids ** 2.0, np.sign(self.resids), cond_var_direct, 1, 0, 1, self.T, backcast, var_bounds) # sigma3 = np.zeros_like(self.sigma2) # sigma3[0] = backcast # for t in range(1,self.T): # sigma3[t] = 0.94 * sigma3[t-1] + 0.06 * self.resids[t-1]**2.0 assert_allclose(self.sigma2 / cond_var_direct, np.ones_like(self.sigma2)) A, b = ewma.constraints() A_target = np.empty((0, 0)) b_target = np.empty((0,)) assert_array_equal(A, A_target) assert_array_equal(b, b_target) state = np.random.get_state() rng = Normal() sim_data = ewma.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) sigma2[0] = initial_value data[0] = np.sqrt(initial_value) for t in range(1, self.T + 500): sigma2[t] = 0.94 * sigma2[t - 1] + 0.06 * data[t - 1] ** 2.0 data[t] = e[t] * np.sqrt(sigma2[t]) data = data[500:] sigma2 = sigma2[500:] assert_almost_equal(data - sim_data[0] + 1.0, np.ones_like(data)) assert_almost_equal(sigma2 / sim_data[1], np.ones_like(sigma2)) assert_equal(ewma.num_params, 0) assert_equal(ewma.name, 'EWMA/RiskMetrics')
def test_ewma(self): ewma = EWMAVariance() sv = ewma.starting_values(self.resids) assert_equal(sv.shape[0], ewma.num_params) bounds = ewma.bounds(self.resids) assert_equal(len(bounds), 0) var_bounds = ewma.variance_bounds(self.resids) backcast = ewma.backcast(self.resids) parameters = np.array([]) names = ewma.parameter_names() names_target = [] assert_equal(names, names_target) ewma.compute_variance(parameters, self.resids, self.sigma2, backcast, var_bounds) cond_var_direct = np.zeros_like(self.sigma2) parameters = np.array([0.0, 0.06, 0.94]) 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, np.ones_like(self.sigma2)) a, b = ewma.constraints() a_target = np.empty((0, 0)) b_target = np.empty((0,)) assert_array_equal(a, a_target) assert_array_equal(b, b_target) state = self.rng.get_state() rng = Normal() rng.random_state.set_state(state) sim_data = ewma.simulate(parameters, self.T, rng.simulate([])) self.rng.set_state(state) e = self.rng.standard_normal(self.T + 500) initial_value = 1.0 sigma2 = np.zeros(self.T + 500) data = np.zeros(self.T + 500) sigma2[0] = initial_value data[0] = np.sqrt(initial_value) for t in range(1, self.T + 500): sigma2[t] = 0.94 * sigma2[t - 1] + 0.06 * data[t - 1] ** 2.0 data[t] = e[t] * np.sqrt(sigma2[t]) data = data[500:] sigma2 = sigma2[500:] assert_almost_equal(data - sim_data[0] + 1.0, np.ones_like(data)) assert_almost_equal(sigma2 / sim_data[1], np.ones_like(sigma2)) assert_equal(ewma.num_params, 0) assert_equal(ewma.name, 'EWMA/RiskMetrics') assert isinstance(ewma.__str__(), str) txt = ewma.__repr__() assert str(hex(id(ewma))) in txt
def test_ewma_estimated(self): ewma = EWMAVariance(lam=None) sv = ewma.starting_values(self.resids) assert sv == 0.94 assert sv.shape[0] == ewma.num_params bounds = ewma.bounds(self.resids) assert len(bounds) == 1 assert bounds == [(0, 1)] var_bounds = ewma.variance_bounds(self.resids) backcast = ewma.backcast(self.resids) w = 0.94 ** np.arange(75) assert_almost_equal(backcast, np.sum((self.resids[:75] ** 2) * (w / w.sum()))) parameters = np.array([0.9234]) var_bounds = ewma.variance_bounds(self.resids) ewma.compute_variance(parameters, self.resids, self.sigma2, backcast, var_bounds) cond_var_direct = np.zeros_like(self.sigma2) cond_var_direct[0] = backcast parameters = np.array([0, 1-parameters[0], parameters[0]]) 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) assert_allclose(self.sigma2 / cond_var_direct, np.ones_like(self.sigma2)) names = ewma.parameter_names() names_target = ['lam'] assert_equal(names, names_target) a, b = ewma.constraints() a_target = np.ones((1, 1)) b_target = np.zeros((1,)) assert_array_equal(a, a_target) assert_array_equal(b, b_target) assert_equal(ewma.num_params, 1) assert_equal(ewma.name, 'EWMA/RiskMetrics') assert isinstance(ewma.__str__(), str) txt = ewma.__repr__() assert str(hex(id(ewma))) in txt
def test_ewma_estimated(self): ewma = EWMAVariance(lam=None) sv = ewma.starting_values(self.resids) assert sv == 0.94 assert sv.shape[0] == ewma.num_params bounds = ewma.bounds(self.resids) assert len(bounds) == 1 assert bounds == [(0, 1)] ewma.variance_bounds(self.resids) backcast = ewma.backcast(self.resids) w = 0.94 ** np.arange(75) assert_almost_equal(backcast, np.sum((self.resids[:75] ** 2) * (w / w.sum()))) parameters = np.array([0.9234]) var_bounds = ewma.variance_bounds(self.resids) ewma.compute_variance(parameters, self.resids, self.sigma2, backcast, var_bounds) cond_var_direct = np.zeros_like(self.sigma2) cond_var_direct[0] = backcast parameters = np.array([0, 1 - parameters[0], parameters[0]]) 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) assert_allclose(self.sigma2 / cond_var_direct, np.ones_like(self.sigma2)) names = ewma.parameter_names() names_target = ['lam'] assert_equal(names, names_target) a, b = ewma.constraints() a_target = np.ones((1, 1)) b_target = np.zeros((1,)) assert_array_equal(a, a_target) assert_array_equal(b, b_target) assert_equal(ewma.num_params, 1) assert_equal(ewma.name, 'EWMA/RiskMetrics') assert isinstance(ewma.__str__(), str) txt = ewma.__repr__() assert str(hex(id(ewma))) in txt state = self.rng.get_state() rng = Normal() rng.random_state.set_state(state) lam = parameters[-1] sim_data = ewma.simulate([lam], self.T, rng.simulate([])) self.rng.set_state(state) e = self.rng.standard_normal(self.T + 500) initial_value = 1.0 sigma2 = np.zeros(self.T + 500) data = np.zeros(self.T + 500) sigma2[0] = initial_value data[0] = np.sqrt(initial_value) for t in range(1, self.T + 500): sigma2[t] = lam * sigma2[t - 1] + (1-lam) * data[t - 1] ** 2.0 data[t] = e[t] * np.sqrt(sigma2[t]) data = data[500:] sigma2 = sigma2[500:] assert_almost_equal(data - sim_data[0] + 1.0, np.ones_like(data)) assert_almost_equal(sigma2 / sim_data[1], np.ones_like(sigma2))