示例#1
0
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)
示例#2
0
    def test_weights(self):
        """Test weighting function.

        """
        nstocks = 6
        nobs = 10
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * .09**.5
        bmat = np.eye(nstocks) * .9**.5
        target = np.eye(nstocks)

        param = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
        innov, hvar = simulate_bekk(param, nobs=nobs)

        res = BEKKResults(innov=innov, hvar=hvar)

        weights = res.weights()

        npt.assert_array_equal(weights, np.ones_like(innov) / nstocks)

        weights = res.weights(kind='equal')

        npt.assert_array_equal(weights, np.ones_like(innov) / nstocks)

        weights = res.weights(kind='minvar')

        for hvari, wi in zip(hvar, weights):
            hinv = np.linalg.solve(hvari, np.ones(nstocks))
            npt.assert_array_almost_equal(wi, hinv / hinv.sum())
示例#3
0
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)
示例#4
0
    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))
示例#5
0
def try_standard():
    """Try simulating and estimating standard BEKK.

    """
    use_target = False
    restriction = 'full'
    nstocks = 6
    nobs = 2000
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
    print(param_true)

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

#    plot_data(innov, hvar_true)

    bekk = BEKK(innov)
    result = bekk.estimate(param_start=param_true, use_target=use_target,
                           model='standard', method='SLSQP',
                           restriction=restriction)

    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)
示例#6
0
def try_bekk():
    """Simulate and estimate BEKK model.

    """
    nstocks = 2
    use_target = True
    nobs = 2000
    restriction = 'full'
    simulate = True

    # A, B, C - n x n matrices
    A = np.eye(nstocks) * .09**.5
    B = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)

    param_true = ParamStandard.from_target(amat=A, bmat=B, target=target)
    theta_true = param_true.get_theta(use_target=use_target,
                                      restriction=restriction)
    print('True parameter:\n', theta_true)
    # Data file
    innov_file = '../data/innovations.npy'

    if simulate:
        # Simulate data
        innov, hvar_true = simulate_bekk(param_true,
                                         nobs=nobs,
                                         distr='skewt',
                                         degf=30)
        np.savetxt(innov_file[:-4] + '.csv', innov, delimiter=",")

    else:
        # Regenerate real data
        download_data(innov_file=innov_file, nstocks=nstocks, nobs=nobs)
        # Load data from the drive
        innov = np.load(innov_file)

    # Estimate parameters
    params = []
    for cython in [True, False]:
        time_start = time.time()
        # Initialize the object
        bekk = BEKK(innov)
        bekk.estimate(param_start=param_true,
                      restriction=restriction,
                      use_target=use_target,
                      method='SLSQP',
                      cython=cython)

        print('Cython: ', cython)
        theta_final = bekk.param_final.get_theta(restriction=restriction,
                                                 use_target=use_target)
        print(theta_final)
        params.append(theta_final)
        print('Time elapsed %.2f, seconds\n' % (time.time() - time_start))

    print('\nNorm difference between the estimates: %.4f' %
          np.linalg.norm(params[0] - params[1]))
    return bekk
示例#7
0
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)
示例#8
0
def try_bekk():
    """Simulate and estimate BEKK model.

    """
    nstocks = 2
    use_target = True
    nobs = 2000
    restriction = 'full'
    simulate = True

    # A, B, C - n x n matrices
    A = np.eye(nstocks) * .09**.5
    B = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)

    param_true = ParamStandard.from_target(amat=A, bmat=B, target=target)
    theta_true = param_true.get_theta(use_target=use_target,
                                      restriction=restriction)
    print('True parameter:\n', theta_true)
    # Data file
    innov_file = '../data/innovations.npy'

    if simulate:
        # Simulate data
        innov, hvar_true = simulate_bekk(param_true, nobs=nobs,
                                         distr='skewt', degf=30)
        np.savetxt(innov_file[:-4] + '.csv', innov, delimiter=",")

    else:
        # Regenerate real data
        download_data(innov_file=innov_file, nstocks=nstocks, nobs=nobs)
        # Load data from the drive
        innov = np.load(innov_file)

    # Estimate parameters
    params = []
    for cython in [True, False]:
        time_start = time.time()
        # Initialize the object
        bekk = BEKK(innov)
        bekk.estimate(param_start=param_true, restriction=restriction,
                      use_target=use_target, method='SLSQP', cython=cython)

        print('Cython: ', cython)
        theta_final = bekk.param_final.get_theta(restriction=restriction,
                                                 use_target=use_target)
        print(theta_final)
        params.append(theta_final)
        print('Time elapsed %.2f, seconds\n' % (time.time() - time_start))

    print('\nNorm difference between the estimates: %.4f'
          % np.linalg.norm(params[0] - params[1]))
    return bekk
示例#9
0
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)
示例#10
0
    def test_simulation(self):
        """Test simulation."""

        nstocks = 6
        nobs = 10
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * .09**.5
        bmat = np.eye(nstocks) * .9**.5
        target = np.eye(nstocks)

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

        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))
示例#11
0
def try_iterative_estimation_standard():
    """Try estimating parameters from simple to more complicated model.

    """
    restriction = 'scalar'
    nstocks = 3
    nobs = 2000
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    bekk = BEKK(innov)
    result = bekk.estimate(use_target=False, restriction=restriction)

    print(result)
示例#12
0
def try_standard_loss():
    """Try forecast evaluation of BEKK model.

    """
    model = 'standard'
    use_target = True
    nstocks = 2
    nobs = 1000
    window = 990
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
    print(param_true)

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    kwargs = {
        'param_start': param_true,
        'innov_all': innov,
        'window': window,
        'model': model,
        'use_target': use_target,
        'alpha': .25,
        'kind': 'equal'
    }
    evaluate = partial(BEKK.collect_losses, **kwargs)
    losses = []
    restrcs = ['scalar', 'diagonal', 'full']
    for restr in restrcs:
        losses.append(evaluate(restriction=restr))

    losses = pd.concat(losses)

    print(losses)

    df = losses['qlike'].unstack('restriction')
    mcs = MCS(df, size=.1)
    mcs.compute()
    print(mcs.pvalues)

    return losses
示例#13
0
def try_iterative_estimation_standard():
    """Try estimating parameters from simple to more complicated model.

    """
    restriction = 'scalar'
    nstocks = 3
    nobs = 2000
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    bekk = BEKK(innov)
    result = bekk.estimate(use_target=False, restriction=restriction)

    print(result)
示例#14
0
    def test_likelihood(self):
        """Test likelihood."""

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

        innov, hvar = simulate_bekk(param, nobs=nobs, distr='normal')

        out1 = likelihood_python(hvar, innov)
        out2 = likelihood_gauss(hvar, innov)

        self.assertIsInstance(out1, float)
        self.assertIsInstance(out2, float)

        self.assertAlmostEqual(out1, out2)
示例#15
0
def time_likelihood():
    """Compare speeds of recrsions and likelihoods.

    """
    nstocks = 2
    nobs = 2000
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
    cmat = param_true.cmat

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    hvar = np.zeros((nobs, nstocks, nstocks), dtype=float)
    hvar[0] = param_true.get_uvar().copy()

    with take_time('Python recursion'):
        filter_var_python(hvar, innov, amat, bmat, cmat)
        hvar1 = hvar.copy()

    hvar = np.zeros((nobs, nstocks, nstocks), dtype=float)
    hvar[0] = param_true.get_uvar().copy()

    with take_time('Cython recursion'):
        filter_var(hvar, innov, amat, bmat, cmat)
        hvar2 = hvar.copy()
        idxl = np.tril_indices(nstocks)
        idxu = np.triu_indices(nstocks)
        hvar2[:, idxu[0], idxu[1]] = hvar2[:, idxl[0], idxl[1]]

    print(np.allclose(hvar_true, hvar1))
    print(np.allclose(hvar_true, hvar2))

    with take_time('Python likelihood'):
        out1 = likelihood_python(hvar, innov)
    with take_time('Cython likelihood'):
        out2 = likelihood_gauss(hvar, innov)

    print(np.allclose(out1, out2))
示例#16
0
def time_likelihood():
    """Compare speeds of recrsions and likelihoods.

    """
    nstocks = 2
    nobs = 2000
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
    cmat = param_true.cmat

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    hvar = np.zeros((nobs, nstocks, nstocks), dtype=float)
    hvar[0] = param_true.get_uvar().copy()

    with take_time('Python recursion'):
        filter_var_python(hvar, innov, amat, bmat, cmat)
        hvar1 = hvar.copy()

    hvar = np.zeros((nobs, nstocks, nstocks), dtype=float)
    hvar[0] = param_true.get_uvar().copy()

    with take_time('Cython recursion'):
        filter_var(hvar, innov, amat, bmat, cmat)
        hvar2 = hvar.copy()
        idxl = np.tril_indices(nstocks)
        idxu = np.triu_indices(nstocks)
        hvar2[:, idxu[0], idxu[1]] = hvar2[:, idxl[0], idxl[1]]

    print(np.allclose(hvar_true, hvar1))
    print(np.allclose(hvar_true, hvar2))

    with take_time('Python likelihood'):
        out1 = likelihood_python(hvar, innov)
    with take_time('Cython likelihood'):
        out2 = likelihood_gauss(hvar, innov)

    print(np.allclose(out1, out2))
示例#17
0
    def test_filter_var(self):
        """Test recursions."""

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

        innov, hvar_true = simulate_bekk(param, nobs=nobs, distr='normal')

        hvar = np.zeros((nobs, nstocks, nstocks), dtype=float)
        hvar[0] = param.get_uvar()

        out1 = filter_var_python(hvar, innov, amat, bmat, cmat)

        hvar = np.zeros((nobs, nstocks, nstocks), dtype=float)
        hvar[0] = param.get_uvar()

        out2 = filter_var(hvar, innov, amat, bmat, cmat)

        idxl = np.tril_indices(nstocks)
        idxu = np.triu_indices(nstocks)

        npt.assert_array_almost_equal(hvar_true,
                                      np.transpose(hvar_true, axes=(0, 2, 1)))

        # npt.assert_array_almost_equal(out2, np.transpose(out2, axes=(0, 2, 1)))
        out2[:, idxu[0], idxu[1]] = out2[:, idxl[0], idxl[1]]

        npt.assert_array_almost_equal(out1, np.transpose(out1, axes=(0, 2, 1)))
        npt.assert_array_almost_equal(out2, np.transpose(out2, axes=(0, 2, 1)))

        npt.assert_array_almost_equal(hvar_true, out1)
        npt.assert_array_almost_equal(hvar_true, out2)
示例#18
0
def try_standard_loss():
    """Try forecast evaluation of BEKK model.

    """
    model = 'standard'
    use_target = True
    nstocks = 2
    nobs = 1000
    window = 990
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
    print(param_true)

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    kwargs = {'param_start': param_true, 'innov_all': innov,
              'window': window, 'model': model, 'use_target': use_target,
              'alpha': .25, 'kind': 'equal'}
    evaluate = partial(BEKK.collect_losses, **kwargs)
    losses = []
    restrcs = ['scalar', 'diagonal', 'full']
    for restr in restrcs:
        losses.append(evaluate(restriction=restr))

    losses = pd.concat(losses)

    print(losses)

    df = losses['qlike'].unstack('restriction')
    mcs = MCS(df, size=.1)
    mcs.compute()
    print(mcs.pvalues)

    return losses
示例#19
0
def try_standard():
    """Try simulating and estimating standard BEKK.

    """
    use_target = False
    restriction = 'full'
    nstocks = 6
    nobs = 2000
    # A, B, C - n x n matrices
    amat = np.eye(nstocks) * .09**.5
    bmat = np.eye(nstocks) * .9**.5
    target = np.eye(nstocks)
    param_true = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
    print(param_true)

    innov, hvar_true = simulate_bekk(param_true, nobs=nobs, distr='normal')

    #    plot_data(innov, hvar_true)

    bekk = BEKK(innov)
    result = bekk.estimate(param_start=param_true,
                           use_target=use_target,
                           model='standard',
                           method='SLSQP',
                           restriction=restriction)

    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)
示例#20
0
    def test_var_ratio(self):
        """Test variance ratio."""

        nstocks = 6
        nobs = 10
        # A, B, C - n x n matrices
        amat = np.eye(nstocks) * .09**.5
        bmat = np.eye(nstocks) * .9**.5
        target = np.eye(nstocks)

        param = ParamStandard.from_target(amat=amat, bmat=bmat, target=target)
        innov, hvar = simulate_bekk(param, nobs=nobs)

        res = BEKKResults(innov=innov, hvar=hvar)

        evar = res.portf_evar()
        rvar = res.portf_rvar()
        vratio = res.loss_var_ratio()
        mvar = res.portf_mvar()

        self.assertEqual(evar.shape, (nobs, ))
        self.assertEqual(rvar.shape, (nobs, ))
        self.assertEqual(vratio.shape, (nobs, ))
        self.assertIsInstance(mvar, float)
示例#21
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)
示例#22
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)