Exemplo n.º 1
0
def pp_test_fcn(data,maxlag):
    #@FORMAT: data = np(values)
    try:
        pp_fcn = UnitRoot.PhillipsPerron(data,maxlag)
        return pp_fcn.pvalue
    except:
        return np.nan
Exemplo n.º 2
0
def stationarity_tests(y_avg):
    ##    print('Results of Dickey-Fuller Test:')
    ##    dftest = adfuller(y_avg, autolag='AIC')
    ##    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    ##    for key,value in dftest[4].items():
    ##        dfoutput['Critical Value (%s)'%key] = value
    ##    print(dfoutput)

    ADF_arch = auni.ADF(y_avg)
    PP_arch = auni.PhillipsPerron(y_avg)
    KPSS_arch = auni.KPSS(y_avg)
    VR_arch = auni.VarianceRatio(y_avg)
    print(ADF_arch.summary)
    print(PP_arch.summary)
    print(KPSS_arch.summary)
    print(VR_arch.summary)
Exemplo n.º 3
0
def has_unit_root(ts, verbose=False):
    adf = unitroot.ADF(ts).pvalue > 0.05
    pp = unitroot.PhillipsPerron(ts).pvalue > 0.05
    kpss = unitroot.KPSS(ts).pvalue <= 0.05
    if verbose:
        print("Test for Unit Root:")
        # Exp: For instance a ‘shock’ dies away with stationarity but persists if non stationary.
        # statsmodels modules cuts the p value at 0.1 (has the same statistic as unitroot module)
        # print(f'Augmented Dickey-Fuller (null = I(1)): p-value = {smt.adfuller(ts)[1]:.4f}')
        print(
            f'>Augmented Dickey-Fuller (null = I(1)): p value = {unitroot.ADF(ts).pvalue:.4f}'
        )
        # print(f'KPSS (null = I(0)): p-value = {smt.kpss(ts, regression="c")[1]:.4f}')
        print(f'>KPSS (null = I(0)): p value = {unitroot.KPSS(ts).pvalue:.4f}'
              )  # tr='nc'/'c'/'ct'
        print(
            f'>Phillips-Perron (null = I(1)): p value = {unitroot.PhillipsPerron(ts).pvalue:.4f}'
        )
    return np.sum([adf, pp, kpss]) / 3.0
Exemplo n.º 4
0
def test_unitroot_phillips_perron(ts):
    '''Phillips-Perron test for presence of unit-root in a time series.'''
    print(au.PhillipsPerron(ts))
    print('\n-----------------------------------------------------\n')
Exemplo n.º 5
0
# 4.3. AUS-RSA Non-Stationary Prices
print('== AUS Prices Augmented Dickey-Fuller Test ==')
print('')
print(at.ADF(taus, trend='ct'))
print('')
print('== RSA Prices Augmented Dickey-Fuller Test ==')
print('')
print(at.ADF(trsa, trend='ct'))
print('')

# 4.4. AUS-RSA Stationary Price Differences
print('== AUS Prices Differences Augmented Dickey-Fuller Test ==')
print('')
print(at.ADF(taus.diff(1).dropna(), trend='ct'))
print('')
print('== RSA Prices Differences Augmented Dickey-Fuller Test ==')
print('')
print(at.ADF(trsa.diff(1).dropna(), trend='ct'))
print('')

# 4.5. AUS-RSA Spread Co-Integration Tests
print('== AUS-RSA Spread Augmented Dickey-Fuller Co-Integration Test ==')
print('')
print(at.ADF(tintsp3, trend='ct'))
print('')
print('== AUS-RSA Spread Phillips-Perron Co-Integration Test ==')
print('')
print(at.PhillipsPerron(tintsp3, trend='ct', test_type='rho'))
print('')
    print('kpss Statistic: %f' % result[0])
    print('p-value: %f' % result[1])
    print('lag parameter: %f' % result[2])
    for key, value in result[3].items():
        print('\t%s: %.3f' % (key, value))

    # test 3@ variance ratio (null hypothesis: random walk, possibly with drift)
    print("\nTEST 3: VARIANCE RATIO")
    print(a.VarianceRatio(X, lags=30, trend='c', debiased=True, robust=True, overlap=True).summary())

    # test 4 DF GLS (null: process contains a unit root)
    dfgls = a.DFGLS(X)
    print(dfgls.summary())

    # test 5
    pp = a.PhillipsPerron(X)
    pp.trend = 'ct'
    print(pp.summary())

    # series in conjunction
    ##########  Correlation between the two with smoothing ##########################

    # Lag plot
    plt.rcParams.update({'ytick.left': False, 'axes.titlepad': 10})

    fig, axes = plt.subplots(2, 4, figsize=(10, 3), sharex=True, sharey=True, dpi=100)
    for i, ax in enumerate(axes.flatten()[:]):
        lag_plot(all_data, lag=i + 1, ax=ax, c='firebrick')
        ax.set_title('Lag ' + str(i + 1))

    fig.suptitle(
def unit_root_test_wrapper(series, lags=None):
    """
    Main function to run multiple stationarity tests. Runs five tests and returns a summary table + decision
    based on the majority rule. If the number of tests that determine a series is stationary equals to the
    number of tests that deem it non-stationary, we assume the series is non-stationary.
        * Augmented Dickey-Fuller (ADF),
        * KPSS,
        * ADF using GLS,
        * Phillips-Perron (PP),
        * Zivot-Andrews (ZA)

    :param lags: (optional) parameter that allows user to run a series of tests for a specific lag value.
    :param series: series to test
    :return: dictionary of summary table for all tests and final decision on stationary vs nonstaionary
    """
    # setting for ADF and KPSS tests
    adf_settings = {'IC': 'AIC', 'store': True}

    kpss_settings = {'reg_type': 'c', 'lags': 'auto', 'store': True}

    arch_test_settings = {}  # settings for PP, ADF GLS and ZA tests
    if lags is not None:
        adf_settings.update({'lags': lags, 'autolag': None})
        kpss_settings.update({'lags:': lags})
        arch_test_settings = {'lags': lags}
    # Run individual tests
    adf = adf_test(series, **adf_settings)  # ADF test
    kpss = kpss_test(series, **kpss_settings)  # KPSS test
    pp = unitroot.PhillipsPerron(series,
                                 **arch_test_settings)  # Phillips-Perron test
    adfgls = unitroot.DFGLS(series, **arch_test_settings)  # ADF using GLS test
    za = unitroot.ZivotAndrews(series,
                               **arch_test_settings)  # Zivot-Andrews test

    # generate output table
    adf_dict = format_test_output(test_name='ADF',
                                  test_res=adf,
                                  H0_unit_root=True)
    kpss_dict = format_test_output(test_name='KPSS',
                                   test_res=kpss,
                                   H0_unit_root=False)
    pp_dict = format_test_output(test_name='Philips Perron',
                                 test_res=pp,
                                 H0_unit_root=True)
    adfgls_dict = format_test_output(test_name='ADF GLS',
                                     test_res=adfgls,
                                     H0_unit_root=True)
    za_dict = format_test_output(test_name='Zivot-Andrews',
                                 test_res=za,
                                 H0_unit_root=True)

    test_dict = {
        'ADF': adf_dict,
        'KPSS': kpss_dict,
        'PP': pp_dict,
        'ADF GLS': adfgls_dict,
        'ZA': za_dict
    }
    test_sum = pd.DataFrame.from_dict(test_dict,
                                      orient='index').reset_index(drop=True)

    # decision based on the majority rule
    if test_sum.shape[0] > 0:
        ratio = test_sum[test_sum["stationary"] ==
                         "yes"].shape[0] / test_sum.shape[0]
    else:
        ratio = 1  # all tests fail, assume the series is stationary

    # Majority rule. If the ratio is exactly 0.5, assume the series in non-stationary.
    stationary = 'YES' if (ratio > 0.5) else 'NO'

    out = {'summary': test_sum, 'stationary': stationary}
    return out
def unit_root_test_wrapper(series, lags=None):
    """
    Main function to run multiple stationarity tests. Runs five tests and returns a summary table + decision
    based on the majority rule. If the number of tests that determine a series is stationary equals to the
    number of tests that deem it non-stationary, we assume the series is non-stationary.
        * Augmented Dickey-Fuller (ADF),
        * KPSS,
        * ADF using GLS,
        * Phillips-Perron (PP),
        * Zivot-Andrews (ZA)

    :param lags: (optional) parameter that allows user to run a series of tests for a specific lag value.
    :param series: series to test
    :return: dictionary of summary table for all tests and final decision on stationary vs nonstaionary
    """
    # setting for ADF and KPSS tests
    adf_settings = {"IC": "AIC", "store": True}

    kpss_settings = {"reg_type": "c", "lags": "auto", "store": True}

    arch_test_settings = {}  # settings for PP, ADF GLS and ZA tests
    if lags is not None:
        adf_settings.update({"lags": lags, "autolag": None})
        kpss_settings.update({"lags:": lags})
        arch_test_settings = {"lags": lags}
    # Run individual tests
    adf = adf_test(series, **adf_settings)  # ADF test
    kpss = kpss_test(series, **kpss_settings)  # KPSS test
    pp = unitroot.PhillipsPerron(series,
                                 **arch_test_settings)  # Phillips-Perron test
    adfgls = unitroot.DFGLS(series, **arch_test_settings)  # ADF using GLS test
    za = unitroot.ZivotAndrews(series,
                               **arch_test_settings)  # Zivot-Andrews test

    # generate output table
    adf_dict = format_test_output(test_name="ADF",
                                  test_res=adf,
                                  H0_unit_root=True)
    kpss_dict = format_test_output(test_name="KPSS",
                                   test_res=kpss,
                                   H0_unit_root=False)
    pp_dict = format_test_output(test_name="Philips Perron",
                                 test_res=pp,
                                 H0_unit_root=True)
    adfgls_dict = format_test_output(test_name="ADF GLS",
                                     test_res=adfgls,
                                     H0_unit_root=True)
    za_dict = format_test_output(test_name="Zivot-Andrews",
                                 test_res=za,
                                 H0_unit_root=True)

    test_dict = {
        "ADF": adf_dict,
        "KPSS": kpss_dict,
        "PP": pp_dict,
        "ADF GLS": adfgls_dict,
        "ZA": za_dict,
    }
    test_sum = pd.DataFrame.from_dict(test_dict,
                                      orient="index").reset_index(drop=True)

    # decision based on the majority rule
    if test_sum.shape[0] > 0:
        ratio = test_sum[test_sum["stationary"] ==
                         "yes"].shape[0] / test_sum.shape[0]
    else:
        ratio = 1  # all tests fail, assume the series is stationary

    # Majority rule. If the ratio is exactly 0.5, assume the series in non-stationary.
    stationary = "YES" if (ratio > 0.5) else "NO"

    out = {"summary": test_sum, "stationary": stationary}
    return out