Financial Statistics and Econometrics: Homework 3

Financial Statistics and Econometrics(金融统计和计量)课程的第 3 次作业

前一次的作业可以点击这里: Financial Statistics and Econometrics: Homework 2

Econometrics

第1问和第2问

代码

# -*- coding: utf-8 -*-
"""
Created on Tue Nov  6 12:08:07 2018

@author: Wengsway

"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import statsmodels.api as sm
import warnings

# 1:导入数据并画出波动图
df = pd.read_csv("000002.csv",header = None,names = ['date','r'])
plt.plot(df['r'],label = 'r')
plt.hlines(np.mean(df['r']),0,835,colors = 'r',linestyles = "--",label = 'mean_r')
plt.legend(loc = 'best')
plt.title('Fluctuation Map')
plt.show()

# 2:用 ARMA模型来估计该时间序列
# 2.1:对序列进行平稳性检验
dftest = sm.tsa.stattools.adfuller(df['r'])
dfoutput = pd.Series(dftest[0:4],
                     index=['Test Statistic', 'p-value',
                            '#Lags Used', 'Number of observations Used'])
print(dfoutput)
print('从 ADF 检验结果可知该时间序列是平稳的')

# 2.2:确定AR模型的阶数
# 2.2.1:确定 p
train = df['r'][:-10]# 将数据划分为训练集和测试机
test = df['r'][-10:]
lag_pacf = sm.tsa.stattools.pacf(train, nlags=20, method='ols')
x1 = np.linspace(1,20,20)
plt.plot(x1,lag_pacf[1:],linestyle = '-', marker='o', mec='r')
plt.axhline(y=-(2/np.sqrt(len(df))), linestyle='--', color='gray')
plt.axhline(y=(2/np.sqrt(len(df))), linestyle='--', color='gray')
plt.title('Partial Autocorrelation of r')
plt.xlabel('Lag')
plt.show()
# 2.2.2:画出AR Model
results_AR = sm.tsa.ARMA(train,(11,0)).fit()
plt.plot(train, 'b', label = 'r')
plt.plot(results_AR.fittedvalues, 'r', label = 'AR model')
plt.legend(loc = 'best')
plt.title('The AR Model')
plt.show()
# 2.2.3:展示IC检验结果
aicList = []
bicList = []
hqicList = []
for i in range(1,8):  #从1阶开始算
    order = (i,0)  # 使用ARMA模型,其中MA阶数为0,只考虑了AR阶数。
    ricModel = sm.tsa.ARMA(train,order).fit()
    aicList.append(ricModel.aic)
    bicList.append(ricModel.bic)
    hqicList.append(ricModel.hqic)
plt.plot(aicList,'ro--',label='aic value')
plt.plot(bicList,'bo--',label='bic value')
plt.plot(hqicList,'ko--',label='hqic value')
plt.legend(loc = 'best')
plt.show()
# 2.2.4:AR模型的检验
delta_AR = results_AR.fittedvalues - train # 取得误差项
acf,q,p = sm.tsa.acf(delta_AR,nlags=10,qstat=True)  # 计算自相关系数 及p-value
out_AR = np.c_[range(1,11), acf[1:], q, p]
output_AR = pd.DataFrame(out_AR, columns=['lag', "ACF", "Q", "P-value"])
output_AR = output_AR.set_index('lag')
print(output_AR)
print('从白噪声检验结果可以看出误差项接近于白噪声序列')
adjR_AR = 1 - delta_AR.var()/train.var()
print('AR 模型的调整R^2为',adjR_AR)
# 2.2.5:AR模型的预测 
predicts_AR = results_AR.predict(825, 834, dynamic=True) #一共有835个数据
compare_AR = pd.DataFrame()
compare_AR['original'] = test
compare_AR['predict_AR'] = predicts_AR
print('AR Predict results:','\n',compare_AR)
plt.plot(compare_AR['original'],label = 'Original')
plt.plot(compare_AR['predict_AR'],label = 'AR_Predict')
plt.title('AR Model prediction results')
plt.legend(loc = 'best')
plt.show()

# 2.3:确定 MA 模型的阶数 
# 2.3.1:根据 AC 图确定 q 并画出 MA Model
# 确定 q
lag_acf = sm.tsa.stattools.acf(train, nlags=20)
x2 = np.linspace(1,20,20)
plt.plot(x2,lag_acf[1:],linestyle = '-', marker='o', mec='r')
plt.axhline(y=-(2/np.sqrt(len(df))), linestyle='--', color='gray')
plt.axhline(y=(2/np.sqrt(len(df))), linestyle='--', color='gray')
plt.title('Autocorrelation of r')
plt.xlabel('Lag')
plt.show()
# 画出 MA Model 
results_MA = sm.tsa.ARMA(train, (0,11)).fit()
plt.plot(df['r'], 'b', label = 'changepct')
plt.plot(results_MA.fittedvalues, 'r', label = 'MA model')
plt.title('The MA Model')
plt.legend(loc = 'best')
plt.show()
# 2.3.2:MA 模型的预测
predicts_MA = results_MA.predict(825, 834, dynamic=True) #一共有835个数据
compare_MA = pd.DataFrame()
compare_MA['original'] = test
compare_MA['predict_MA'] = predicts_MA
print('MA Predict results:','\n',compare_MA)
plt.plot(compare_MA['original'],label = 'Original')
plt.plot(compare_MA['predict_MA'],label = 'MA_Predict')
plt.title('MA Model prediction results')
plt.legend(loc = 'best')
plt.show()
# 2.3.3:MA 模型的检验
delta_MA = results_MA.fittedvalues - train  # 取得误差项
adjR_MA = 1 - delta_MA.var()/train.var()
print('MA 模型的调整R^2为',adjR_MA)
# 2.4: ARMA Model

warnings.filterwarnings("ignore")
AIC_output = sm.tsa.arma_order_select_ic(train,
                                         max_ar=5,
                                         max_ma=5,
                                         ic='aic')['aic_min_order']
print('以 AIC 最小原则选取的 p 和 q:',AIC_output)
BIC_output = sm.tsa.arma_order_select_ic(train,
                                         max_ar=5,
                                         max_ma=5,
                                         ic='bic')['bic_min_order']
print('以 BIC 最小原则选取的 p 和 q:',BIC_output)
HQIC_output = sm.tsa.arma_order_select_ic(train,
                                          max_ar=5,
                                          max_ma=5,
                                          ic='hqic')['hqic_min_order']
print('以 HQIC 最小原则选取的 p 和 q:',HQIC_output)
# 假如以AIC原则为准
results_ARMA_1 = sm.tsa.ARMA(train, (3,2)).fit()
print(results_ARMA_1.summary())
predicts_ARMA_1 = results_ARMA_1.predict(825, 834, dynamic = True)
compare_ARMA_1 = pd.DataFrame()
compare_ARMA_1['original'] = test
compare_ARMA_1['predict_ARMA_1'] = predicts_ARMA_1
print('ARMA(3,2) Predict results:','\n',compare_ARMA_1)
plt.plot(compare_ARMA_1['original'],label = 'Original')
plt.plot(compare_ARMA_1['predict_ARMA_1'],label = 'ARMA_Predict_1')
plt.title('ARMA(3,2) Model prediction results')
plt.legend(loc = 'best')
plt.show()
# ARMA模型的检验
delta_ARMA_1 = results_ARMA_1.fittedvalues - train # 拟合优度检验
adjR_ARMA_1 = 1 - delta_ARMA_1.var()/train.var()
print('ARMA(3,2)模型的调整R^2为',adjR_ARMA_1)

结果

Fluctuation Map
Fluctuation Map
Test Statistic                 -29.465798
p-value                          0.000000
#Lags Used                       0.000000
Number of observations Used    834.000000
dtype: float64

从 ADF 检验结果可知该时间序列是平稳的

Partial Autocorrelation of r
Partial Autocorrelation of r
The AR Model
The AR Model
The IC Results
The IC Results
           ACF         Q   P-value
lag                               
1.0  -0.000676  0.000378  0.984489
2.0  -0.002286  0.004709  0.997648
3.0   0.003245  0.013448  0.999587
4.0  -0.002859  0.020240  0.999949
5.0  -0.004877  0.040027  0.999983
6.0   0.002683  0.046024  0.999998
7.0  -0.000434  0.046181  1.000000
8.0  -0.001756  0.048756  1.000000
9.0   0.001566  0.050808  1.000000
10.0 -0.002783  0.057293  1.000000

从白噪声检验结果可以看出误差项接近于白噪声序列

AR 模型的调整R^2为 0.021315709346917178
AR Predict results: 
      original  predict_AR
825 -0.002321   -0.000411
826 -0.042345    0.010983
827  0.010690   -0.002776
828 -0.007692    0.004617
829  0.001938    0.005875
830 -0.057060    0.004500
831 -0.031282    0.007006
832  0.026998    0.008854
833 -0.011856    0.003430
834  0.071987    0.004828
AR Model prediction results
AR Model prediction results
Autocorrelation of r
Autocorrelation of r
 The MA Model
The MA Model
相关文章
Python Course homework 2

这是 Python 课程的第二次作业 其 阅读更多

Python Course homework 1

这是 Python 课程的第一次作业

3 个 Python 编程小练习

3 个 Python 小练习:(1)查询 阅读更多

发表评论