def test_from_abcmat(self): """Test spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] weights = ParamSpatial.get_weight(groups) ncat = 1 alpha, beta, gamma = .01, .16, .09 # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) * alpha**.5 bvecs = np.ones((ncat+1, nstocks)) * beta**.5 dvecs = np.vstack([np.ones((1, nstocks)), np.ones((ncat, nstocks)) * gamma**.5]) amat = np.diag(avecs[0]) + np.diag(avecs[0]).dot(weights[0]) bmat = np.diag(bvecs[0]) + np.diag(bvecs[0]).dot(weights[0]) dmat = np.eye(nstocks) - np.diag(dvecs[1]).dot(weights[0]) dmat_inv = scl.inv(dmat) cmat = dmat_inv.dot(np.diag(dvecs[0])).dot(dmat_inv) param = ParamSpatial.from_abcmat(avecs=avecs, bvecs=bvecs, cmat=cmat, groups=groups) npt.assert_array_equal(amat, param.amat) npt.assert_array_equal(bmat, param.bmat) npt.assert_array_equal(cmat, param.cmat) npt.assert_array_equal(avecs, param.avecs) npt.assert_array_equal(bvecs, param.bvecs) npt.assert_array_equal(None, param.dvecs) npt.assert_array_equal(weights, param.weights)
def test_get_weight(self): """Test construction of spatial weights from groups. """ groups = [[(0, 1), (2, 3)]] weight = ParamSpatial.get_weight(groups=groups) weight_exp = np.zeros((1, 4, 4)) weight_exp[0, 0, 1] = 1 weight_exp[0, 1, 0] = 1 weight_exp[0, 2, 3] = 1 weight_exp[0, 3, 2] = 1 npt.assert_almost_equal(weight, weight_exp) groups = [[(0, 1, 2)]] weight = ParamSpatial.get_weight(groups=groups) weight_exp = np.array([[[0, .5, .5], [.5, 0, .5], [.5, .5, 0]]]) npt.assert_almost_equal(weight, weight_exp) groups = [[(0, 1), (2, 3)], [(0, 2), (1, 3)]] weight = ParamSpatial.get_weight(groups=groups) weight_exp = np.zeros((len(groups), 4, 4)) weight_exp[0, :2, :2] = np.array([[0, 1], [1, 0]]) weight_exp[0, 2:, 2:] = np.array([[0, 1], [1, 0]]) weight_exp[1, 0:3:2, 0:3:2] = np.array([[0, 1], [1, 0]]) weight_exp[1, 1:4:2, 1:4:2] = np.array([[0, 1], [1, 0]]) npt.assert_almost_equal(weight, weight_exp) groups = [[(0, 1), (2, 3, 4)]] weight = ParamSpatial.get_weight(groups=groups) weight_exp = np.zeros((len(groups), 5, 5)) weight_exp[0, :2, :2] = np.array([[0, 1], [1, 0]]) weight_exp[0, 2:, 2:] = np.array([[[0, .5, .5], [.5, 0, .5], [.5, .5, 0]]]) npt.assert_almost_equal(weight, weight_exp)
def test_from_abt(self): """Test spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] weights = ParamSpatial.get_weight(groups) ncat = 1 alpha, beta, gamma = .01, .16, .09 # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) * alpha**.5 bvecs = np.ones((ncat+1, nstocks)) * beta**.5 dvecs = np.vstack([np.ones((1, nstocks)), np.ones((ncat, nstocks)) * gamma**.5]) amat = np.diag(avecs[0]) + np.diag(avecs[0]).dot(weights[0]) bmat = np.diag(bvecs[0]) + np.diag(bvecs[0]).dot(weights[0]) dmat = np.eye(nstocks) - np.diag(dvecs[1]).dot(weights[0]) dmat_inv = scl.inv(dmat) ccmat = dmat_inv.dot(np.diag(dvecs[0])).dot(dmat_inv) cmat = scl.cholesky(ccmat, 1) target = ParamSpatial.find_stationary_var(amat=amat, bmat=bmat, cmat=cmat) cmat_new = ParamSpatial.find_cmat(amat=amat, bmat=bmat, target=target) npt.assert_array_almost_equal(cmat[np.tril_indices(nstocks)], cmat_new[np.tril_indices(nstocks)]) param = ParamSpatial.from_abt(avecs=avecs, bvecs=bvecs, target=target, groups=groups, restriction='hetero') npt.assert_array_equal(amat, param.amat) npt.assert_array_equal(bmat, param.bmat) npt.assert_array_almost_equal(cmat, param.cmat) npt.assert_array_equal(avecs, param.avecs) npt.assert_array_equal(bvecs, param.bvecs) # npt.assert_array_equal(None, param.dvecs) npt.assert_array_equal(weights, param.weights)
def try_spatial_combinations(): """Try simulating spatial BEKK and estimating it with both spatial and standard. """ use_target = False cfree = True restriction = 'full' nstocks = 3 nobs = 2000 groups = [(0, 1)] weights = ParamSpatial.get_weight(groups=groups, nitems=nstocks) ncat = weights.shape[0] alpha = np.array([.1, .01]) beta = np.array([.5, .01]) gamma = .0 # A, B, C - n x n matrices avecs = np.ones((ncat + 1, nstocks)) * alpha[:, np.newaxis]**.5 bvecs = np.ones((ncat + 1, nstocks)) * beta[:, np.newaxis]**.5 dvecs = np.ones((ncat, nstocks)) * gamma**.5 vvec = np.ones(nstocks) param_true = ParamSpatial.from_spatial(avecs=avecs, bvecs=bvecs, dvecs=dvecs, vvec=vvec, weights=weights) print(param_true) innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal') bekk = BEKK(innov) # ------------------------------------------------------------------------- # Estimate spatial result = bekk.estimate(param_start=param_true, use_target=use_target, restriction=restriction, cfree=cfree, model='spatial', weights=weights, method='SLSQP', cython=True) print(result) theta_true = param_true.get_theta(use_target=use_target, cfree=cfree, restriction=restriction) theta_final = result.param_final.get_theta(use_target=use_target, cfree=cfree, restriction=restriction) norm = np.linalg.norm(theta_true - theta_final) print('\nParameters (true and estimated):\n', np.vstack([theta_true, theta_final]).T) print('\nEucledean norm of the difference = %.4f' % norm) # ------------------------------------------------------------------------- # Estimate standard param_true = ParamStandard.from_abc(amat=param_true.amat, bmat=param_true.bmat, cmat=param_true.cmat) result = bekk.estimate(param_start=param_true, use_target=use_target, restriction=restriction, cfree=cfree, model='standard', weights=weights, method='SLSQP', cython=True) print(result) theta_true = param_true.get_theta(use_target=use_target, restriction=restriction) theta_final = result.param_final.get_theta(use_target=use_target, restriction=restriction) norm = np.linalg.norm(theta_true - theta_final) print('\nParameters (true and estimated):\n', np.vstack([theta_true, theta_final]).T) print('\nEucledean norm of the difference = %.4f' % norm)
def try_spatial_combinations(): """Try simulating spatial BEKK and estimating it with both spatial and standard. """ use_target = False cfree = True restriction = 'full' nstocks = 3 nobs = 2000 groups = [(0, 1)] weights = ParamSpatial.get_weight(groups=groups, nitems=nstocks) ncat = weights.shape[0] alpha = np.array([.1, .01]) beta = np.array([.5, .01]) gamma = .0 # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) * alpha[:, np.newaxis]**.5 bvecs = np.ones((ncat+1, nstocks)) * beta[:, np.newaxis]**.5 dvecs = np.ones((ncat, nstocks)) * gamma**.5 vvec = np.ones(nstocks) param_true = ParamSpatial.from_spatial(avecs=avecs, bvecs=bvecs, dvecs=dvecs, vvec=vvec, weights=weights) print(param_true) innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal') bekk = BEKK(innov) # ------------------------------------------------------------------------- # Estimate spatial result = bekk.estimate(param_start=param_true, use_target=use_target, restriction=restriction, cfree=cfree, model='spatial', weights=weights, method='SLSQP', cython=True) print(result) theta_true = param_true.get_theta(use_target=use_target, cfree=cfree, restriction=restriction) theta_final = result.param_final.get_theta(use_target=use_target, cfree=cfree, restriction=restriction) norm = np.linalg.norm(theta_true - theta_final) print('\nParameters (true and estimated):\n', np.vstack([theta_true, theta_final]).T) print('\nEucledean norm of the difference = %.4f' % norm) # ------------------------------------------------------------------------- # Estimate standard param_true = ParamStandard.from_abc(amat=param_true.amat, bmat=param_true.bmat, cmat=param_true.cmat) result = bekk.estimate(param_start=param_true, use_target=use_target, restriction=restriction, cfree=cfree, model='standard', weights=weights, method='SLSQP', cython=True) print(result) theta_true = param_true.get_theta(use_target=use_target, restriction=restriction) theta_final = result.param_final.get_theta(use_target=use_target, restriction=restriction) norm = np.linalg.norm(theta_true - theta_final) print('\nParameters (true and estimated):\n', np.vstack([theta_true, theta_final]).T) print('\nEucledean norm of the difference = %.4f' % norm)