コード例 #1
0
def test_autoreg_info_criterion(lag):
    data = sunspots.load(as_pandas=False)
    endog = data.endog
    endog_tmp = endog[16 - lag:]
    r = AutoReg(endog_tmp, lags=lag).fit()
    # See issue #324 for the corrections vs. R
    k_ar = len(r.model.ar_lags)
    k_trend = 1
    log_sigma2 = np.log(r.sigma2)
    aic = r.aic
    aic = (aic - log_sigma2) * (1 + k_ar) / (1 + k_ar + k_trend)
    aic += log_sigma2

    hqic = r.hqic
    hqic = (hqic - log_sigma2) * (1 + k_ar) / (1 + k_ar + k_trend)
    hqic += log_sigma2

    bic = r.bic
    bic = (bic - log_sigma2) * (1 + k_ar) / (1 + k_ar + k_trend)
    bic += log_sigma2

    res1 = np.array([aic, hqic, bic, r.fpe])
    # aic correction to match R
    res2 = results_ar.ARLagResults("const").ic.T

    assert_almost_equal(res1, res2[lag - 1, :], DECIMAL_6)

    r2 = AutoReg(endog, lags=lag, hold_back=16).fit()
    assert_allclose(r.aic, r2.aic)
    assert_allclose(r.bic, r2.bic)
    assert_allclose(r.hqic, r2.hqic)
    assert_allclose(r.fpe, r2.fpe)
コード例 #2
0
def test_ar_select_order_smoke():
    data = sunspots.load(as_pandas=True).data["SUNACTIVITY"]
    ar_select_order(data, 4, glob=True, trend="n")
    ar_select_order(data, 4, glob=False, trend="n")
    ar_select_order(data, 4, seasonal=True, period=12)
    ar_select_order(data, 4, seasonal=False)
    ar_select_order(data, 4, glob=True)
    ar_select_order(data, 4, glob=True, seasonal=True, period=12)
コード例 #3
0
def main():
    """
    Toy program to test the KPSS autolag method of Hobijn et al (1998).
    Unit tests using statsmodels data sets verified against SAS 9.3. To
    use, modify the following lines in main KPSS method:

      old:
        if lags is None:
            # from Kwiatkowski et al. referencing Schwert (1989)
            lags = int(np.ceil(12. * np.power(nobs / 100., 1 / 4.)))

      new:
        if lags is None:
            # autolag method of Hobijn et al. (1998)
            lags = _kpss_autolag(resids, nobs)
    """
    print("KPSS autolag method of Hobijn et al. (1998)")
    # real GDP from macrodata data set
    with warnings.catch_warnings(record=True) as w:
        res = kpss(macrodata.load().data['realgdp'], 'c')
    print("  realgdp('c'): stat =", "{0:0.5f}".format(res[0]), " pval =",
          "{0:0.5f}".format(res[1]), " lags =", format(res[2]))
    assert_almost_equal(res[0], 2.06851, decimal=3)
    assert_equal(res[2], 9)
    # sunspot activity from sunspots data set
    with warnings.catch_warnings(record=True) as w:
        res = kpss(sunspots.load().data['SUNACTIVITY'], 'c')
    print("  sunactivity('c'): stat =", "{0:0.5f}".format(res[0]), " pval =",
          "{0:0.5f}".format(res[1]), " lags =", format(res[2]))
    assert_almost_equal(res[0], 0.66987, decimal=3)
    assert_equal(res[2], 7)
    # volumes from nile data set
    with warnings.catch_warnings(record=True) as w:
        res = kpss(nile.load().data['volume'], 'c')
    print("  volume('c'): stat =", "{0:0.5f}".format(res[0]), " pval =",
          "{0:0.5f}".format(res[1]), " lags =", format(res[2]))
    assert_almost_equal(res[0], 0.86912, decimal=3)
    assert_equal(res[2], 5)
    # log-coinsurance from randhie data set
    with warnings.catch_warnings(record=True) as w:
        res = kpss(randhie.load().data['lncoins'], 'ct')
    print("  lncoins('ct'): stat =", "{0:0.5f}".format(res[0]), " pval =",
          "{0:0.5f}".format(res[1]), " lags =", format(res[2]))
    assert_almost_equal(res[0], 0.36762, decimal=3)
    assert_equal(res[2], 75)
    # in-vehicle time from modechoice data set
    with warnings.catch_warnings(record=True) as w:
        res = kpss(modechoice.load().data['invt'], 'ct')
    print("  invt('ct'): stat =", "{0:0.5f}".format(res[0]), " pval =",
          "{0:0.5f}".format(res[1]), " lags =", format(res[2]))
    assert_almost_equal(res[0], 0.40258, decimal=3)
    assert_equal(res[2], 18)
コード例 #4
0
 def test_lags(self):
     # real GDP from macrodata data set
     with pytest.warns(InterpolationWarning):
         res = kpss(self.x, 'c', nlags='auto')
     assert_equal(res[2], 9)
     # real interest rates from macrodata data set
     res = kpss(sunspots.load(True).data['SUNACTIVITY'], 'c', nlags='auto')
     assert_equal(res[2], 7)
     # volumes from nile data set
     with pytest.warns(InterpolationWarning):
         res = kpss(nile.load(True).data['volume'], 'c', nlags='auto')
     assert_equal(res[2], 5)
     # log-coinsurance from randhie data set
     with pytest.warns(InterpolationWarning):
         res = kpss(randhie.load(True).data['lncoins'], 'ct', nlags='auto')
     assert_equal(res[2], 75)
     # in-vehicle time from modechoice data set
     with pytest.warns(InterpolationWarning):
         res = kpss(modechoice.load(True).data['invt'], 'ct', nlags='auto')
     assert_equal(res[2], 18)
コード例 #5
0
 def test_lags(self):
     # real GDP from macrodata data set
     with pytest.warns(InterpolationWarning):
         res = kpss(self.x, "c", nlags="auto")
     assert_equal(res[2], 9)
     # real interest rates from macrodata data set
     res = kpss(sunspots.load().data["SUNACTIVITY"], "c", nlags="auto")
     assert_equal(res[2], 7)
     # volumes from nile data set
     with pytest.warns(InterpolationWarning):
         res = kpss(nile.load().data["volume"], "c", nlags="auto")
     assert_equal(res[2], 5)
     # log-coinsurance from randhie data set
     with pytest.warns(InterpolationWarning):
         res = kpss(randhie.load().data["lncoins"], "ct", nlags="auto")
     assert_equal(res[2], 75)
     # in-vehicle time from modechoice data set
     with pytest.warns(InterpolationWarning):
         res = kpss(modechoice.load().data["invt"], "ct", nlags="auto")
     assert_equal(res[2], 18)
コード例 #6
0
 def test_lags(self):
     # real GDP from macrodata data set
     with warnings.catch_warnings(record=True):
         lags = kpss(self.x, 'c', lags='auto')[2]
     assert_equal(lags, 9)
     # real interest rates from macrodata data set
     with warnings.catch_warnings(record=True):
         lags = kpss(sunspots.load().data['SUNACTIVITY'], 'c',
                     lags='auto')[2]
     assert_equal(lags, 7)
     # volumes from nile data set
     with warnings.catch_warnings(record=True):
         lags = kpss(nile.load().data['volume'], 'c', lags='auto')[2]
     assert_equal(lags, 5)
     # log-coinsurance from randhie data set
     with warnings.catch_warnings(record=True):
         lags = kpss(randhie.load().data['lncoins'], 'ct', lags='auto')[2]
     assert_equal(lags, 75)
     # in-vehicle time from modechoice data set
     with warnings.catch_warnings(record=True):
         lags = kpss(modechoice.load().data['invt'], 'ct', lags='auto')[2]
     assert_equal(lags, 18)
コード例 #7
0
 def test_lags(self):
     # real GDP from macrodata data set
     with warnings.catch_warnings(record=True):
         lags = kpss(self.x, 'c', lags='auto')[2]
     assert_equal(lags, 9)
     # real interest rates from macrodata data set
     with warnings.catch_warnings(record=True):
         lags = kpss(sunspots.load().data['SUNACTIVITY'], 'c',
                     lags='auto')[2]
     assert_equal(lags, 7)
     # volumes from nile data set
     with warnings.catch_warnings(record=True):
         lags = kpss(nile.load().data['volume'], 'c', lags='auto')[2]
     assert_equal(lags, 5)
     # log-coinsurance from randhie data set
     with warnings.catch_warnings(record=True):
         lags = kpss(randhie.load().data['lncoins'], 'ct', lags='auto')[2]
     assert_equal(lags, 75)
     # in-vehicle time from modechoice data set
     with warnings.catch_warnings(record=True):
         lags = kpss(modechoice.load().data['invt'], 'ct', lags='auto')[2]
     assert_equal(lags, 18)
コード例 #8
0
def test_autoreg_info_criterion(lag):
    data = sunspots.load()
    endog = np.asarray(data.endog)
    endog_tmp = endog[16 - lag:]
    r = AutoReg(endog_tmp, lags=lag).fit()
    # See issue #324 for the corrections vs. R
    aic = r.aic
    hqic = r.hqic
    bic = r.bic

    res1 = np.array([aic, hqic, bic, r.fpe])
    # aic correction to match R
    res2 = results_ar.ARLagResults("const").ic.T
    comp = res2[lag - 1, :].copy()
    k = 2 + lag
    pen = np.array([2, 2 * np.log(np.log(r.nobs)), np.log(r.nobs)])
    comp[:3] = -2 * r.llf + pen * k
    assert_almost_equal(res1, comp, DECIMAL_6)

    r2 = AutoReg(endog, lags=lag, hold_back=16).fit()
    assert_allclose(r.aic, r2.aic)
    assert_allclose(r.bic, r2.bic)
    assert_allclose(r.hqic, r2.hqic)
    assert_allclose(r.fpe, r2.fpe)
コード例 #9
0
 def setup_class(cls):
     data = sunspots.load(as_pandas=False)
     cls.res1 = AutoReg(data.endog, lags=9, trend="n").fit()
     cls.res2 = results_ar.ARResultsOLS(constant=False)
コード例 #10
0
 def setup_class(cls):
     data = sunspots.load(as_pandas=True)
     data.endog.index = list(range(len(data.endog)))
     cls.res1 = AutoReg(data.endog, lags=9).fit()
     cls.res2 = results_ar.ARResultsOLS(constant=True)
コード例 #11
0
ファイル: try_mlecov.py プロジェクト: timgates42/statsmodels
    y = arma_generate_sample(ar, ma, nobs, 2)
    y -= y.mean()  #I have not checked treatment of mean yet, so remove
    mod = MLEGLS(y)
    mod.nar, mod.nma = 2, 2  #needs to be added, no init method
    mod.nobs = len(y)
    res = mod.fit(start_params=[0.1, -0.8, 0.2, 0.1, 1.])
    print('DGP', ar, ma)
    print(res.params)
    from statsmodels.regression import yule_walker
    print(yule_walker(y, 2))
    #resi = mod.fit_invertible(start_params=[0.1,0,0.2,0, 0.5])
    #print(resi.params

    arpoly, mapoly = getpoly(mod, res.params[:-1])

    data = sunspots.load()
    #ys = data.endog[-100:]
    ##    ys = data.endog[12:]-data.endog[:-12]
    ##    ys -= ys.mean()
    ##    mods = MLEGLS(ys)
    ##    mods.nar, mods.nma = 13, 1   #needs to be added, no init method
    ##    mods.nobs = len(ys)
    ##    ress = mods.fit(start_params=np.r_[0.4, np.zeros(12), [0.2, 5.]],maxiter=200)
    ##    print(ress.params
    ##    import matplotlib.pyplot as plt
    ##    plt.plot(data.endog[1])
    ##    #plt.show()

    sigma = mod._params2cov(res.params[:-1], nobs) * res.params[-1]**2
    print(mvn_loglike(y, sigma))
    llo = mvn_nloglike_obs(y, sigma)
コード例 #12
0
 def setup_class(cls):
     data = sunspots.load()
     cls.res1 = AutoReg(np.asarray(data.endog), lags=9, trend="n").fit()
     cls.res2 = results_ar.ARResultsOLS(constant=False)
コード例 #13
0
    y = arma_generate_sample(ar, ma, nobs, 2)
    y -= y.mean()  #I have not checked treatment of mean yet, so remove
    mod = MLEGLS(y)
    mod.nar, mod.nma = 2, 2  #needs to be added, no init method
    mod.nobs = len(y)
    res = mod.fit(start_params=[0.1, -0.8, 0.2, 0.1, 1.])
    print('DGP', ar, ma)
    print(res.params)
    from statsmodels.regression import yule_walker
    print(yule_walker(y, 2))
    #resi = mod.fit_invertible(start_params=[0.1,0,0.2,0, 0.5])
    #print(resi.params

    arpoly, mapoly = getpoly(mod, res.params[:-1])

    data = sunspots.load(as_pandas=False)
    #ys = data.endog[-100:]
    ##    ys = data.endog[12:]-data.endog[:-12]
    ##    ys -= ys.mean()
    ##    mods = MLEGLS(ys)
    ##    mods.nar, mods.nma = 13, 1   #needs to be added, no init method
    ##    mods.nobs = len(ys)
    ##    ress = mods.fit(start_params=np.r_[0.4, np.zeros(12), [0.2, 5.]],maxiter=200)
    ##    print(ress.params
    ##    import matplotlib.pyplot as plt
    ##    plt.plot(data.endog[1])
    ##    #plt.show()

    sigma = mod._params2cov(res.params[:-1], nobs) * res.params[-1]**2
    print(mvn_loglike(y, sigma))
    llo = mvn_nloglike_obs(y, sigma)
コード例 #14
0
ファイル: test_unitroot.py プロジェクト: vasudeva-ram/arch
    cv_inf = mackinnoncrit()
    assert np.all(cv_50 <= cv_inf)


def test_adf_short_timeseries():
    # GH 262
    import numpy as np
    from arch.unitroot import ADF
    x = np.asarray([0., 0., 0., 0., 0., 0., 1., 1., 0., 0.])
    adf = ADF(x)
    assert_almost_equal(adf.stat, -2.236, decimal=3)
    assert adf.lags == 1


kpss_autolag_data = ((macrodata.load().data['realgdp'], 'c',
                      9), (sunspots.load().data['SUNACTIVITY'], 'c',
                           7), (nile.load().data['volume'], 'c',
                                5), (randhie.load().data['lncoins'], 'ct', 75),
                     (modechoice.load().data['invt'], 'ct', 18))


@pytest.mark.filterwarnings('ignore::DeprecationWarning')
@pytest.mark.parametrize('data,trend,lags', kpss_autolag_data)
def test_kpss_data_dependent_lags(data, trend, lags):
    # real GDP from macrodata data set
    kpss = KPSS(data, trend=trend)
    assert_equal(kpss.lags, lags)


za_test_result = namedtuple('za_test_result', [
    'stat',
コード例 #15
0
ファイル: test_unitroot.py プロジェクト: bashtage/arch
    cv_inf = mackinnoncrit()
    assert np.all(cv_50 <= cv_inf)


def test_adf_short_timeseries():
    # GH 262
    import numpy as np
    from arch.unitroot import ADF
    x = np.asarray([0., 0., 0., 0., 0., 0., 1., 1., 0., 0.])
    adf = ADF(x)
    assert_almost_equal(adf.stat, -2.236, decimal=3)
    assert adf.lags == 1


kpss_autolag_data = ((macrodata.load().data['realgdp'], 'c', 9),
                     (sunspots.load().data['SUNACTIVITY'], 'c', 7),
                     (nile.load().data['volume'], 'c', 5),
                     (randhie.load().data['lncoins'], 'ct', 75),
                     (modechoice.load().data['invt'], 'ct', 18))


@pytest.mark.filterwarnings('ignore::DeprecationWarning')
@pytest.mark.parametrize('data,trend,lags', kpss_autolag_data)
def test_kpss_data_dependent_lags(data, trend, lags):
    # real GDP from macrodata data set
    kpss = KPSS(data, trend=trend)
    assert_equal(kpss.lags, lags)


za_test_result = namedtuple('za_test_result',
                            ['stat', 'pvalue', 'lags', 'trend',