在预测

时间:2016-08-09 00:52:55

标签: r time-series bayesian forecasting

我最近阅读了Steven Scott在Google上针对贝叶斯结构时间序列模型的bsts包,并希望尝试使用我一直用于各种预测任务的预测包中的auto.arima函数。

我尝试了几个例子,并对包的效率和点预测印象深刻。但是,当我查看预测方差时,我几乎总是发现,与auto.arima相比,bsts最终给出了更宽的置信区间。以下是白噪声数据的示例代码

library("forecast")
library("data.table")
library("bsts")
truthData = data.table(target = rnorm(250))
freq = 52
ss = AddGeneralizedLocalLinearTrend(list(), truthData$target)
ss = AddSeasonal(ss, truthData$target, nseasons = freq)
tStart = proc.time()[3]
model = bsts(truthData$target, state.specification = ss, niter = 500)
print(paste("time taken: ", proc.time()[3] - tStart))
burn = SuggestBurn(0.1, model)
pred = predict(model, horizon = 2 * freq, burn = burn, quantiles = c(0.10, 0.90))

## auto arima fit
max.d = 1; max.D = 1; max.p = 3; max.q = 3; max.P = 2; max.Q = 2; stepwise = FALSE
dataXts = ts(truthData$target, frequency = freq)
tStart = proc.time()[3]
autoArFit = auto.arima(dataXts, max.D = max.D, max.d = max.d, max.p = max.p, max.q = max.q, max.P = max.P, max.Q = max.P, stepwise = stepwise)
print(paste("time taken: ", proc.time()[3] - tStart))
par(mfrow = c(2, 1))
plot(pred, ylim = c(-5, 5))
plot(forecast(autoArFit, 2 * freq), ylim = c(-5, 5))

这是情节 forecast variance bsts top panel auto.arima lower panel 我想知道是否有人可以对这种行为有所了解以及我们如何控制预测方差。据我所知,Hyndman博士的论文中auto.arima的预测方差计算不考虑参数估计方差,即估计的ar和ma系数的方差。这是我在这里看到的差异的驱动原因还是我缺少其他微妙的点,可以通过一些参数来控制。

由于

这是一个测试中间范围预测问题的包含概率的脚本,将bsts与auto.arima进行比较

library("forecast")
library("data.table")
library("bsts")
set.seed(1234)
n = 260
freq = 52
h = 10
rep = 50
max.d = 1; max.D = 1; max.p = 2; max.q = 2; max.P = 1; max.Q = 1; stepwise = TRUE
containsProb = NULL
for (i in 1:rep) {
    print(i)
    truthData = data.table(time = 1:n, target = rnorm(n))
    yTrain = truthData$target[1:(n - h)]
    yTest = truthData$target[(n - h + 1):n]

    ## fit bsts model
    ss = AddLocalLevel(list(), truthData$target)
    ss = AddSeasonal(ss, truthData$target, nseasons = freq)
    tStart = proc.time()[3]
    model = bsts(yTrain, state.specification = ss, niter = 500)
    print(paste("time taken: ", proc.time()[3] - tStart))
    pred = predict(model, horizon = h, burn = SuggestBurn(0.1, model), quantiles = c(0.10, 0.90))
    containsProbBs = sum(yTest > pred$interval[1,] & yTest < pred$interval[2,]) / h

    ## auto.arima model fit
    dataTs = ts(yTrain, frequency = freq)
    tStart = proc.time()[3]
    autoArFit = auto.arima(dataTs, max.D = max.D, max.d = max.d, max.p = max.p, max.q = max.q, max.P = max.P, max.Q = max.P, stepwise = stepwise)
    print(paste("time taken: ", proc.time()[3] - tStart))
    fcst = forecast(autoArFit, h = h)

    ## inclusion probabilities for 80% CI
    containsProbBs = sum(yTest > pred$interval[1,] & yTest < pred$interval[2,]) / h
    containsProbAr = sum(yTest > fcst$lower[,1] & yTest < fcst$upper[,1]) / h
    containsProb = rbindlist(list(containsProb, data.table(bs = containsProbBs, ar = containsProbAr)))
}
colMeans(containsProb)
>  bs   ar 
 0.79 0.80 
c(sd(containsProb$bs), sd(containsProb$ar))
> [1] 0.13337719 0.09176629

1 个答案:

答案 0 :(得分:4)

不同之处在于BSTS模型是非平稳的,而在这种情况下选择的ARIMA模型是静止的(实际上只是白噪声)。对于BSTS模型,预测间隔在预测范围内继续扩大,而ARIMA模型具有恒定的预测间隔。对于第一个预测范围,它们相对接近,但它们在更长的视野中分歧。