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_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)