def test_targeting(self): """Test targeting.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] ncat = len(groups) alpha = [.01, .02, .03, .04, .05, .06] beta = [.07, .08, .09, .1, .11, .12] delta = [.13, .14, .15, .16, .17, .18] # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) bvecs = np.ones((ncat+1, nstocks)) dvecs = np.ones((ncat+1, nstocks)) avecs[0] = alpha[:4] avecs[1, :2] *= alpha[-2] avecs[1, 2:] *= alpha[-1] bvecs[0] = beta[:4] bvecs[1, :2] *= beta[-2] bvecs[1, 2:] *= beta[-1] dvecs[0] = delta[:4] dvecs[1, :2] *= delta[-2] dvecs[1, 2:] *= delta[-1] param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) restriction = 'h**o' target = param.get_uvar() param = ParamSpatial.from_abt(avecs=avecs, bvecs=bvecs, groups=groups, target=target, restriction=restriction, solve_dvecs=True) param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=param.dvecs, groups=groups)
def test_simulation_spatial(self): """Test simulation spatial.""" nobs = 10 nstocks = 4 groups = [[(0, 1), (2, 3)]] 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]) param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) for distr in ['normal', 'student', 'skewt']: innov, hvar = simulate_bekk(param, nobs=nobs, distr=distr) self.assertEqual(innov.shape, (nobs, nstocks)) self.assertEqual(hvar.shape, (nobs, nstocks, nstocks))
def test_from_theta_shomo(self): """Test init from theta for spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] ncat = len(groups) 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]) param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) restriction = 'shomo' target = None theta = [avecs[:, 0], bvecs[:, 0], dvecs[0], dvecs[1:, 0]] theta = np.concatenate(theta) param_new = ParamSpatial.from_theta(theta=theta, groups=groups, restriction=restriction, target=target) npt.assert_array_equal(param.amat, param_new.amat) npt.assert_array_equal(param.bmat, param_new.bmat) npt.assert_array_equal(param.cmat, param_new.cmat) npt.assert_array_equal(param.avecs, param_new.avecs) npt.assert_array_equal(param.bvecs, param_new.bvecs) npt.assert_array_equal(param.dvecs, param_new.dvecs) target = param.get_uvar() theta = [avecs[:, 0], bvecs[:, 0]] theta = np.concatenate(theta) cmat = param.find_cmat(amat=param.amat, bmat=param.bmat, target=target) param_new = ParamSpatial.from_theta(theta=theta, groups=groups, restriction=restriction, target=target) npt.assert_array_equal(param.amat, param_new.amat) npt.assert_array_equal(param.bmat, param_new.bmat) npt.assert_array_equal(cmat, param_new.cmat) npt.assert_array_equal(param.avecs, param_new.avecs) npt.assert_array_equal(param.bvecs, param_new.bvecs) # npt.assert_array_equal(None, param_new.dvecs) cfree = True theta = [avecs[:, 0], bvecs[:, 0], param.cmat[np.tril_indices(nstocks)]] theta = np.concatenate(theta) param_new = ParamSpatial.from_theta(theta=theta, groups=groups, restriction=restriction, cfree=cfree) npt.assert_array_equal(param.amat, param_new.amat) npt.assert_array_equal(param.bmat, param_new.bmat) npt.assert_array_equal(np.tril(param.cmat), param_new.cmat) npt.assert_array_equal(param.avecs, param_new.avecs) npt.assert_array_equal(param.bvecs, param_new.bvecs) npt.assert_array_equal(None, param_new.dvecs)
def try_interative_estimation_spatial(): """Try estimating parameters from simple to more complicated model. """ restriction = 'full' nobs = 2000 groups = [[(0, 2), (1, 3)]] nstocks = np.max(groups) + 1 ncat = len(groups) 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_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, vvec=vvec, groups=groups) print(param_true) innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal') bekk = BEKK(innov) result = bekk.estimate(use_target=False, restriction=restriction, cfree=False, model='spatial', groups=groups) print(result)
def try_interative_estimation_spatial(): """Try estimating parameters from simple to more complicated model. """ restriction = 'full' nobs = 2000 groups = [[(0, 2), (1, 3)]] nstocks = np.max(groups) + 1 ncat = len(groups) 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_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, vvec=vvec, groups=groups) print(param_true) innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal') bekk = BEKK(innov) result = bekk.estimate(use_target=False, restriction=restriction, cfree=False, model='spatial', groups=groups) print(result)
def try_spatial(): """Try simulating and estimating spatial BEKK. """ cfree = False restriction = 'h**o' nobs = 2000 groups = [[(0, 1), (2, 3)]] nstocks = np.max(groups) + 1 ncat = len(groups) alpha = np.array([.1, .01]) beta = np.array([.5, .01]) gamma = .09 # 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.vstack( [np.ones((1, nstocks)), np.ones((ncat, nstocks)) * gamma**.5]) param_true = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) print(param_true) innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal') # plot_data(innov, hvar_true) bekk = BEKK(innov) for use_target in [True, False]: result = bekk.estimate(param_start=param_true, use_target=use_target, cfree=cfree, restriction=restriction, groups=groups, model='spatial', 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):') print(np.vstack([theta_true, theta_final]).T) print('\nEucledean norm of the difference = %.4f' % norm)
def test_from_theta_ghomo_special(self): """Test group init from theta for spatial specification.""" nstocks = 4 groups = [[(0, 1, 2, 3)]] ncat = len(groups) alpha = [.01, .02, .03, .04, .05] beta = [.07, .08, .09, .1, .11] delta = [.13, .14, .15, .16, .17] # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) bvecs = np.ones((ncat+1, nstocks)) dvecs = np.ones((ncat+1, nstocks)) avecs[0] = alpha[:4] avecs[1] *= alpha[-1] bvecs[0] = beta[:4] bvecs[1] *= beta[-1] dvecs[0] = delta[:4] dvecs[1] *= delta[-1] param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) target = param.get_uvar() theta = np.concatenate([alpha, beta]) cmat = param.find_cmat(amat=param.amat, bmat=param.bmat, target=target) uvar = param.find_stationary_var(amat=param.amat, bmat=param.bmat, cmat=cmat) npt.assert_array_almost_equal(target, uvar) restriction = 'h**o' param_homo = ParamSpatial.from_theta(theta=theta, groups=groups, restriction=restriction, target=target) theta_homo = param_homo.get_theta(restriction=restriction, use_target=False) restriction = 'ghomo' param_ghomo = ParamSpatial.from_theta(theta=theta, groups=groups, restriction=restriction, target=target) theta_ghomo = param_ghomo.get_theta(restriction=restriction, use_target=False) npt.assert_array_almost_equal(cmat, param.cmat) npt.assert_array_almost_equal(theta_ghomo, theta_homo, decimal=3) for param_check in [param_homo, param_ghomo]: npt.assert_array_equal(param.avecs, param_check.avecs) npt.assert_array_equal(param.bvecs, param_check.bvecs) npt.assert_array_equal(param.amat, param_check.amat) npt.assert_array_equal(param.bmat, param_check.bmat) npt.assert_array_equal(cmat, param_check.cmat)
def test_get_theta_homo(self): """Test theta vector for spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] 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]) param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) restriction = 'h**o' use_target = True theta = [avecs[0], avecs[1:, 0], bvecs[0], bvecs[1:, 0]] theta = np.concatenate(theta) nparams = 2 * (nstocks + ncat) theta_exp = param.get_theta(restriction=restriction, use_target=use_target) self.assertEqual(nparams, theta_exp.size) self.assertEqual(nparams, theta.size) npt.assert_array_equal(theta, theta_exp) use_target = False theta = [avecs[0], avecs[1:, 0], bvecs[0], bvecs[1:, 0], dvecs[0], dvecs[1:, 0]] theta = np.concatenate(theta) nparams = 3 * (nstocks + ncat) theta_exp = param.get_theta(restriction=restriction, use_target=use_target) self.assertEqual(nparams, theta_exp.size) self.assertEqual(nparams, theta.size) npt.assert_array_equal(theta, theta_exp) cfree = True theta = [avecs[0], avecs[1:, 0], bvecs[0], bvecs[1:, 0], param.cmat[np.tril_indices(param.cmat.shape[0])]] theta = np.concatenate(theta) nparams = 2 * (nstocks + ncat) + nstocks * (nstocks + 1) // 2 theta_exp = param.get_theta(restriction=restriction, cfree=cfree) self.assertEqual(nparams, theta_exp.size) self.assertEqual(nparams, theta.size) npt.assert_array_equal(theta, theta_exp)
def try_spatial(): """Try simulating and estimating spatial BEKK. """ cfree = False restriction = 'h**o' nobs = 2000 groups = [[(0, 1), (2, 3)]] nstocks = np.max(groups) + 1 ncat = len(groups) alpha = np.array([.1, .01]) beta = np.array([.5, .01]) gamma = .09 # 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.vstack([np.ones((1, nstocks)), np.ones((ncat, nstocks)) * gamma**.5]) param_true = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) print(param_true) innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal') # plot_data(innov, hvar_true) bekk = BEKK(innov) for use_target in [True, False]: result = bekk.estimate(param_start=param_true, use_target=use_target, cfree=cfree, restriction=restriction, groups=groups, model='spatial', 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):') print(np.vstack([theta_true, theta_final]).T) print('\nEucledean norm of the difference = %.4f' % norm)
def test_from_theta_ghomo(self): """Test group init from theta for spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] ncat = len(groups) alpha = [.01, .02, .03, .04, .05, .06] beta = [.07, .08, .09, .1, .11, .12] delta = [.13, .14, .15, .16, .17, .18] # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) bvecs = np.ones((ncat+1, nstocks)) dvecs = np.ones((ncat+1, nstocks)) avecs[0] = alpha[:4] avecs[1, :2] *= alpha[-2] avecs[1, 2:] *= alpha[-1] bvecs[0] = beta[:4] bvecs[1, :2] *= beta[-2] bvecs[1, 2:] *= beta[-1] dvecs[0] = delta[:4] dvecs[1, :2] *= delta[-2] dvecs[1, 2:] *= delta[-1] param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) restriction = 'ghomo' target = param.get_uvar() theta = np.concatenate([alpha, beta]) cmat = param.find_cmat(amat=param.amat, bmat=param.bmat, target=target) uvar = param.find_stationary_var(amat=param.amat, bmat=param.bmat, cmat=cmat) npt.assert_array_almost_equal(target, uvar) param_new = ParamSpatial.from_theta(theta=theta, groups=groups, restriction=restriction, target=target) npt.assert_array_equal(param.avecs, param_new.avecs) npt.assert_array_equal(param.bvecs, param_new.bvecs) # npt.assert_array_equal(None, param_new.dvecs) npt.assert_array_equal(param.amat, param_new.amat) npt.assert_array_equal(param.bmat, param_new.bmat) npt.assert_array_equal(cmat, param_new.cmat) npt.assert_array_almost_equal(cmat, param.cmat)
def test_get_theta_from_ab(self): """Test theta vector for spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] 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]) param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) restriction = 'hetero' theta = np.concatenate([avecs.flatten(), bvecs.flatten()]) theta_exp = param.get_theta_from_ab(restriction=restriction) npt.assert_array_equal(theta, theta_exp) restriction = 'ghomo' theta = np.concatenate([avecs[0], avecs[1:, :2].flatten(), bvecs[0], bvecs[1:, :2].flatten()]) theta_exp = param.get_theta_from_ab(restriction=restriction) npt.assert_array_equal(theta, theta_exp) restriction = 'h**o' theta = np.concatenate([avecs[0], avecs[1:, 0], bvecs[0], bvecs[1:, 0]]) theta_exp = param.get_theta_from_ab(restriction=restriction) npt.assert_array_equal(theta, theta_exp) restriction = 'shomo' theta = np.concatenate([avecs[:, 0], bvecs[:, 0]]) theta_exp = param.get_theta_from_ab(restriction=restriction) npt.assert_array_equal(theta, theta_exp)
def test_from_abdv(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]) param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) 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) 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(dvecs, param.dvecs) npt.assert_array_equal(weights, param.weights) mats = ParamSpatial.from_vecs_to_mat(avecs=avecs, bvecs=bvecs, dvecs=dvecs, weights=weights) amat_new, bmat_new, dmat_new = mats npt.assert_array_equal(amat, amat_new) npt.assert_array_equal(bmat, bmat_new) npt.assert_array_equal(dmat, dmat_new)
def test_get_theta_ghomo(self): """Test theta vector for spatial specification.""" nstocks = 4 groups = [[(0, 1), (2, 3)]] ncat = len(groups) alpha = [.01, .02, .03] beta = [.04, .05, .06] delta = [.07, .08] # A, B, C - n x n matrices avecs = np.ones((ncat+1, nstocks)) bvecs = np.ones((ncat+1, nstocks)) dvecs = np.ones((ncat+1, nstocks)) avecs[0, :] *= alpha[0] avecs[1, :2] *= alpha[1] avecs[1, 2:] *= alpha[2] bvecs[0, :] *= beta[0] bvecs[1, :2] *= beta[1] bvecs[1, 2:] *= beta[2] dvecs[1, :2] *= delta[0] dvecs[1, 2:] *= delta[1] param = ParamSpatial.from_abdv(avecs=avecs, bvecs=bvecs, dvecs=dvecs, groups=groups) restriction = 'ghomo' use_target = True theta = [avecs[0], [avecs[1, 0]], [avecs[1, 2]], bvecs[0], [bvecs[1, 0]], [bvecs[1, 2]]] theta = np.concatenate(theta) nparams = 2 * (nstocks + 2 * ncat) theta_exp = param.get_theta(restriction=restriction, use_target=use_target) self.assertEqual(nparams, theta_exp.size) self.assertEqual(nparams, theta.size) npt.assert_array_equal(theta, theta_exp) use_target = False theta = [avecs[0], [avecs[1, 0]], [avecs[1, 2]], bvecs[0], [bvecs[1, 0]], [bvecs[1, 2]], dvecs[0], [dvecs[1, 0]], [dvecs[1, 2]]] theta = np.concatenate(theta) nparams = 3 * (nstocks + 2 * ncat) theta_exp = param.get_theta(restriction=restriction, use_target=use_target) self.assertEqual(nparams, theta_exp.size) self.assertEqual(nparams, theta.size) npt.assert_array_equal(theta, theta_exp) cfree = True theta = [avecs[0], [avecs[1, 0]], [avecs[1, 2]], bvecs[0], [bvecs[1, 0]], [bvecs[1, 2]], param.cmat[np.tril_indices(param.cmat.shape[0])]] theta = np.concatenate(theta) nparams = 2 * (nstocks + 2 * ncat) + nstocks * (nstocks + 1) // 2 theta_exp = param.get_theta(restriction=restriction, cfree=cfree) self.assertEqual(nparams, theta_exp.size) self.assertEqual(nparams, theta.size) npt.assert_array_equal(theta, theta_exp)