我一直在努力将ARIMA-GARCH模型与2005年至2008年底的S&P 500历史数据相适应。该代码主要基于Mike Halls-Moore撰写的“ Advanced Algorithmic Trading”一书。这个想法很简单。我们首先将arma模型拟合至前500天的对数回报,然后选择最佳参数p,q。使用来自'rugarch'库的ugarchfit()将这些参数作为GARCH模型的输入。然后使用ugarchforecast()进行预测。
我遇到的问题是,连续运行两次相同的代码会在大约3%的时间内产生不同的预测。这是我正在使用的代码:
# Import libraries
library(quantmod)
library(lattice)
library(timeSeries)
library(rugarch)
# Get the S&P 500 data
getSymbols("^GSPC", from="2003-01-01")
spReturns = diff(log(Cl(GSPC["2003-01-01/2009"])))
spReturns[as.character(head(index(Cl(GSPC["2003-01-01/2009"])),1))] = 0
# Create a vector of the forecasts
windowLength = 500 # The rolling window used to make the predictions
foreLength = length(spReturns) - windowLength
forecasts <- vector(mode="character", length=1)
# r stands for the rolling window
for (r in 0:foreLength) {
# We first select the best ARMA model based on the previous days
spReturnsOffset = spReturns[(1+r):(windowLength+r)]
final.aic <- Inf
final.order <- c(0,0,0)
for (p in 0:5) for (q in 0:5) {
if (p==0 && q==0) {next}
arimaFit = tryCatch(arima(spReturnsOffset, order=c(p, 0, q)), error=function(err) FALSE, warning=function(err) FALSE)
if(!is.logical(arimaFit)) {
current.aic <- AIC(arimaFit)
if (current.aic < final.aic) {
final.aic <- current.aic
final.order <- c(p, 0, q)
final.arima <- arimaFit
}
} else {next}
}
# We now do the ARMA-GARCH fitting, where we pass the best ARMA order found above
spec = ugarchspec(variance.model=list(garchOrder=c(1,1)), mean.model=list(armaOrder=c(final.order[1],final.order[3]),include.mean=T), distribution.model="sged")
fit = tryCatch(ugarchfit(spec, spReturnsOffset, solver='hybrid'), error=function(e) e, warning=function(w) w)
# If the fit does not converge, take 0 as our forecast
if(is(fit, "warning")) {forecasts[r+1] = 0}
else {
fore = ugarchforecast(fit, n.ahead=1)
forecasts[r+1] = fore@forecast$seriesFor[1]
}
}
我连续两次运行代码,结果发现两次运行之间的预测向量中的一些值不同。我特别注意到以下内容:
所有这些行为令我感到困惑。相同的输入参数是否总会导致相同的结果/错误? ugarchfit中是否使用了一些随机种子?
最终是否可以归因于ugarchfit中的resolver ='hybrid'参数?但是话又说回来,我不确定在这里使用什么其他求解器,文档对此也很含糊。