我正在尝试在R中实施DCC(动态条件相关)预测。但是,当我尝试一些测试值时,与Pearson相关性相比,预测似乎不合适,特别是在预测未来两年以上的相关性时。我对R很新,所以我真的非常感谢任何帮助!
我正在使用rmgarch包(在此处找到:https://cran.r-project.org/web/packages/rmgarch/index.html)。
这是我的代码:
library(quantmod)
library(rmgarch)
library(bizdays)
library(timeDate)
forecast <- function(ticker1, ticker2, start, end1, end2)
{
startingYear = as.numeric(format(as.yearmon(end1), "%Y"))
endingYear = as.numeric(format(as.yearmon(end2), "%Y"))
hol = as.POSIXlt(holidayNYSE(startingYear))
hol = as.character(hol)
year = startingYear + 1
while (year < endingYear)
{
temp = as.POSIXlt(holidayNYSE(year))
temp = as.character(temp)
hol = c(hol, temp)
year = year + 1
}
#cal <- Calendar(holidays = hol, start.date = end1, end.date = end2, weekdays = c("saturday", "sunday"))
#n.ahead = bizdays(end1, end2, cal)
n.ahead = 252 * (endingYear - startingYear)
dat1 = getSymbols(ticker1, src = 'yahoo', from = start, to = end1, auto.assign=F)
dat2 = getSymbols(ticker2, src = 'yahoo', from = start, to = end1, auto.assign=F)
dat1 = dat1[,4]
dat2 = dat2[,4]
eps <- data.frame(s1=dat1, s2=dat2)
spec = ugarchspec()
uspec = multispec(replicate(2, spec))
spec = dccspec(uspec)
fit = dccfit(spec, eps)
result = dccforecast(fit, n.ahead)
return(result)
}
con <- file("test.log")
sink(con, append = TRUE)
sink(con, append = TRUE, type = "message")
source("script.R", echo = TRUE, max.deparse.length = 10000)
startYear = 1998
endYear = 2014
trainingPeriod = 5
forecastPeriod = 5
for (i in startYear:endYear)
{
result = forecast('^GSPC', '^RUT', paste(i-trainingPeriod-forecastPeriod, '-01-01', sep=""), paste(i-forecastPeriod, '-01-01', sep=""), paste(i, '-01-01', sep=""))
print(result)
result = forecast('^GSPC', '^RUT', paste(i-trainingPeriod-forecastPeriod, '-04-01', sep=""), paste(i-forecastPeriod, '-04-01', sep=""), paste(i, '-04-01', sep=""))
print(result)
result = forecast('^GSPC', '^RUT', paste(i-trainingPeriod-forecastPeriod, '-07-01', sep=""), paste(i-forecastPeriod, '-07-01', sep=""), paste(i, '-07-01', sep=""))
print(result)
result = forecast('^GSPC', '^RUT', paste(i-trainingPeriod-forecastPeriod, '-10-01', sep=""), paste(i-forecastPeriod, '-10-01', sep=""), paste(i, '-10-01', sep=""))
print(result)
print("====================================================================================")
}
sink()
sink(type="message")
有谁知道会出现什么问题?我意识到DCC对于更长的预测范围变得越来越不准确,但我不确定这是否会导致问题。非常感谢任何建议。