Exemple #1
0
def test_trends_low_memory(trend):
    rnd = np.random.RandomState(12345)
    y = np.cumsum(rnd.randn(250))
    adf = ADF(y, trend=trend, max_lags=16)
    adf2 = ADF(y, trend=trend, low_memory=True, max_lags=16)
    assert adf.lags == adf2.lags
    assert adf.max_lags == 16
    adf.max_lags = 1
    assert_equal(adf.lags, 1)
    assert_equal(adf.max_lags, 1)
Exemple #2
0
def test_representations(trend):
    rnd = np.random.RandomState(12345)
    y = np.cumsum(rnd.randn(250))
    adf = ADF(y, trend=trend, max_lags=16)
    check = 'Constant'
    if trend == 'nc':
        check = 'No Trend'
    assert check in adf.__repr__()
    assert check in adf.__repr__()
    assert check in adf._repr_html_()
    assert 'class="simpletable"' in adf._repr_html_()
Exemple #3
0
 def test_adf_auto_t_stat(self):
     adf = ADF(self.inflation, method='t-stat')
     assert_equal(adf.lags, 10)
     old_stat = adf.stat
     adf.lags += 1
     assert adf.stat != old_stat
     old_stat = adf.stat
     assert_equal(adf.y, self.inflation)
     adf.trend = 'ctt'
     assert adf.stat != old_stat
     assert adf.trend == 'ctt'
     assert len(adf.valid_trends) == len(('nc', 'c', 'ct', 'ctt'))
     for d in adf.valid_trends:
         assert d in ('nc', 'c', 'ct', 'ctt')
     assert adf.null_hypothesis == 'The process contains a unit root.'
     assert adf.alternative_hypothesis == 'The process is weakly stationary.'
Exemple #4
0
 def test_adf_lags_10(self):
     adf = ADF(self.inflation, lags=10)
     assert_almost_equal(adf.stat, -2.28375, DECIMAL_4)
     adf.summary()
Exemple #5
0
 def test_invalid_determinstic(self):
     adf = ADF(self.inflation)
     with pytest.raises(ValueError):
         adf.trend = 'bad-value'
Exemple #6
0
 def test_negative_lag(self):
     adf = ADF(self.inflation)
     with pytest.raises(ValueError):
         adf.lags = -1
Exemple #7
0
#查看前3行数据
CPI.head(n=3)
#查看后3行数据
CPI.tail(n=3)
CPI.shape

#剔除最后3期数据,构造用于建模的数据子集
CPItrain = CPI[3:]
CPItrain.head(n=3)
#绘制时序图,直观了解数据情况
CPI.sort_index().plot(title='CPI 2001-2014')

#进行ADF单位根检验,并查看结果;
CPItrain = CPItrain.dropna().CPI

print(ADF(CPItrain, max_lags=10).summary().as_text())
#lag即为上述检验表达式中的m,在这里我们选择检验12阶的自相关系数。
LjungBox = stattools.q_stat(stattools.acf(CPItrain)[1:12], len(CPItrain))
LjungBox[1][-1]

#将画面一分为二
axe1 = plt.subplot(121)
axe2 = plt.subplot(122)
#在第一个画面中画出序列的自相关系数图
plot1 = plot_acf(CPItrain, lags=30, ax=axe1)
#在第二个画面中画出序列的偏自相关系数图
plot2 = plot_pacf(CPItrain, lags=30, ax=axe2)

#order表示建立的模型的阶数,c(1,0,1)表示建立的是ARMA(1,1)模型;
#中间的数字0表示使用原始的、未进行过差分(差分次数为0)的数据;
#此处我们无需考虑它
Exemple #8
0
import numpy as np
import pandas as pd
import pandas_datareader as web
import matplotlib.pyplot as plt
import datetime

start = datetime.datetime(2018, 1, 1)
end = datetime.datetime(2019, 6, 30)
apple = web.DataReader('AAPL', 'yahoo', start, end)
apple = apple.dropna()
apple = pd.DataFrame(apple['Adj Close'].values,
                     index=pd.to_datetime(apple.index),
                     columns=['Price'])
apple = np.log(apple / apple.shift(1))
apple = apple.dropna()
apple.columns = ['Return']
apple.tail(3)
traindata = apple[:-3]
apple.plot()

from arch.unitroot import ADF
result = ADF(traindata.Return, max_lags=10)
print(result.summary().as_text())

from statsmodels.tsa import stattools
LjungBox = stattools.q_stat(stattools.acf(traindata)[1:12], len(traindata))
LjungBox[1][-1]

import statsmodels.graphics.tsaplots as ts
ts.plot_acf(traindata, use_vlines=True, lags=30)