예제 #1
0
def test_kpss():
    test = KPSSTest(alpha=0.05, null='level', lshort=True)
    pval, is_sig = test.is_stationary(austres)
    assert is_sig  # show it is significant
    assert_almost_equal(pval, 0.01)

    # test the ndiffs with the KPSS test
    assert ndiffs(austres, test='kpss', max_d=2) == 2
예제 #2
0
def test_pp():
    test = PPTest(alpha=0.05, lshort=True)
    pval, is_sig = test.is_stationary(austres)
    assert is_sig
    assert_almost_equal(pval, 0.02139, decimal=5)

    # test n diffs
    nd = ndiffs(austres, test='pp', max_d=2)
    assert nd == 1
예제 #3
0
    def autoarima(self, data, pre_len=7):
        D_f = nsdiffs(data, m=3, max_D=5, test='ch')
        d_f = ndiffs(data, alpha=0.05, test='kpss', max_d=5)
        if len(data) <= 30:
            seasonal = False
        else:
            seasonal = True
        try:
            stepwise_fit = auto_arima(
                data,
                start_p=0,
                start_q=0,
                max_p=3,
                max_q=3,
                m=12,
                start_P=0,
                seasonal=seasonal,
                d=d_f,
                D=D_f,
                trace=False,
                error_action=
                'ignore',  # don't want to know if an order does not work
                suppress_warnings=True,  # don't want convergence warnings
                stepwise=True)  # set to stepwise
        except:
            stepwise_fit = auto_arima(
                data,
                start_p=0,
                start_q=0,
                max_p=3,
                max_q=0,
                m=12,
                start_P=0,
                seasonal=False,
                d=0,
                D=0,
                trace=False,
                error_action=
                'ignore',  # don't want to know if an order does not work
                suppress_warnings=True,  # don't want convergence warnings
                stepwise=True)  # set to stepwise
        output = stepwise_fit.predict(n_periods=pre_len).tolist()

        self._get_model({
            'model_name': 'autoarima',
            'model': stepwise_fit,
            'pred': output,
            'org_data': data,
            'pre_len': pre_len
        })

        return output
예제 #4
0
def test_ndiffs_stationary():
    # show that for a stationary vector, ndiffs returns 0
    x = np.ones(10)
    assert ndiffs(x, alpha=0.05, test='kpss', max_d=2) == 0
    assert ndiffs(x, alpha=0.05, test='pp', max_d=2) == 0
    assert ndiffs(x, alpha=0.05, test='adf', max_d=2) == 0
    #----------------------------------
    #exit()
    print('==-Order of (d,D) ===')
    r = []
    terr = [v for v in h_snl['STND_TRRTRY_NM'].unique()]
    for ter in terr:
        terrData = h_snl[h_snl.STND_TRRTRY_NM == ter]
        optionList = list(set(terrData.KEY))
        for i in optionList:
            print('In progress' + '\n' + ter + '-----' + str(i))
            r1 = pd.DataFrame()
            histData = h_snl[(h_snl.STND_TRRTRY_NM == ter) & (h_snl.KEY == i)]
            if len(histData) >= 52:
                sales = histData['RTL_QTY']
                try:
                    d = int(ndiffs(sales, test='adf'))
                    D = int(nsdiffs(sales, m=52, max_D=5))
                    r.append(ter + "_" + str(i) + "_" + str(d) + "_" + str(D))
                    print('== Difference algo works ==')
                except:
                    d = 1
                    D = 0
                    r.append(ter + "_" + str(i) + "_" + str(d) + "_" + str(D))
                    print('== Difference algo fails  ==')

            else:
                histData['d'] = 1
                histData['D'] = 0
                r.append(ter + "_" + str(i) + "_" + str(d) + "_" + str(D))
                print('== Less history ==')
예제 #6
0
def test_ndiffs_corner_cases():
    with pytest.raises(ValueError):
        ndiffs(austres, max_d=0)