import pandas
data = pandas.read_csv(
'D:\\DATA\\pycase\\number2\\9.3\\Data.csv' ,
index_col='日期'
)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data.plot()
from statsmodels.graphics.tsaplots import plot_acf
plot_acf(data).show()
from statsmodels.tsa.stattools import adfuller as ADF
def tagADF(t):
result = pandas.DataFrame(index=[
"Test Statistic Value", "p-value", "Lags Used",
"Number of Observations Used",
"Critical Value(1%)", "Critical Value(5%)", "Critical Value(10%)"
], columns=['销量']
);
result['销量']['Test Statistic Value'] = t[0]
result['销量']['p-value'] = t[1]
result['销量']['Lags Used'] = t[2]
result['销量']['Number of Observations Used'] = t[3]
result['销量']['Critical Value(1%)'] = t[4]['1%']
result['销量']['Critical Value(5%)'] = t[4]['5%']
result['销量']['Critical Value(10%)'] = t[4]['10%']
return result;
print('原始序列的ADF检验结果为:',tagADF(ADF(data[u'销量'])))
D_data = data.diff(1).dropna()
D_data.columns = [u'销量差分']
D_data.plot()
plt.show()
plot_acf(D_data).show()
plt.show()
from statsmodels.graphics.tsaplots import plot_pacf
plot_pacf(D_data).show()
print(u'差分序列的ADF检验结果为:', tagADF(ADF(D_data[u'销量差分'])))
from statsmodels.stats.diagnostic import acorr_ljungbox
print(u'差分序列的白噪声检验结果为:', acorr_ljungbox(D_data, lags=1))
from statsmodels.tsa.arima_model import ARIMA
pmax = int(len(D_data)/10)
qmax = int(len(D_data)/10)
bic_matrix = []
for p in range(pmax+1):
tmp = []
for q in range(qmax+1):
try:
tmp.append(ARIMA(data, (p,1,q)).fit().bic)
except:
tmp.append(None)
bic_matrix.append(tmp)
bic_matrix = pandas.DataFrame(bic_matrix)
p,q = bic_matrix.stack().idxmin()
print(u'BIC最小的p值和q值为:%s、%s' %(p,q))
model = ARIMA(data, (p,1,q)).fit()
model.summary2()
model.forecast(5)