Exemplo n.º 1
0
def test_bivariate_eg_null(data, trend, method):
    if isinstance(data, pd.DataFrame):
        y, x = data.y, data.x
    else:
        x = data[:, 0]
        y = data[:, 1]
    test_yx = engle_granger(y, x, trend=trend, method=method)
    key = (trend, "y")
    assert_allclose(test_yx.stat, NULL_TEST_VALUES[key][0], rtol=1e-4)
    assert_allclose(test_yx.rho, 1 + NULL_TEST_VALUES[key][2], rtol=1e-4)
    assert_allclose(test_yx.pvalue, NULL_TEST_VALUES[key][1], rtol=1e-2)

    test_xy = engle_granger(x, y, trend=trend, method=method)
    key = (trend, "x")
    assert_allclose(test_xy.stat, NULL_TEST_VALUES[key][0], rtol=1e-4)
    assert_allclose(test_xy.rho, 1 + NULL_TEST_VALUES[key][2], rtol=1e-4)
    assert_allclose(test_xy.pvalue, NULL_TEST_VALUES[key][1], rtol=1e-2)
    s = str(test_xy)
    assert "Engle-Granger" in s
    assert "Statistic:" in s
    assert "ID:" not in s
    r = test_yx.__repr__()
    assert "ID:" in r
    assert test_yx.null_hypothesis == "No Cointegration"
    assert test_yx.alternative_hypothesis == "Cointegration"
    assert 1 in test_yx.critical_values
    assert 5 in test_yx.critical_values
    assert 10 in test_yx.critical_values
Exemplo n.º 2
0
def test_exceptions(data):
    if isinstance(data, pd.DataFrame):
        y, x = data.y, data.x
    else:
        y, x = data[:, :2].T
    with pytest.raises(ValueError, match="Unknown trend. Must be one of"):
        engle_granger(y, x, trend="nc")
Exemplo n.º 3
0
def test_trivariate(data, trend, method, lhs):
    rhs = ["x", "y", "z"]
    if isinstance(data, pd.DataFrame):
        rhs.remove(lhs)
        dep, exog = data[lhs], data[rhs]
    else:
        dep_loc = rhs.index(lhs)
        exog_locs = [0, 1, 2]
        exog_locs.remove(dep_loc)
        dep = data[:, dep_loc]
        exog = data[:, exog_locs]

    test = engle_granger(dep, exog, trend=trend, method=method)
    key = (lhs, trend)
    assert_allclose(test.stat, TRIVARIATE[key][0], rtol=1e-4)
    assert_allclose(test.rho, 1 + TRIVARIATE[key][2], rtol=1e-4)
    assert_allclose(test.pvalue, TRIVARIATE[key][1], rtol=2e-2)
    assert test.lags == 0
    assert isinstance(test.summary(), Summary)
    assert isinstance(test._repr_html_(), str)

    ci = test.cointegrating_vector
    assert isinstance(ci, pd.Series)

    if HAS_MATPLOTLIB:
        import matplotlib.pyplot as plt

        fig = test.plot()
        assert isinstance(fig, plt.Figure)
    assert isinstance(test.resid, pd.Series)
    assert test.resid.shape[0] == dep.shape[0]
Exemplo n.º 4
0
def test_bivariate_eg_alternative(data, trend, method):
    if isinstance(data, pd.DataFrame):
        y, w = data.y, data.w
    else:
        w = data[:, -1]
        y = data[:, 1]

    test_yw = engle_granger(y, w, trend=trend, method=method, max_lags=17)
    key = (method, trend, "y")
    assert_allclose(test_yw.stat, ALT_TEST_VALUES[key][0], rtol=1e-4)
    assert_allclose(test_yw.rho, 1 + ALT_TEST_VALUES[key][2], rtol=1e-4)
    assert_allclose(test_yw.pvalue, ALT_TEST_VALUES[key][1], atol=1e-3)
    assert test_yw.lags == ALT_TEST_VALUES[key][3]
    assert test_yw.max_lags == 17

    test_wy = engle_granger(w, y, trend=trend, method=method, max_lags=17)
    key = (method, trend, "w")
    assert_allclose(test_wy.stat, ALT_TEST_VALUES[key][0], rtol=1e-4)
    assert_allclose(test_wy.rho, 1 + ALT_TEST_VALUES[key][2], rtol=1e-4)
    assert_allclose(test_wy.pvalue, ALT_TEST_VALUES[key][1], atol=1e-3)
    assert test_wy.lags == ALT_TEST_VALUES[key][3]
Exemplo n.º 5
0
def test_plot(data):
    rhs = ["x", "y", "z"]
    lhs = "y"
    if isinstance(data, pd.DataFrame):
        rhs.remove(lhs)
        dep, exog = data[lhs], data[rhs]
    else:
        dep_loc = rhs.index(lhs)
        exog_locs = [0, 1, 2]
        exog_locs.remove(dep_loc)
        dep = data[:, dep_loc]
        exog = data[:, exog_locs]
    test = engle_granger(dep, exog)
    assert isinstance(test.plot(), plt.Figure)
Exemplo n.º 6
0
def test_name_ci_vector(data):
    if not isinstance(data, pd.DataFrame):
        return
    eg = engle_granger(data.w, data[["x", "y", "z"]])
    ci = eg.cointegrating_vector
    assert list(ci.index) == ["w", "x", "y", "z", "const"]