from statsmodels.formula.api import ols
import matplotlib.pyplot as plt
import pandas as pd
from statsmodels.tsa.stattools import adfuller
import statsmodels.tsa.api as smt
from statsmodels.graphics.api import qqplot
from statsmodels.stats.stattools import durbin_watson
from sklearn.metrics import mean_squared_error
from pylab import mpl
import numpy as np
data1 = pd.read_excel('C://fangyongxiang/123/work1/实验3 综合实验1与2/香港港元存款余额月度数据.xls', index_col=0)
savings = data1['save']
time = data1['num']
fitting1 = ols('savings~time', data=savings).fit()
fig1 = plt.figure(figsize=(12, 8))
ax = plt.subplot()
y_fitted = fitting1.fittedvalues
ax.plot(time, savings, 'o', label='raw data')
ax.plot(time, y_fitted, 'r--', label='ols')
error1 = mean_squared_error(y_fitted, savings)
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
data2 = data1['saving1']
def del_nan(list):
a = list.dropna()
return a
for i in range(0,11):
print(adfuller(del_nan(data2.diff(i))))
print('一阶差分最平稳')
base = del_nan(data2.diff(1))
acf = smt.stattools.acf(base,nlags=10)
pacf = smt.stattools.pacf(base, nlags=10)
print(acf)
print(pacf)
def rrr(a):
with plt.style.context('bmh'):
plt.figure(figsize=(300,8))
时序图 = plt.subplot2grid((3, 1), (0,0))
data2.plot(ax=时序图)
时序图.set_title('时序图')
自相关图 = plt.subplot2grid((3,1), (1, 0))
smt.graphics.plot_acf(a, lags=10, ax=自相关图, alpha=0.05)
自相关图.set_title('自相关图')
偏自相关图 = plt.subplot2grid((3,1), (2, 0))
smt.graphics.plot_pacf(a, lags=10, ax=偏自相关图, alpha=0.05)
偏自相关图.set_title('偏相关图')
plt.tight_layout()
return
rrr(base)
plt.show()
print('三个可选模型为ARMA(0,2), ARMA(2,0), ARMA(2,2)')
model1 = smt.ARMA(base,(0,2)).fit()
model2 = smt.ARMA(base, (2,0)).fit()
print(model1.aic, model1.bic, model1.hqic)
print(model2.aic, model2.bic, model2.hqic)
print('modle2AIC,BIC,HQIC最小,选modle2')
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = smt.graphics.plot_acf(model1.resid.values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = smt.graphics.plot_pacf(model1.resid, lags=40, ax=ax2)
fig1 = plt.figure(figsize=(12,8))
ax3 = fig1.add_subplot(211)
fig1 = smt.graphics.plot_acf(model2.resid.values.squeeze(), lags=40, ax=ax3)
ax4 = fig1.add_subplot(212)
fig1 = smt.graphics.plot_pacf(model2.resid, lags=40, ax=ax4)
plt.show()
print('检验一阶自相关')
print(durbin_watson(model2.resid.values))
print('不存在(一阶)自相关性 ')
fig1 = plt.figure(figsize=(12,8))
ax = fig1.add_subplot(111)
fig1 = qqplot(model2.resid, line='q', ax=ax, fit=True)
print('一系列滞后相关检验')
r,q,p = smt.stattools.acf(model2.resid.values.squeeze(), qstat=True)
data = np.c_[range(1,41), r[1:], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
print('观察最后一列前十二行的检验概率')
predict_data = model2.predict('1999/2/12','2007/12/11' , dynamic=False)
model2_1_shift = base.shift(1)
recovery = predict_data.add(model2_1_shift)
print(recovery)
from statsmodels.tsa.arima_model import ARIMA
model2_ARIMA = ARIMA(base, order=(2,1,0))
model2_ARIMA_fit = model2_ARIMA.fit()
predict = model2_ARIMA_fit.forecast()[0]
print(predict)
error2 = mean_square_error(predict, base)
print('线性模型均方误差', 'ARIMA')
print(error1, error2)
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
import statsmodels.tsa.api as smt
from statsmodels.graphics.api import qqplot
from statsmodels.stats.stattools import durbin_watson
from pylab import mpl
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
data1 =pd.read_excel('c://fangyongxiang//123//work1//test1//123.xls', index_col=0)
data1 = data1['EUR/USD']
def del_nan(list):
a = list.dropna()
return a
for i in range(0,11):
print(adfuller(del_nan(data1.diff(i))))
print('一阶差分最平稳')
base = del_nan(data1.diff(1))
acf = smt.stattools.acf(base,nlags=40)
pacf = smt.stattools.pacf(base, nlags=40)
print(acf)
print(pacf)
def rrr(a):
with plt.style.context('bmh'):
plt.figure(figsize=(300,8))
时序图 = plt.subplot2grid((3, 1), (0,0))
data1.plot(ax=时序图)
时序图.set_title('时序图')
自相关图 = plt.subplot2grid((3,1), (1, 0))
smt.graphics.plot_acf(a, lags=40, ax=自相关图, alpha=0.05)
自相关图.set_title('自相关图')
偏自相关图 = plt.subplot2grid((3,1), (2, 0))
smt.graphics.plot_pacf(a, lags=40, ax=偏自相关图, alpha=0.05)
偏自相关图.set_title('偏相关图')
plt.tight_layout()
return
rrr(base)
print('三个可选模型为ARMA(0,1), ARMA(2,0), ARMA(2,1)')
model1 = smt.ARMA(base,(0,1)).fit()
model2 = smt.ARMA(base, (2,0)).fit()
model3 = smt.ARMA(base, (2,1)).fit()
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = smt.graphics.plot_acf(model1.resid.values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = smt.graphics.plot_pacf(model1.resid, lags=40, ax=ax2)
fig1 = plt.figure(figsize=(12,8))
ax3 = fig1.add_subplot(211)
fig1 = smt.graphics.plot_acf(model2.resid.values.squeeze(), lags=40, ax=ax3)
ax4 = fig1.add_subplot(212)
fig1 = smt.graphics.plot_pacf(model2.resid, lags=40, ax=ax4)
fig2 = plt.figure(figsize=(12,8))
ax5 = fig2.add_subplot(211)
fig2 = smt.graphics.plot_acf(model3.resid.values.squeeze(), lags=40, ax=ax5)
ax6 = fig2.add_subplot(212)
fig2 = smt.graphics.plot_pacf(model3.resid, lags=40, ax=ax6)
print(model1.aic, model1.bic, model1.hqic)
print(model2.aic, model2.bic, model2.hqic)
print(model3.aic, model3.bic, model3.hqic)
print('modle1AIC,BIC,HQIC最小,选modle1')
print('检验一阶自相关')
print(durbin_watson(model1.resid.values))
print('不存在(一阶)自相关性 ')
fig1 = plt.figure(figsize=(12,8))
ax = fig1.add_subplot(111)
fig1 = qqplot(model1.resid, line='q', ax=ax, fit=True)
print('一系列滞后相关检验')
r,q,p = smt.stattools.acf(model1.resid.values.squeeze(), qstat=True)
data = np.c_[range(1,41), r[1:41], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
print('观察最后一列前十二行的检验概率')
predict_data = model1.predict('1993/2/1','2007/12/1' , dynamic=False)
model1_1_shift = base.shift(1)
recovery = predict_data.add(model1_1_shift)
print(recovery)
from statsmodels.tsa.arima_model import ARIMA
model2_ARIMA = ARIMA(base, order=(1,1,0))
model2_ARIMA_fit = model2_ARIMA.fit()
predict = model2_ARIMA_fit.forecast()[0]
print(predict)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller
import statsmodels.tsa.api as smt
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.graphics.api import qqplot
from statsmodels.stats.stattools import durbin_watson
from pylab import mpl
import numpy as np
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
data1 =pd.read_excel('C://fangyongxiang/123/work1/实验2指导确定性趋势建模/Book1.xls', index_col=0)
data1 = data1['cxx']
def del_nan(list):
a = list.dropna()
return a
for i in range(0,11):
print(adfuller(del_nan(data1.diff(i))))
print('一阶差分最平稳')
base = del_nan(data1.diff(1))
acf = smt.stattools.acf(base,nlags=10)
pacf = smt.stattools.pacf(base, nlags=10)
print(acf)
print(pacf)
def rrr(a):
with plt.style.context('bmh'):
plt.figure(figsize=(300,8))
时序图 = plt.subplot2grid((3, 1), (0,0))
data1.plot(ax=时序图)
时序图.set_title('时序图')
自相关图 = plt.subplot2grid((3,1), (1, 0))
smt.graphics.plot_acf(a, lags=10, ax=自相关图, alpha=0.05)
自相关图.set_title('自相关图')
偏自相关图 = plt.subplot2grid((3,1), (2, 0))
smt.graphics.plot_pacf(a, lags=10, ax=偏自相关图, alpha=0.05)
偏自相关图.set_title('偏相关图')
plt.tight_layout()
return
rrr(base)
plt.show()
print('三个可选模型为ARMA(0,1), ARMA(2,0), ARMA(2,1)')
model1 = smt.ARMA(base,(0,1)).fit()
model2 = smt.ARMA(base, (1,0)).fit()
model3 = smt.ARMA(base, (1,1)).fit()
fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = smt.graphics.plot_acf(model1.resid.values.squeeze(), lags=40, ax=ax1)
ax2 = fig.add_subplot(212)
fig = smt.graphics.plot_pacf(model1.resid, lags=40, ax=ax2)
fig1 = plt.figure(figsize=(12,8))
ax3 = fig1.add_subplot(211)
fig1 = smt.graphics.plot_acf(model2.resid.values.squeeze(), lags=40, ax=ax3)
ax4 = fig1.add_subplot(212)
fig1 = smt.graphics.plot_pacf(model2.resid, lags=40, ax=ax4)
fig2 = plt.figure(figsize=(12,8))
ax5 = fig2.add_subplot(211)
fig2 = smt.graphics.plot_acf(model3.resid.values.squeeze(), lags=40, ax=ax5)
ax6 = fig2.add_subplot(212)
fig2 = smt.graphics.plot_pacf(model3.resid, lags=40, ax=ax6)
plt.show()
print(model1.aic, model1.bic, model1.hqic)
print(model2.aic, model2.bic, model2.hqic)
print(model3.aic, model3.bic, model3.hqic)
print('modle2AIC,BIC,HQIC最小,选modle2')
print('检验一阶自相关')
print(durbin_watson(model2.resid.values))
print('不存在(一阶)自相关性 ')
fig1 = plt.figure(figsize=(12,8))
ax = fig1.add_subplot(111)
fig1 = qqplot(model2.resid, line='q', ax=ax, fit=True)
print('一系列滞后相关检验')
r,q,p = smt.stattools.acf(model2.resid.values.squeeze(), qstat=True)
data = np.c_[range(1,28), r[1:28], q, p]
table = pd.DataFrame(data, columns=['lag', "AC", "Q", "Prob(>Q)"])
print(table.set_index('lag'))
print('观察最后一列前十二行的检验概率')
from statsmodels.tsa.arima_model import ARIMA
model2_ARIMA = ARIMA(base, order=(0,1,1))
model2_ARIMA_fit = model2_ARIMA.fit()
predict = model2_ARIMA_fit.forecast()[0]
print(predict)
plt.show()