在 ARIMA (0,1,1) 中模拟预测值

机器算法验证 时间序列 预测 模拟
2022-03-27 06:42:01

我使用“预测”包拟合了带有漂移项的 ARIMA (0,1,1) 模型。我想进行模拟研究以获得预测值的平均值和 95% 的预测区间,并将它们与“精确”平均值和 95 个预测区间进行比较。下面的代码是我所做的。该图显示了模拟的和精确的结果。

问题:为什么基于模拟的置信区间上限和下限与准确值不同?虽然平均值接近准确值。我的错误在哪里?

我补充说,我知道这个包中提供了一个“模拟”功能来进行模拟。但是这个函数为我产生了 NA 并且不能正常工作,所以我试着自己做!

library(forecast)
f4=Arima(WWWusage,order=c(0,1,1),include.drift=TRUE)
mean.exact.f=forecast(f4,h=21)$mean
U.exact.f=forecast(f4,h=21)$upper[,2]
L.exact.f=forecast(f4,h=21)$lower[,2]
ff.sim=function(m,h,N){
sigma.est=m$sigma
ma1.est=coef(f4)[1]
drf.est=coef(f4)[2]
ff=matrix(0,ncol=N,nrow=(h+1))
for(j in 1:N){
              ff[1,j]=(m$x[length(m$x)])
               e=rnorm(h+1,0,sqrt(sigma.est))
                   for(i in 2:(h+1)){
                     ff[i,j]=ff[i-1,j]+drf.est+e[i-1]+ma1.est*e[i]
                                     }
              }
return(list(ff=ff))}
f.sim=ff.sim(f4,21,10000)$ff[-1,]
mean.sim=apply(f.sim,1,mean)
U.sim=apply(f.sim,1,quantile, probs = c(.95))
L.sim=apply(f.sim,1,quantile, probs = c(.05))
plot(1:21,mean.exact.f,type="l",ylim=range(mean.sim,U.sim,L.sim,mean.exact.f,U.exact.f,L.exact.f),xlab="t",col="blue",main="Simulated (green) & Exact (blue), forecasted mean and 95% C.I.")
points(1:21,U.exact.f,type="l",col="blue")
points(1:21,L.exact.f,type="l",col="blue")
points(1:21,mean.sim,type="l",col="green")
points(1:21,U.sim,type="l",col="green")
points(1:21,L.sim,type="l",col="green")

阴谋

1个回答

您的代码有几个问题:

  1. 默认预测区间为 80% 和 95%,但您模拟 90% 区间。
  2. 预测应使用第一次预测中的最后一个残差。
  3. 您的 ma 系数适用于et而不是et1.

这是一些更正的代码:

library(forecast)
f4 <- Arima(WWWusage, order=c(0,1,1), include.drift=TRUE)
f4.f <- forecast(f4, h=21, level=90)
mean.exact.f <- f4.f$mean
    U.exact.f <- f4.f$upper
L.exact.f <- f4.f$lower

ff.sim <- function(m,h,N)
{
  sigma.est <- m$sigma
      ma1.est <- coef(m)[1]
      drf.est <- coef(m)[2]
      ff <- matrix(0,ncol=N,nrow=(h+1))
      for(j in 1:N)
      {
        ff[1,j] <- m$x[length(m$x)]
        e <- c(m$residuals[length(m$x)],rnorm(h,0,sqrt(sigma.est)))
    for(i in 2:(h+1))
      ff[i,j] <- ff[i-1,j]+drf.est+ma1.est*e[i-1]+e[i]
  }
  return(list(ff=ff[-1,]))
}

f.sim <- ff.sim(f4,21,10000)$ff
mean.sim <-apply(f.sim,1,mean)
U.sim <-apply(f.sim,1,quantile, probs = c(.95))
L.sim <-apply(f.sim,1,quantile, probs = c(.05))
plot(1:21,mean.exact.f,type="l", 
  ylim=range(mean.sim,U.sim,L.sim,mean.exact.f,U.exact.f,L.exact.f), xlab="t",
  col="blue", main="Simulated (green) & Exact (blue), forecasted mean and 95% C.I.")
points(1:21,U.exact.f,type="l",col="blue")
points(1:21,L.exact.f,type="l",col="blue")
points(1:21,mean.sim,type="l",col="green")
points(1:21,U.sim,type="l",col="green")
points(1:21,L.sim,type="l",col="green")

你现在应该得到几乎相同的蓝线和绿线。