加载数据
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
## 2015-01-02 161.31 163.31 161.00 162.06 5525500 127.5311
## 2015-01-05 161.27 161.27 159.19 159.51 4880400 125.5245
## 2015-01-06 159.67 159.96 155.17 156.07 6146700 122.8174
## 2015-01-07 157.20 157.20 154.03 155.05 4701800 122.0147
## 2015-01-08 156.24 159.04 155.55 158.42 4236800 124.6667
## 2015-01-09 158.42 160.34 157.25 159.11 4488300 125.2097
## IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
## 2020-07-02 119.69 121.42 119.26 119.70 3747000 119.70
## 2020-07-06 121.25 121.85 119.46 120.19 4045400 120.19
## 2020-07-07 119.00 119.51 117.33 117.58 4278100 117.58
## 2020-07-08 118.06 118.64 116.48 117.71 5192800 117.71
## 2020-07-09 118.00 118.00 115.20 115.71 4763000 115.71
## 2020-07-10 115.50 118.57 115.29 118.35 4285700 118.35
取股票收盘价IBM.Close建立模型
## IBM.Close
## 2015-01-02 162.06
## 2015-01-05 159.51
## 2015-01-06 156.07
## 2015-01-07 155.05
## 2015-01-08 158.42
## 2015-01-09 159.11
## [1] "xts" "zoo"
数据基本情况分析
## Loading required package: timeDate
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
##
## Attaching package: 'fBasics'
## The following object is masked from 'package:TTR':
##
## volatility
## IBM.Close
## nobs 1390.000000
## NAs 0.000000
## Minimum 94.769997
## Maximum 181.949997
## 1. Quartile 137.690002
## 3. Quartile 156.655002
## Mean 146.735612
## Median 146.705002
## Sum 203962.500233
## SE Mean 0.406378
## LCL Mean 145.938430
## UCL Mean 147.532793
## Variance 229.549084
## Stdev 15.150877
## Skewness -0.246567
## Kurtosis 0.006665
ADF平稳性检验
ADF检验的原假设为存在单位根(即不平稳)
下面对price和一阶差分后的price进行adf检验,p值都小于0.05,一阶差分后的p值甚至为0.01。
可以选择一阶差分后的数据进行建模,更为严谨。即arima(p,d,q)中的d=1
##
## Augmented Dickey-Fuller Test
##
## data: price
## Dickey-Fuller = -3.576, Lag order = 11, p-value = 0.03499
## alternative hypothesis: stationary
## Warning in adf.test(na.omit(diff(price))): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: na.omit(diff(price))
## Dickey-Fuller = -9.9506, Lag order = 11, p-value = 0.01
## alternative hypothesis: stationary
ACF和PACF
根据acf和pacf可以判断ARMA的阶数 AR(P):acf衰减趋于0,pacf p阶截尾 MA(q):acf q阶截尾,pacf衰减趋于0 ARMA(p,q):acf q阶后衰减趋于0,pacf p阶后衰减趋于0
arch效应检验
因为arch效应不会因为线性适配而消失,所以可以直接对原数据进行arch效应检验。
原假设:不存在arch效应,p值小于0.01,原假设被拒绝,存在arch效应
所以需要建立garch模型
##
## ARCH LM-test; Null hypothesis: no ARCH effects
##
## data: price
## Chi-squared = 1352.6, df = 12, p-value < 2.2e-16
模型估计
均值方程arima模型选择
根据acf和pacf选择对一阶差分的price建立arma(9,0)模型,即arima(9,1,0)
## Length Class Mode
## coef 9 -none- numeric
## sigma2 1 -none- numeric
## var.coef 81 -none- numeric
## mask 9 -none- logical
## loglik 1 -none- numeric
## aic 1 -none- numeric
## arma 7 -none- numeric
## residuals 1390 ts numeric
## call 3 -none- call
## series 1 -none- character
## code 1 -none- numeric
## n.cond 1 -none- numeric
## nobs 1 -none- numeric
## model 10 -none- list
自动适配arima模型,aic比自己建立的模型大,舍弃,还是用arima(9,1,0)
##
## Attaching package: 'forecast'
## The following object is masked from 'package:FinTS':
##
## Acf
## Series: price
## ARIMA(4,1,1)
##
## Coefficients:
## ar1 ar2 ar3 ar4 ma1
## -0.8500 -0.0293 0.0243 -0.0493 0.8011
## s.e. 0.0698 0.0355 0.0353 0.0285 0.0649
##
## sigma^2 estimated as 4.581: log likelihood=-3025.42
## AIC=6062.84 AICc=6062.9 BIC=6094.25
建立garch模型
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
garch模型的均值方程选择arima(9,1,0)
mean.spec <- list(armaOrder=c(9,1,0),include.mean=T,archm=F,archpow=1,arfima=F,
external.regressors=NULL)
var.spec <- list(model="sGARCH",garchOrder=c(2,1,submodel=NULL,external.regressors=NULL,
variance.targeting=F))
myspec <- ugarchspec(mean.model = mean.spec,variance.model = var.spec,
distribution.model = dist.spec)
根据“Weighted Ljung-Box Test on Standardized Squared Residuals”结果:残差不相关
根据“Weighted ARCH LM Tests”结果:不存在arch效应
根据“Asymptotic Critical Values (10% 5% 1%)”结果:原假设参数稳定 根据“Adjusted Pearson Goodness-of-Fit Test:”结果:学生t分布适配该模型
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(2,1)
## Mean Model : ARFIMA(9,0,1)
## Distribution : std
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 157.249290 0.658319 238.864925 0.000000
## ar1 1.243748 0.013657 91.072730 0.000000
## ar2 -0.254903 0.028203 -9.038203 0.000000
## ar3 0.021963 0.049786 0.441146 0.659107
## ar4 -0.026469 0.041758 -0.633869 0.526166
## ar5 -0.001765 0.040097 -0.044013 0.964894
## ar6 -0.026115 0.039202 -0.666156 0.505312
## ar7 0.049794 0.039035 1.275619 0.202090
## ar8 0.036085 0.037922 0.951574 0.341313
## ar9 -0.048520 0.023688 -2.048264 0.040534
## ma1 -0.300910 0.032174 -9.352571 0.000000
## omega 0.191567 0.099115 1.932782 0.053263
## alpha1 0.111383 0.053305 2.089541 0.036659
## alpha2 0.000000 0.068015 0.000000 1.000000
## beta1 0.855210 0.053881 15.872263 0.000000
## shape 3.677378 0.353156 10.412889 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 157.249290 0.467621 336.275252 0.000000
## ar1 1.243748 0.010067 123.542172 0.000000
## ar2 -0.254903 0.034202 -7.452839 0.000000
## ar3 0.021963 0.065548 0.335064 0.737576
## ar4 -0.026469 0.043260 -0.611864 0.540628
## ar5 -0.001765 0.037631 -0.046898 0.962595
## ar6 -0.026115 0.036714 -0.711300 0.476899
## ar7 0.049794 0.036584 1.361077 0.173489
## ar8 0.036085 0.035785 1.008392 0.313266
## ar9 -0.048520 0.023162 -2.094808 0.036188
## ma1 -0.300910 0.033528 -8.974944 0.000000
## omega 0.191567 0.162406 1.179561 0.238175
## alpha1 0.111383 0.061292 1.817262 0.069177
## alpha2 0.000000 0.099848 0.000000 1.000000
## beta1 0.855210 0.099545 8.591173 0.000000
## shape 3.677378 0.390268 9.422708 0.000000
##
## LogLikelihood : -2777.961
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.0201
## Bayes 4.0804
## Shibata 4.0198
## Hannan-Quinn 4.0426
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2233 0.6365
## Lag[2*(p+q)+(p+q)-1][29] 7.7704 1.0000
## Lag[4*(p+q)+(p+q)-1][49] 15.4919 0.9984
## d.o.f=10
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2496 0.6174
## Lag[2*(p+q)+(p+q)-1][8] 1.2848 0.9503
## Lag[4*(p+q)+(p+q)-1][14] 2.2917 0.9844
## d.o.f=3
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[4] 0.3662 0.500 2.000 0.5451
## ARCH Lag[6] 1.1284 1.461 1.711 0.7112
## ARCH Lag[8] 1.5105 2.368 1.583 0.8377
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 3.6903
## Individual Statistics:
## mu 0.006577
## ar1 0.084087
## ar2 0.087880
## ar3 0.067967
## ar4 0.083323
## ar5 0.091697
## ar6 0.087893
## ar7 0.090260
## ar8 0.076043
## ar9 0.083183
## ma1 0.082929
## omega 0.322465
## alpha1 0.502654
## alpha2 0.581832
## beta1 0.376002
## shape 0.645468
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 3.46 3.75 4.3
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.0406 0.2982
## Negative Sign Bias 0.3938 0.6938
## Positive Sign Bias 0.2071 0.8360
## Joint Effect 1.2571 0.7393
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 27.87 0.08595
## 2 30 29.45 0.44166
## 3 40 40.39 0.40876
## 4 50 52.59 0.33682
##
##
## Elapsed time : 0.700161
##
## please wait...calculating quantiles...
预测
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2020-07-10]:
## Series Sigma
## T+1 118.6 2.224
## T+2 118.8 2.230
## T+3 119.3 2.236
## T+4 119.7 2.242
## T+5 119.9 2.247
## T+6 120.1 2.252
## T+7 120.3 2.257
## T+8 120.7 2.262
## T+9 121.0 2.266
## T+10 121.3 2.271
注意:plot(fc)可以画出预测的图,但是无法knit出来