Ejemplo n.º 1
0
def data(request):
    p, const, rho, common_exog, included_weights, output_dict = request.param
    if common_exog and isinstance(p, list):
        p = 3
    return generate_data(p=p, const=const, rho=rho,
                         common_exog=common_exog, included_weights=included_weights,
                         output_dict=output_dict)
Ejemplo n.º 2
0
def test_tvalues_homogeneity(method, cov_type):
    eqns = generate_data(k=3)
    mod = SUR(eqns)
    kwargs = {}

    base = direct_gls(eqns, 1)
    base_tstat = np.squeeze(base[0]) / np.sqrt(np.diag(base[1]))
    base_100 = direct_gls(eqns, 1 / 100)
    base_100_tstat = np.squeeze(base_100[0]) / np.sqrt(np.diag(base_100[1]))
    assert_allclose(base_tstat, base_100_tstat)

    if cov_type == "hac":
        kwargs["bandwidth"] = 1
    elif cov_type == "clustered":
        key0 = list(eqns.keys())[0]
        nobs = eqns[key0]["dependent"].shape[0]
        rs = np.random.RandomState(231823)
        kwargs["clusters"] = rs.randint(0, nobs // 5, size=(nobs, 1))
    res0 = mod.fit(method=method, cov_type=cov_type, **kwargs)
    for key in eqns:
        eqns[key]["dependent"] = eqns[key]["dependent"] / 100.0

    mod = SUR(eqns)
    res1 = mod.fit(method=method, cov_type=cov_type, **kwargs)
    assert_allclose(res0.tstats, res1.tstats)
    if cov_type == "robust" and method == "gls":
        assert_allclose(res0.tstats, base_tstat)
        assert_allclose(res1.tstats, base_100_tstat)
Ejemplo n.º 3
0
def missing_data(request):
    eqns = generate_data()
    np.random.seed(12345)
    missing = np.random.random_sample(500)
    missing = missing < request.param
    for key in eqns:
        eqns[key]['dependent'][missing] = np.nan
    return eqns
Ejemplo n.º 4
0
def test_restricted_f_statistic():
    data = generate_data(k=2, p=2)
    mod = SUR(data)
    r = DataFrame(np.zeros((1, 6)), columns=mod.param_names)
    r.iloc[0, 1] = 1.0
    mod.add_constraints(r)
    res = mod.fit()
    eqn = res.equations[res.equation_labels[0]]
    assert isinstance(eqn.f_statistic, InvalidTestStatistic)
Ejemplo n.º 5
0
def mvreg_data(request):
    const, rho, included_weights = request.param
    values = generate_data(const=const, rho=rho,
                           common_exog=True, included_weights=included_weights)
    dep = []
    for key in values:
        exog = values[key]['exog']
        dep.append(values[key]['dependent'])
    return np.hstack(dep), exog
Ejemplo n.º 6
0
def test_mv_ols_hac_smoke(kernel_options):
    data = generate_data(p=3, const=True, rho=0.8, common_exog=False,
                         included_weights=False, output_dict=True)
    mod = SUR(data)
    res = mod.fit(cov_type='kernel', **kernel_options)
    assert 'Kernel (HAC) ' in str(res)
    assert 'Kernel: {0}'.format(kernel_options['kernel']) in str(res)
    if kernel_options['bandwidth'] == 0:
        res_base = mod.fit(cov_type='robust', debiased=kernel_options['debiased'])
        assert_allclose(res.tstats, res_base.tstats)
Ejemplo n.º 7
0
def test_invalid_kernel_options(kernel_options):
    data = generate_data(p=3, const=True, rho=0.8, common_exog=False,
                         included_weights=False, output_dict=True)
    mod = SUR(data)
    with pytest.raises(TypeError):
        ko = {k: v for k, v in kernel_options.items()}
        ko['bandwidth'] = 'None'
        mod.fit(cov_type='kernel', **ko)
    with pytest.raises(TypeError):
        ko = {k: v for k, v in kernel_options.items()}
        ko['kernel'] = 1
        mod.fit(cov_type='kernel', **ko)
Ejemplo n.º 8
0
def test_system_r2_direct():
    eqns = generate_data(k=3)
    mod = SUR(eqns)
    res = mod.fit(method="ols", cov_type="unadjusted")
    y = np.hstack([eqns[eq]["dependent"] for eq in eqns])
    ref = reference_mcelroy(res.resids, y, res.sigma)
    assert_allclose(ref, res.system_rsquared.mcelroy)
    ref = reference_berndt(res.resids, y)
    assert_allclose(ref, res.system_rsquared.berndt)

    res = mod.fit(method="gls", cov_type="unadjusted", iter_limit=100)
    y = np.hstack([eqns[eq]["dependent"] for eq in eqns])
    ref = reference_mcelroy(res.resids, y, res.sigma)
    assert_allclose(ref, res.system_rsquared.mcelroy)
    ref = reference_berndt(res.resids, y)
    assert_allclose(ref, res.system_rsquared.berndt, atol=1e-3, rtol=1e-3)
Ejemplo n.º 9
0
def test_mv_ols_hac_smoke(kernel_options):
    data = generate_data(
        p=3,
        const=True,
        rho=0.8,
        common_exog=False,
        included_weights=False,
        output_dict=True,
    )
    mod = SUR(data)
    res = mod.fit(cov_type="kernel", **kernel_options)
    assert "Kernel (HAC) " in str(res)
    assert "Kernel: {0}".format(kernel_options["kernel"]) in str(res)
    if kernel_options["bandwidth"] == 0:
        res_base = mod.fit(cov_type="robust",
                           debiased=kernel_options["debiased"])
        assert_allclose(res.tstats, res_base.tstats)
Ejemplo n.º 10
0
def test_likelihood_ratio(k):
    eqns = generate_data(k=k)
    mod = SUR(eqns)
    res = mod.fit()
    stat = res.likelihood_ratio()
    if k == 1:
        assert isinstance(stat, InvalidTestStatistic)
        assert "Likelihood Ratio Test" in str(stat)
        assert np.isnan(stat.stat)
        return
    eps = np.asarray(res.resids)
    sigma = eps.T @ eps / eps.shape[0]
    nobs = res.resids.shape[0]
    direct = np.linalg.slogdet(sigma * np.eye(k))[1]
    direct -= np.linalg.slogdet(sigma)[1]
    direct *= nobs
    assert isinstance(stat, WaldTestStatistic)
    assert_allclose(stat.stat, direct)
    assert stat.df == 3
    assert_allclose(stat.pval, 1.0 - scipy.stats.chi2(3).cdf(direct))
    assert "Covariance is diagonal" in stat.null
    assert "Likelihood Ratio Test" in str(stat)
Ejemplo n.º 11
0
def test_brequsch_pagan(k):
    eqns = generate_data(k=k)
    mod = SUR(eqns)
    res = mod.fit()
    stat = res.breusch_pagan()
    if k == 1:
        assert isinstance(stat, InvalidTestStatistic)
        assert "Breusch-Pagan" in str(stat)
        assert np.isnan(stat.stat)
        return
    rho = np.asarray(res.resids.corr())
    nobs = res.resids.shape[0]
    direct = 0.0
    for i in range(3):
        for j in range(i + 1, 3):
            direct += rho[i, j]**2
    direct *= nobs
    assert isinstance(stat, WaldTestStatistic)
    assert_allclose(stat.stat, direct)
    assert stat.df == 3
    assert_allclose(stat.pval, 1.0 - scipy.stats.chi2(3).cdf(direct))
    assert "Residuals are uncorrelated" in stat.null
    assert "Breusch-Pagan" in str(stat)
Ejemplo n.º 12
0
matrix V = e(V)

estout matrix(V, fmt(%13.12g)) using {outfile}, append

file open myfile using {outfile}, write append
file write myfile  "*********** Sigma ****************" _n
file close myfile

matrix Sigma = e(Sigma)

estout matrix(Sigma, fmt(%13.12g)) using {outfile}, append
"""
output = output.format(outfile=OUTFILE)

data = generate_data(n=200, k=3, p=[2, 3, 4], const=True, seed=0)
common_data = generate_data(n=200, k=3, p=3, common_exog=True, seed=1)
missing_data = generate_data(n=200, k=3, p=[2, 3, 4], const=True, seed=2)

cmds = []
for i, dataset in enumerate((data, common_data, missing_data)):
    base = 'mod_{0}'.format(i)
    cmd = ''
    for j, key in enumerate(dataset):
        dep = dataset[key]['dependent']
        dep = pd.DataFrame(dep, columns=[base + '_y_{0}'.format(j)])
        exog = dataset[key]['exog'][:, 1:]
        exog_cols = [
            base + '_x_{0}{1}'.format(j, k) for k in range(exog.shape[1])
        ]
        exog = pd.DataFrame(exog, columns=exog_cols)
Ejemplo n.º 13
0
def test_unknown_method():
    mod = SUR(generate_data(k=3))
    with pytest.raises(ValueError, match="method must be 'ols' or 'gls'"):
        mod.fit(method="other")