Re: rugarch roll plot. why abs(mu) in plot?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: rugarch roll plot. why abs(mu) in plot?

Владимир Иванов
 
Hi. After fitting a rolling model and plotting the Series forecast all forecasted values are positive. Use this code to reproduce: http://www.unstarched.net/wp-content/uploads/2013/06/an-example-in-rugarch.pdf I would expect the series forecasts be both positive and negative.
 
 
After looking through the sources I found that it is a plot of abs(mu) against realized values. :
 
# rolling series forecast comparison plot
.plot.garchroll.3 = function(x, VaR.alpha = 0.01, density.support = c(-0.15, 0.15), ...)
{
density = x@forecast$density
plot(as.Date(rownames(density)), density[,6], type="l", col = "grey",
main = paste("Series Forecast vs Realized", sep = ""),
ylab = "", xlab  = "", cex.main = 0.7, cex.axis = 0.8, cex.lab=0.9)
lines(as.Date(rownames(density)), abs(density[,1]), col = "tomato1", lwd = 1.5)
grid()
invisible(x)
}
 
I think it there should be no abs . What do you think?

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: rugarch roll plot. why abs(mu) in plot?

alexios
Agreed, it shouldn’t...my bad for copying the code from another plot snippet.
Thanks for catching this. Will fix in next release.

Alexios


> On Nov 30, 2018, at 20:50, Владимир Иванов <[hidden email]> wrote:
>
>  
>>> Hi. After fitting a rolling model and plotting the Series forecast all forecasted values are positive. Use this code to reproduce: http://www.unstarched.net/wp-content/uploads/2013/06/an-example-in-rugarch.pdf I would expect the series forecasts be both positive and negative.
>>>  
>>> <series_plot.png>
>>>  
>>> After looking through the sources I found that it is a plot of abs(mu) against realized values. :
>>>  
>>> # rolling series forecast comparison plot
>>> .plot.garchroll.3 = function(x, VaR.alpha = 0.01, density.support = c(-0.15, 0.15), ...)
>>> {
>>> density = x@forecast$density
>>> plot(as.Date(rownames(density)), density[,6], type="l", col = "grey",
>>> main = paste("Series Forecast vs Realized", sep = ""),
>>> ylab = "", xlab  = "", cex.main = 0.7, cex.axis = 0.8, cex.lab=0.9)
>>> lines(as.Date(rownames(density)), abs(density[,1]), col = "tomato1", lwd = 1.5)
>>> grid()
>>> invisible(x)
>>> }
>>>  
>>> I think it there should be no abs . What do you think?
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

rugarch xts causes error

Владимир Иванов
In reply to this post by Владимир Иванов
 
Hi
 
Used example provided by Alexios in some thread
 
===========================
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 
  f1<-ugarchforecast(specf1, inputs[1:(2000+i-1),1], n.ahead=1, 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, 
                                                                   n.ahead=1)))) 
all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit, 
                                                                  n.ahead=1)))) 
# 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])
==========================
 
But, when running it i get an error:
 
> fit<-ugarchfit(spec, inputs[1:2000,1])
Error in pars[idx["mxreg", 1]:idx["mxreg", 2], 1] <- fit.mean[i] : 
  replacement has length zero

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: rugarch xts causes error

alexios
The external.regressors input must be a matrix not an xts object e.g. use:
spec<-ugarchspec(mean.model=list(external.regressors=coredata(inputs[1:2000,2])))

This will probably be relaxed in a future release.

Regards,

Alexios

On 12/3/18 4:45 AM, Владимир Иванов wrote:

> Hi
> Used example provided by Alexios in some thread
> ===========================
> 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
>    f1<-ugarchforecast(specf1, inputs[1:(2000+i-1),1], n.ahead=1,
> 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,
>                                                                    
>   n.ahead=1))))
> all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit,
>                                                                    
> n.ahead=1))))
> # 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])
> ==========================
> But, when running it i get an error:
>  > fit<-ugarchfit(spec, inputs[1:2000,1])
> Error in pars[idx["mxreg", 1]:idx["mxreg", 2], 1] <- fit.mean[i] :
>    replacement has length zero
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|

Re: rugarch xts causes error

Владимир Иванов
Thanks a lot -- that works for me


03.12.2018, 17:10, "alexios galanos" <[hidden email]>:

The external.regressors input must be a matrix not an xts object e.g. use:
spec<-ugarchspec(mean.model=list(external.regressors=coredata(inputs[1:2000,2])))

This will probably be relaxed in a future release.

Regards,

Alexios

On 12/3/18 4:45 AM, Владимир Иванов wrote:

 Hi
 Used example provided by Alexios in some thread
 ===========================
 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
    f1<-ugarchforecast(specf1, inputs[1:(2000+i-1),1], n.ahead=1,
 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,

   n.ahead=1))))
 all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit,

 n.ahead=1))))
 # 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])
 ==========================
 But, when running it i get an error:
  > fit<-ugarchfit(spec, inputs[1:2000,1])
 Error in pars[idx["mxreg", 1]:idx["mxreg", 2], 1] <- fit.mean[i] :
    replacement has length zero

 _______________________________________________
 [hidden email] mailing list
 https://stat.ethz.ch/mailman/listinfo/r-sig-finance
 -- Subscriber-posting only. If you want to post, subscribe first.
 -- Also note that this is not the r-help list where general R questions should go.


_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.