# [R-SIG-Finance] Rugarch package using external regressors, a forecasting doubt

Diego Ignacio Acuña Rozas diego.acuna at usm.cl
Fri Jun 26 03:22:39 CEST 2015

```Thanks alexios, your example was a tremendous help to my case.

Best,

alexios wrote:
>
> As this appears to be a general point of confusion, see below a
> commented example:
>
> #########################
> library(rugarch)
> library(xts)
> data(sp500ret)
> spx<-xts(sp500ret, as.Date(rownames(sp500ret)))
> xreg<-xts(rnorm(nrow(spx)), index(spx))
> colnames(xreg)<-"xreg"
> # assume xreg is an actual series, so we lag it
> # as we would do in a real application
> xreg = lag(xreg,1)
> inputs<-na.omit(cbind(spx, xreg, join="left"))
> # real time forecasting
> spec<-ugarchspec(mean.model=list(external.regressors=inputs[1:2000,2]))
> fit<-ugarchfit(spec, inputs[1:2000,1])
> # 2 ways to do real-time forecasting (ugarchforecast and ugarchfilter)
> # Example: forecast points 2001:2020
> xforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020]))
> sforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020]))
> for(i in 1:20){
> # Forecast(T+1)|Information(T)
> # 1. Create a similar spec as you used in estimation
> # and add the lagged regressor upto time T
> specf1<-ugarchspec(mean.model=list(external.regressors=inputs[1:(2000+i-1),2]))
>
>
> # Pass the estimated coefficients from the estimation upto time 2000
> setfixed(specf1)<-as.list(coef(fit))
> # 2. Forecast using ugarchforecast on a specification with fixed
> parameters
> # where n.old is used in order to recreate the correct start-up
> conditions
> # used in the fitting routine
> # 3. Forecast using ugarchfilter on a specification with fixed
> parameters.
> # For this method, append a new row to the end of the data with zeros,
> # as you would do with related filters. This forces the routine to
> # output the value at time T+1
> newdat<-rbind(inputs[1:(2000+i-1),],xts(matrix(0, nrow=1, ncol=2),
> tail(move(index(inputs[1:(2000+i-1)])),1)))
> specf2<-ugarchspec(mean.model=list(external.regressors=newdat[,2]))
> setfixed(specf2)<-as.list(coef(fit))
> f2<-ugarchfilter(specf2, newdat[,1], n.old=2000)
> # fitted = estimated conditional mean values for uGARCHfit objects
> # fitted = forecast/filtered conditional mean values for
> uGARCHforecast/uGARCHfilter objects
> xforc[i,1] = as.numeric(fitted(f1))
> xforc[i,2] = as.numeric(tail(fitted(f2),1))
> # sigma = conditional sigma values (fitted/forecast etc)
> sforc[i,1] = as.numeric(sigma(f1))
> sforc[i,2] = as.numeric(tail(sigma(f2),1))
> }
> # check
> all.equal(xforc[,1], xforc[,2])
> all.equal(sforc[,1], sforc[,2])
> # check that the 1-ahead forecast directly from the fitted object is also
> # the same
> all.equal(as.numeric(xforc[1,1]), as.numeric(fitted(ugarchforecast(fit,
> all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit,
> # check the filter values vs the fitted values (i.e. why we use the
> n.old argument)
> all.equal(fitted(fit), fitted(f2)[1:2000])
> all.equal(sigma(fit), sigma(f2)[1:2000])
> #########################
>
>
> Regards,
>
> Alexios
>
> On 25/06/2015 21:11, Diego Ignacio Acuña Rozas wrote:
>>
>> Hi all, I'm using the rugarch package (which is btw an excellent piece
>> of software). I'm
>> using the A-PARCH(1,1) model with an external regressor (the Yang-Zhang
>> volatility proxy) and I've a doubt about how to perform forecasting
>> using the model:
>>
>> * Suppose I've n observations for the returns of a financial serie and
>> for the external regressor
>> * Then, I use ugarchspec with an A-PARCH(0,1) model with the external
>> regressor, the ugarchspec and ugarchfit steps work pretty well.
>> * Now I want to do forecasting. I don't quite fully understand the
>> limitation of this process. When I use ugarchforecast, the main
>> limitation is that I only can forecast 1-step ahead due to the external
>> regressor data limitation (I don't have more than n data for the
>> external regressor)???
>>
>> Now, if I fit a model with the n external regressor data and if I do a
>> 1-step ahead forecast for t+1, then the next day I recieve new data for
>> the external regressor, how to use the rugarch package to use this new
>> data and generate a new 1-step ahead forecast from t+1 to t+2? is this
>> possible?
>>
>> Thanks in advance, I've read a lot of the rugarch documentation and from
>> this mailing list, but I'm a bit lost regarding to my doubts.
>> Any help will be really appreciated.
>>
>> Best,
>>
>>
>

alexios wrote:
> As this appears to be a general point of confusion, see below a
> commented example:
>
> #########################
> library(rugarch)
> library(xts)
> data(sp500ret)
> spx<-xts(sp500ret, as.Date(rownames(sp500ret)))
> xreg<-xts(rnorm(nrow(spx)), index(spx))
> colnames(xreg)<-"xreg"
> # assume xreg is an actual series, so we lag it
> # as we would do in a real application
> xreg = lag(xreg,1)
> inputs<-na.omit(cbind(spx, xreg, join="left"))
> # real time forecasting
> spec<-ugarchspec(mean.model=list(external.regressors=inputs[1:2000,2]))
> fit<-ugarchfit(spec, inputs[1:2000,1])
> # 2 ways to do real-time forecasting (ugarchforecast and ugarchfilter)
> # Example: forecast points 2001:2020
> xforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020]))
> sforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020]))
> for(i in 1:20){
>   # Forecast(T+1)|Information(T)
>   # 1. Create a similar spec as you used in estimation
>   # and add the lagged regressor upto time T
> specf1<-ugarchspec(mean.model=list(external.regressors=inputs[1:(2000+i-1),2]))
>
>   # Pass the estimated coefficients from the estimation upto time 2000
>   setfixed(specf1)<-as.list(coef(fit))
>   # 2. Forecast using ugarchforecast on a specification with fixed
> parameters
>   # where n.old is used in order to recreate the correct start-up
> conditions
>   # used in the fitting routine
> n.old=2000)
>   # 3. Forecast using ugarchfilter on a specification with fixed
> parameters.
>   # For this method, append a new row to the end of the data with zeros,
>   # as you would do with related filters. This forces the routine to
>   # output the value at time T+1
>   newdat<-rbind(inputs[1:(2000+i-1),],xts(matrix(0, nrow=1, ncol=2),
> tail(move(index(inputs[1:(2000+i-1)])),1)))
>   specf2<-ugarchspec(mean.model=list(external.regressors=newdat[,2]))
>   setfixed(specf2)<-as.list(coef(fit))
>   f2<-ugarchfilter(specf2, newdat[,1], n.old=2000)
>   # fitted = estimated conditional mean values for uGARCHfit objects
>   # fitted = forecast/filtered conditional mean values for
> uGARCHforecast/uGARCHfilter objects
>   xforc[i,1] = as.numeric(fitted(f1))
>   xforc[i,2] = as.numeric(tail(fitted(f2),1))
>   # sigma = conditional sigma values (fitted/forecast etc)
>   sforc[i,1] = as.numeric(sigma(f1))
>   sforc[i,2] = as.numeric(tail(sigma(f2),1))
> }
> # check
> all.equal(xforc[,1], xforc[,2])
> all.equal(sforc[,1], sforc[,2])
> # check that the 1-ahead forecast directly from the fitted object is also
> # the same
> all.equal(as.numeric(xforc[1,1]), as.numeric(fitted(ugarchforecast(fit,
> all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit,
> # check the filter values vs the fitted values (i.e. why we use the
> n.old argument)
> all.equal(fitted(fit), fitted(f2)[1:2000])
> all.equal(sigma(fit), sigma(f2)[1:2000])
> #########################
>
>
> Regards,
>
> Alexios
>
> On 25/06/2015 21:11, Diego Ignacio Acuña Rozas wrote:
>> Hi all, I'm using the rugarch package (which is btw an excellent piece
>> of software). I'm
>> using the A-PARCH(1,1) model with an external regressor (the Yang-Zhang
>> volatility proxy) and I've a doubt about how to perform forecasting
>> using the model:
>>
>>   * Suppose I've n observations for the returns of a financial serie and
>> for the external regressor
>>   * Then, I use ugarchspec with an A-PARCH(0,1) model with the external
>> regressor, the ugarchspec and ugarchfit steps work pretty well.
>>   * Now I want to do forecasting. I don't quite fully understand the
>> limitation of this process. When I use ugarchforecast, the main
>> limitation is that I only can forecast 1-step ahead due to the external
>> regressor data limitation (I don't have more than n data for the
>> external regressor)???
>>
>> Now, if I fit a model with the n external regressor data and if I do a
>> 1-step ahead forecast for t+1, then the next day I recieve new data for
>> the external regressor, how to use the rugarch package to use this new
>> data and generate a new 1-step ahead forecast from t+1 to t+2? is this
>> possible?
>>
>> Thanks in advance, I've read a lot of the rugarch documentation and from
>> this mailing list, but I'm a bit lost regarding to my doubts.
>> Any help will be really appreciated.
>>
>> Best,
>>
>>
>

--
Diego Ignacio Acuña Rozas
Mg. (c) Ciencias de la Ingeniería Informática