2.6.3 MA模型定阶

案例:CRSP等权指数月度收益率(1926-2008)

  • 读入数据
library(readr)
d <- read_table(
  "D:/齐安静 教学/时间序列分析/北大/ftsdata/m-ibm3dx2608.txt",
  col_types=cols(.default=col_double(),
                 date=col_date(format="%Y%m%d")))
  • 查看数据
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(xts)
ibmind <- xts(as.matrix(d[,-1]), d$date) # d[,-1]是去除第一列的意思。
rm(d) #从内存删除数据d。
tclass(ibmind) <- "yearmon"
ew <- ts(coredata(ibmind)[,"ewrtn"], start=c(1926,1), frequency=12)
head(ibmind)
##             ibmrtn     vwrtn     ewrtn     sprtn
## Jan 1926 -0.010381  0.000724  0.023174  0.022472
## Feb 1926 -0.024476 -0.033374 -0.053510 -0.043956
## Mar 1926 -0.115591 -0.064341 -0.096824 -0.059113
## Apr 1926  0.089783  0.038358  0.032946  0.022688
## May 1926  0.036932  0.012172  0.001035  0.007679
## Jun 1926  0.068493  0.056888  0.050487  0.043184
  • 画折线图
plot(ew, main="CRSP Equal Weighted Index Monthly Return")
abline(h=0, col="gray")

  • acf图
forecast::Acf(ew, main="")
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

2.6.4 MA模型估计

resm1 <- arima(ew, order=c(0,0,9)); resm1
## 
## Call:
## arima(x = ew, order = c(0, 0, 9))
## 
## Coefficients:
##          ma1     ma2      ma3      ma4     ma5      ma6     ma7      ma8
##       0.2144  0.0374  -0.1203  -0.0425  0.0232  -0.0302  0.0482  -0.0276
## s.e.  0.0316  0.0321   0.0328   0.0336  0.0319   0.0318  0.0364   0.0354
##          ma9  intercept
##       0.1350     0.0122
## s.e.  0.0323     0.0028
## 
## sigma^2 estimated as 0.005043:  log likelihood = 1220.86,  aic = -2419.72
Box.test(resm1$residuals, type="Ljung", lag=12, fitdf=9)
## 
##  Box-Ljung test
## 
## data:  resm1$residuals
## X-squared = 6.0921, df = 3, p-value = 0.1072
resm1b <- arima(ew, order=c(0,0,9), 
                fixed=c(NA,0,NA,0,0,0,0,0,NA,NA))
resm1b
## 
## Call:
## arima(x = ew, order = c(0, 0, 9), fixed = c(NA, 0, NA, 0, 0, 0, 0, 0, NA, NA))
## 
## Coefficients:
##          ma1  ma2      ma3  ma4  ma5  ma6  ma7  ma8     ma9  intercept
##       0.1909    0  -0.1199    0    0    0    0    0  0.1227     0.0122
## s.e.  0.0293    0   0.0338    0    0    0    0    0  0.0312     0.0027
## 
## sigma^2 estimated as 0.005097:  log likelihood = 1215.61,  aic = -2421.22

2.6.5 预测

  • 利用前986期数据进行建模,留下10期数据进行验证
resm1c <- arima(ew[1:986], order=c(0,0,9), 
                fixed=c(NA,0,NA,0,0,0,0,0,NA,NA))
resm1c
## 
## Call:
## arima(x = ew[1:986], order = c(0, 0, 9), fixed = c(NA, 0, NA, 0, 0, 0, 0, 0, 
##     NA, NA))
## 
## Coefficients:
##          ma1  ma2      ma3  ma4  ma5  ma6  ma7  ma8     ma9  intercept
##       0.1844    0  -0.1206    0    0    0    0    0  0.1218     0.0128
## s.e.  0.0295    0   0.0338    0    0    0    0    0  0.0312     0.0027
## 
## sigma^2 estimated as 0.005066:  log likelihood = 1206.44,  aic = -2402.88
  • 预测
pred1c <- predict(resm1c, n.ahead=10, se.fit=TRUE)
tmp.tab <- cbind(Observed=round(c(ew[987:996]), 4), 
                 Predicted=round(c(pred1c$pred), 4), 
                 SE=round(c(pred1c$se), 4))
row.names(tmp.tab) <- sprintf("2008-%02d", 3:12)
tmp.tab
##         Observed Predicted     SE
## 2008-03  -0.0260    0.0043 0.0712
## 2008-04   0.0312    0.0136 0.0724
## 2008-05   0.0322    0.0150 0.0724
## 2008-06  -0.0871    0.0145 0.0729
## 2008-07  -0.0010    0.0120 0.0729
## 2008-08   0.0141    0.0018 0.0729
## 2008-09  -0.1209    0.0122 0.0729
## 2008-10  -0.2060    0.0055 0.0729
## 2008-11  -0.1366    0.0085 0.0729
## 2008-12   0.0431    0.0128 0.0734
  • 画图
plot(window(ew, start=c(2007, 7), end=c(2008,12)),
     ylab="", type="b", ylim=c(-0.2, 0.2), lwd=2)
lines(ts(c(pred1c$pred), start=c(2008,3), frequency = 12), 
      col="red", lwd=1, lty=2, type="b", pch=2)
lines(ts(c(pred1c$pred) - 2*c(pred1c$se), start=c(2008,3), frequency = 12), 
      col="blue", lwd=2, lty=3, type="l")
lines(ts(c(pred1c$pred) + 2*c(pred1c$se), start=c(2008,3), frequency = 12), 
      col="blue", lwd=2, lty=3, type="l")

  • 样本标准差
sd(c(ew[1:986]))
## [1] 0.07368157