예제 #1
0
    def test_from_abc(self):
        """Test init from abc."""

        nstocks = 2
        amat = np.eye(nstocks)
        bmat = np.eye(nstocks)
        cmat = np.eye(nstocks)
        param = ParamStandard.from_abc(amat=amat, bmat=bmat, cmat=cmat)
        npt.assert_array_equal(amat, param.amat)
        npt.assert_array_equal(bmat, param.bmat)
        npt.assert_array_equal(cmat, param.cmat)

        nstocks = 2
        alpha, beta = .09, .81
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * alpha**.5
        bmat = np.eye(nstocks) * beta**.5
        target = np.eye(nstocks)
        # Choose intercept to normalize unconditional variance to one
        cmat = ParamStandard.find_cmat(amat=amat, bmat=bmat, target=target)

        param = ParamStandard.from_abc(amat=amat, bmat=bmat, cmat=cmat)

        npt.assert_array_equal(amat, param.amat)
        npt.assert_array_equal(bmat, param.bmat)
        npt.assert_array_equal(cmat, param.cmat)
예제 #2
0
    def test_from_target(self):
        """Test init from abc."""

        nstocks = 2
        target = np.eye(nstocks)*.5

        param = ParamStandard.from_target(target=target)
        param_default = ParamStandard(nstocks)
        cmat = ParamStandard.find_cmat(amat=param_default.amat,
                                    bmat=param_default.bmat, target=target)
        param_default = ParamStandard.from_abc(amat=param_default.amat,
                                            bmat=param_default.bmat, cmat=cmat)

        npt.assert_array_equal(param.amat, param_default.amat)
        npt.assert_array_equal(param.bmat, param_default.bmat)
        npt.assert_array_equal(param.cmat, cmat)

        amat = np.eye(nstocks)*.1
        bmat = np.eye(nstocks)*.5

        param = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
        cmat = ParamStandard.find_cmat(amat=amat, bmat=bmat, target=target)

        npt.assert_array_equal(amat, param.amat)
        npt.assert_array_equal(bmat, param.bmat)
        npt.assert_array_equal(cmat, param.cmat)
예제 #3
0
    def test_forecast(self):
        """Test forecast."""

        nstocks = 2
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * .09**.5
        bmat = np.eye(nstocks) * .9**.5
        cmat = np.eye(nstocks)
        param = ParamStandard.from_abc(amat=amat, bmat=bmat, cmat=cmat)

        innov = np.ones(nstocks)
        hvar = np.ones((nstocks, nstocks))

        forecast = BEKK.forecast_one(hvar=hvar, innov=innov, param=param)
        exp = cmat.dot(cmat.T)
        exp += amat.dot(innov * innov[:, np.newaxis]).dot(amat.T)
        exp += bmat.dot(hvar).dot(bmat.T)

        self.assertEqual(forecast.shape, (nstocks, nstocks))
        npt.assert_array_equal(forecast, exp)
예제 #4
0
    def test_find_stationary_var(self):
        """Test find stationary variance matrix."""

        nstocks = 2
        alpha, beta = .09, .5
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * alpha**.5
        bmat = np.eye(nstocks) * beta**.5
        target = np.eye(nstocks)
        # Choose intercept to normalize unconditional variance to one
        cmat = ParamStandard.find_cmat(amat=amat, bmat=bmat, target=target)
        param = ParamStandard.from_abc(amat=amat, bmat=bmat, cmat=cmat)
        hvar = param.get_uvar()

        npt.assert_array_almost_equal(hvar, target)

        hvar = ParamStandard.find_stationary_var(amat=amat, bmat=bmat,
                                                 cmat=cmat)

        npt.assert_array_almost_equal(hvar, target)
        npt.assert_array_equal(hvar, hvar.transpose())
예제 #5
0
    def test_loss(self):
        """Test loss function."""

        nstocks = 2
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * .09**.5
        bmat = np.eye(nstocks) * .9**.5
        cmat = np.eye(nstocks)
        param = ParamStandard.from_abc(amat=amat, bmat=bmat, cmat=cmat)

        innov = np.ones(nstocks)
        hvar = np.ones((nstocks, nstocks))

        pret = BEKK.pret(innov)
        pvar = BEKK.pvar(hvar)
        self.assertIsInstance(pret, float)
        self.assertIsInstance(pvar, float)

        weights = [1, 3]
        pret = BEKK.pret(innov, weights=weights)
        pvar = BEKK.pvar(hvar, weights=weights)

        self.assertIsInstance(pret, float)
        self.assertIsInstance(pvar, float)
        self.assertEqual(pret, 1)
        self.assertEqual(pvar, 1)

        forecast = BEKK.forecast_one(hvar=hvar, innov=innov, param=param)
        proxy = BEKK.sqinnov(innov)

        self.assertEqual(proxy.shape, (nstocks, nstocks))
        self.assertEqual(forecast.shape, (nstocks, nstocks))

        for kind in ['equal', 'minvar']:
            var = BEKK.portf_var(forecast=forecast, alpha=.05, weights=weights)
            self.assertIsInstance(var, float)

            loss_var = BEKK.loss_var(error=innov[-1])
            self.assertIsInstance(loss_var, float)

        loss_eucl = BEKK.loss_eucl(forecast=forecast, proxy=proxy)
        loss_frob = BEKK.loss_frob(forecast=forecast, proxy=proxy)
        loss_stein = BEKK.loss_stein(forecast=forecast, proxy=proxy)
        loss_stein2 = BEKK.loss_stein2(forecast=forecast, innov=innov)

        self.assertIsInstance(loss_eucl, float)
        self.assertIsInstance(loss_frob, float)
        self.assertIsInstance(loss_stein, float)
        self.assertIsInstance(loss_stein2, float)

        portf_lscore = BEKK.portf_lscore(forecast=hvar, innov=innov)
        portf_mse = BEKK.portf_mse(forecast=hvar, proxy=proxy)
        portf_qlike = BEKK.portf_qlike(forecast=hvar, proxy=proxy)

        self.assertIsInstance(portf_lscore, float)
        self.assertIsInstance(portf_mse, float)
        self.assertIsInstance(portf_qlike, float)
        self.assertEqual(portf_lscore, .5)
        self.assertEqual(portf_mse, 0)
        self.assertEqual(portf_qlike, 1)

        all_losses = BEKK.all_losses(forecast=forecast,
                                     proxy=proxy,
                                     innov=innov)

        self.assertIsInstance(all_losses, dict)
예제 #6
0
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)
예제 #7
0
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)