Exemple #1
0
def test_no_exog():
    data = generate_3sls_data_v2(nexog=0, const=False)
    mod = IV3SLS(data)
    res = mod.fit()

    data = generate_3sls_data_v2(nexog=0, const=False, omitted="drop")
    mod = IV3SLS(data)
    res2 = mod.fit()

    data = generate_3sls_data_v2(nexog=0, const=False, omitted="empty")
    mod = IV3SLS(data)
    res3 = mod.fit()

    data = generate_3sls_data_v2(nexog=0, const=False, output_dict=False)
    mod = IV3SLS(data)
    res4 = mod.fit()

    data = generate_3sls_data_v2(
        nexog=0, const=False, output_dict=False, omitted="empty"
    )
    mod = IV3SLS(data)
    res5 = mod.fit()
    assert_series_equal(res.params, res2.params)
    assert_series_equal(res.params, res3.params)
    assert_series_equal(res.params, res4.params)
    assert_series_equal(res.params, res5.params)
Exemple #2
0
def test_no_endog():
    data = generate_3sls_data_v2(nendog=0, ninstr=0)
    mod = IV3SLS(data)
    res = mod.fit()

    data = generate_3sls_data_v2(nendog=0, ninstr=0, omitted='drop')
    mod = IV3SLS(data)
    res2 = mod.fit()

    data = generate_3sls_data_v2(nendog=0, ninstr=0, omitted='empty')
    mod = IV3SLS(data)
    res3 = mod.fit()

    data = generate_3sls_data_v2(nendog=0, ninstr=0, output_dict=False)
    mod = IV3SLS(data)
    res4 = mod.fit()

    data = generate_3sls_data_v2(nendog=0,
                                 ninstr=0,
                                 output_dict=False,
                                 omitted='empty')
    mod = IV3SLS(data)
    res5 = mod.fit()
    assert_series_equal(res.params, res2.params)
    assert_series_equal(res.params, res3.params)
    assert_series_equal(res.params, res4.params)
    assert_series_equal(res.params, res5.params)
def cov_data():
    data = generate_3sls_data_v2(k=2)
    mod = IV3SLS(data)
    res = mod.fit(cov_type="unadjusted")
    x = mod._x
    z = mod._z
    eps = res.resids.values
    sigma = res.sigma
    return x, z, eps, sigma
def cov(request):
    eqns = generate_3sls_data_v2(k=3)
    est = request.param[0]
    name = request.param[1]
    sigma = full_sigma = np.eye(3)
    x = [eqns[key].exog for key in eqns]
    n = x[0].shape[0]
    eps = np.random.standard_normal((n, 3))
    return est(x, eps, sigma, full_sigma, gls=True, debiased=True), name
def weight_data():
    eqns = generate_3sls_data_v2(k=2)
    mod = IV3SLS(eqns)
    x = mod._x
    z = mod._z
    res = mod.fit(cov_type="unadjusted")
    eps = np.asarray(res.resids)
    sigma = np.asarray(res.sigma)
    return x, z, eps, sigma
Exemple #6
0
def cov_data():
    data = generate_3sls_data_v2(k=2)
    mod = IV3SLS(data)
    res = mod.fit(cov_type="unadjusted")
    x = mod._x
    z = mod._z
    eps = np.asarray(res.resids)
    sigma = np.asarray(res.sigma)
    return x, z, eps, sigma
def weight_data():
    eqns = generate_3sls_data_v2(k=2)
    mod = IV3SLS(eqns)
    x = mod._x
    z = mod._z
    res = mod.fit(cov_type="unadjusted")
    eps = res.resids.values
    sigma = res.sigma
    return x, z, eps, sigma
Exemple #8
0
def data(request):
    steps, robust = request.param
    weight_type = 'robust' if robust else 'unadjusted'
    eqns = generate_3sls_data_v2(k=3)
    y = [eqns[key].dependent for key in eqns]
    x = [np.concatenate([eqns[key].exog, eqns[key].endog], 1) for key in eqns]
    z = [np.concatenate([eqns[key].exog, eqns[key].instruments], 1) for key in eqns]

    return AttrDict(eqns=eqns, x=x, y=y, z=z, steps=steps,
                    robust=robust, weight_type=weight_type)
Exemple #9
0
def test_gmm_equiv():
    eqns = generate_3sls_data_v2(k=1)
    sys_mod = IVSystemGMM(eqns)
    eqn = eqns[list(eqns.keys())[0]]
    gmm_mod = IVGMM(eqn.dependent, eqn.exog, eqn.endog, eqn.instruments)
    sys_res = sys_mod.fit()
    gmm_res = gmm_mod.fit()

    assert_allclose(sys_res.params, gmm_res.params)
    assert_allclose(sys_res.rsquared, gmm_res.rsquared)
    assert_allclose(sys_res.tstats, gmm_res.tstats)
Exemple #10
0
def gmm_cov(request):
    eqns = generate_3sls_data_v2(k=3)
    est = request.param[0]
    name = request.param[1]
    sigma = np.eye(3)
    x = [eqns[key].exog for key in eqns]
    z = [np.concatenate([eqns[key].exog, eqns[key].instruments], 1) for key in eqns]
    kz = sum(map(lambda a: a.shape[1], z))
    w = np.eye(kz)
    n = x[0].shape[0]
    eps = np.random.standard_normal((n, 3))
    return est(x, z, eps, w, sigma=sigma), name
Exemple #11
0
def test_3sls_2sls_equiv():
    eqns = generate_3sls_data_v2(k=1)
    tsls_mod = IV3SLS(eqns)
    tsls = tsls_mod.fit(method='ols', cov_type='unadjusted', debiased=False)
    eqn = eqns[list(eqns.keys())[0]]
    ivmod = IV2SLS(eqn.dependent, eqn.exog, eqn.endog, eqn.instruments)
    iv = ivmod.fit(cov_type='unadjusted', debiased=False)
    assert_allclose(iv.params, tsls.params)
    assert_allclose(iv.tstats, tsls.tstats)
    assert_allclose(iv.rsquared, tsls.rsquared)

    tsls = tsls_mod.fit(method='ols', cov_type='unadjusted', debiased=True)
    iv = ivmod.fit(cov_type='unadjusted', debiased=True)
    assert_allclose(iv.tstats, tsls.tstats)

    tsls = tsls_mod.fit(method='ols', cov_type='robust', debiased=False)
    iv = ivmod.fit(cov_type='robust', debiased=False)
    assert_allclose(iv.tstats, tsls.tstats)
Exemple #12
0
def test_gmm_3sls_equiv():
    eqns = generate_3sls_data_v2(k=3)
    gmm = IVSystemGMM(eqns).fit(iter_limit=1)
    tsls = IV3SLS(eqns).fit(method='ols')
    assert_allclose(gmm.params, tsls.params)
def test_no_constant_smoke():
    eqns = generate_3sls_data_v2(k=3, const=False)
    mod = IVSystemGMM(eqns)
    mod.fit()
Exemple #14
0
def test_uneven_shapes():
    data = generate_3sls_data_v2()
    eq = data[list(data.keys())[0]]
    eq['weights'] = np.ones(eq.dependent.shape[0] // 2)
    with pytest.raises(ValueError):
        IV3SLS(data)
from collections import OrderedDict
from itertools import product

import numpy as np
import pytest
from pandas import Series, concat
from pandas.testing import assert_series_equal, assert_frame_equal

from linearmodels import SUR, IVSystemGMM, IV3SLS
from linearmodels.formula import sur, iv_system_gmm, iv_3sls
from linearmodels.system.model import SystemFormulaParser
from linearmodels.tests.system._utility import generate_3sls_data_v2
from linearmodels.utility import AttrDict

data = generate_3sls_data_v2(k=2, const=False)
joined = []
for i, key in enumerate(data):
    eq = data[key]
    joined.append(Series(eq.dependent[:, 0], name='y{0}'.format(i + 1)))
    for j, col in enumerate(eq.exog.T):
        joined.append(Series(col, name='x{0}{1}'.format(i + 1, j + 1)))
    k = len(eq.exog.T)
    for j, col in enumerate(eq.endog.T):
        joined.append(Series(col, name='x{0}{1}'.format(i + 1, j + k + 1)))
    for j, col in enumerate(eq.instruments.T):
        joined.append(Series(col, name='z{0}{1}'.format(i + 1, j + 1)))
joined = concat(joined, 1)

fmlas = [{
    'eq1': 'y1 ~ x11 + x12',
    'eq2': 'y2 ~ x21 + x22'