Skip to content

rugarch VaR calculation "manually"

12 messages · Alexios Ghalanos, Neuman Co, Brian G. Peterson

#
I am using the rugarch package in R and I have some questions:

I want to use the rugarch package to calculate the VaR.

I used the following code to to fit a certain model:

spec2<-ugarchspec(variance.model = list(model = "sGARCH", garchOrder =
c(1, 1)),
mean.model = list(armaOrder = c(5, 5), include.mean = FALSE),
distribution.model =
"norm",fixed.pars=list(ar1=0,ar2=0,ar3=0,ma1=0,ma2=0,ma3=0))

model2<-ugarchfit(spec=spec2,data=mydata)

Now I can look at the 2.5 % VaR with the following command:
plot(model)

and choosing the second plot.

Now my first question is: How can I get the 1.0% VaR VALUES, so not
the plot, but
the values/numbers?

In case of the normal distribution, one can easily do the calculation of
the VaR with using the forecasted conditional volatility and the forecasted
conditional mean:

I use the ugarchforecast command and with that I can get the cond. volatility
and cond. mean (my mean equation is an modified ARMA(5,5), see above in the spec
command):

forecast = ugarchforecast(spec, data, n.roll = , n.ahead = , out.sample=)

# conditional mean
cmu = as.numeric(as.data.frame(forecast, which = "series",
rollframe="all", aligned = FALSE))
# conditional sigma
csigma = as.numeric(as.data.frame(forecast, which = "sigma",
rollframe="all", aligned = FALSE))

I can calculate the VaR by using the property, that the normal distribution
is part of the location-scale distribution families

# use location+scaling transformation property of normal distribution:
VaR = qnorm(0.01)*csigma + cmu

My second question belongs to the n.roll and out.sample command. I had
a look at the
description http://www.inside-r.org/packages/cran/rugarch/docs/ugarchforecast
but I did not understand the n.roll and out.sample command. I want to
calculate the
daily VaR, so I need one step ahead predicitons and I do not want to reestimate
the model every time step. So what does it mean "to roll the forecast
1 step " and
what is out.sample?

My third question(s) is (are): How to calculate the VaR in case of a
standardized
hyperbolic distribution? Can I still calculate it like in the normal case
or does it not work anymore (I am not sure, if the sdhyp belongs to the
location-scale family).

Even if it does work (so if the sdhyp belongs to the family of
location-scale distributions) how do I calculate it in case of a
distribution, which does not belong to the location-scale family?
(I mean, if I cannot calculate it via VaR = uncmean + sigma* z_alpha how
do I have to calculate it). Which distribution implemented in the rugarch
package does not belong to the location-scale family?

Thanks a lot for your help!
#
Hello,
On 07/05/2013 12:15, Neuman Co wrote:
Use the 'quantile' method i.e. 'quantile(model2, 0.01)'...also applies 
to uGARCHforecast, uGARCHsim etc It IS documented.
NO. 'as.data.frame' has long been deprecated. Use the 'sigma' and 
'fitted' methods (and make sure you upgrade to latest version of rugarch).
out.sample, which is used in the estimation stage retains 'out.sample' 
data (i.e. they are not used in the estimation) so that a rolling 
forecast can then be performed using this data. 'Rolling' means using 
data at time T-p (p=lag) to create a conditional 1-ahead forecast at 
time T. For the ugarchforecast method, this means using only estimates 
of the parameters from length(data)-out.sample. There is no 
re-estimation taking place (this is only done in the ugarchroll method).
For n.ahead>1, this becomes an unconditional forecast. Equivalently, you 
can append new data to your old dataset and use the ugarchfilter method.
ALL distributions in the rugarch package are represented in a location- 
and scale- invariant parameterization since this is a key property 
required in working with the standardized residuals in the density 
function (i.e. the subtraction of the mean and scaling by the volatility).
The standardized Generalized Hyperbolic distribution does indeed have 
this property, and details are available in the vignette. See also paper 
by Blaesild (http://biomet.oxfordjournals.org/content/68/1/251.short) 
for the linear transformation (aX+b) property.

If working with the standard (NOT standardized) version of the GH 
distribution (\lambda, \alpha, \beta, \delta, \mu) you need to apply the 
location/scaling transformation as the example below shows which is 
equivalent to just using the location/scaling transformation in the 
standardized version:
#################################################
library(rugarch)
# zeta = shape
zeta = 0.4
# rho = skew
rho = 0.9
# lambda = GIG mixing distribution shape parameter
lambda=-1
# POSITIVE scaling factor (sigma is in any always positive)
# mean
scaling = 0.02
# sigma
location = 0.001

# standardized transformation based on (0,1,\rho,\zeta)
# parameterization:
x1 = scaling*qdist("ghyp", seq(0.001, 0.5, length.out=100), shape = 
zeta, skew = rho, lambda = lambda)+location
# Equivalent to standard transformation:
# First obtain the standard parameters (which have a mean of zero
# and sigma of 1).
parms = rugarch:::.paramGH(zeta, rho, lambda)
x2 = rugarch:::.qgh( seq(0.001, 0.5, length.out=100), alpha = 
parms[1]/abs(scaling), beta = parms[2]/abs(scaling), 
delta=abs(scaling)*parms[3], mu = (scaling)*parms[4]+location, lambda = 
lambda)
all.equal(x1, x2)
# Notice the approximation error in the calculation of the quantile for 
# which there is no closed form solution (and rugarch uses a tolerance
# value of .Machine$double.eps^0.25)
# Load the GeneralizedHyperbolic package of Scott to adjust the
# tolerance:
library(GeneralizedHyperbolic)

x1 = location+scaling*qghyp(seq(0.001, 0.5, length.out=100), mu = 
parms[4], delta = parms[3], alpha =  parms[1], beta = parms[2], lambda = 
lambda, lower.tail = TRUE, method = c("spline", "integrate")[2], 
nInterpol = 501, subdivisions = 500, uniTol = 2e-12)
# equivalent to:
x2 = qghyp(seq(0.001, 0.5, length.out=100), mu = 
(scaling)*parms[4]+location, delta = abs(scaling)*parms[3], alpha = 
parms[1]/abs(scaling), beta = parms[2]/abs(scaling), lambda = lambda, 
lower.tail = TRUE, method = c("spline", "integrate")[2], nInterpol = 
501, subdivisions = 500, uniTol = 2e-12)

all.equal(x1, x2)
#################################################
Regards,

Alexios
#
thanks a lot for your help, but
"Use the 'sigma' and 'fitted' methods"

But these are the fitted values for the volatility and the final
fitted values. But to calculate the VaR I need the 1 step ahead
forecast of the cond. sigmas and the 1 step ahead forecast of the
cond. mean. The cond.mean is not equivalent to the fitted values? And
the fitted values are not one step ahead forecasts?

2013/5/7 alexios ghalanos <alexios at 4dscape.com>:
#
Applying 'sigma' (conditional volatility) and 'fitted' (conditional 
mean) METHODS on a uGARCHforecast object will return the forecast values 
of those quantities with appropriately labelled dates (the T+0 time). 
This too is documented in the help files, and there was also a blog post 
about this ('Whats new in rugarch (ver 1.01-5)').

For VaR, as I already mentioned, you can use the 'quantile' method on
any of the returned S4 class objects.

-Alexios
On 07/05/2013 12:51, Neuman Co wrote:
#
The data I am working with can be found here:
http://uploadeasy.net/upload/2fvhy.rar

I fitted the following model:
alvnomodel<-ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0), include.mean = FALSE),
distribution.model = "norm")

alvnomodelgarch<-ugarchfit(spec=alvnomodel,data=alvlloss)

Now I want to get the one-day ahead forecasts of the cond. volatility
and the cond. mean. I try to applay ugrachforecast:

ugarchforecast(alvnomodelgarch, n.ahead = 1,out.sample=50,n.roll=10)

But I get the error message:
ugarchforecast-->error: n.roll must not be greater than out.sample!

What is wrong?

Also I don't know what values I should take for out.sample and n.roll?
I just want to get 1 day ahead forecasts. What values do I need to
insert?

2013/5/7 alexios ghalanos <alexios at 4dscape.com>:
#
Also http://www.unstarched.net/2013/02/27/whats-new-in-rugarch-ver-1-01-5/
does not work if I try it.
If I do
c(is(sigma((fit))), is(fitted(fit)), is(residuals(fit)))
I do not get xts xts xts but numeric vector and so on?

If I try
plot(xts(fit at model$modeldata$data, fit at model$modeldata$index),
auto.grid = FALSE,
    minor.ticks = FALSE, main = 'S&P500 Conditional Mean')
lines(fitted(fit), col = 2)
grid()

I get the error messages
Fehler in plot(xts(fit at model$modeldata$data, fit at model$modeldata$index),  :
  Fehler bei der Auswertung des Argumentes 'x' bei der Methodenauswahl
f?r Funktion 'plot': Fehler in xts(fit at model$modeldata$data,
fit at model$modeldata$index) :
  order.by requires an appropriate time-based object

and sigma(f) is also not working?
Fehler in UseMethod("sigma") :
  nicht anwendbare Methode f?r 'sigma' auf Objekt der Klasse
"c('uGARCHforecast', 'GARCHforecast', 'rGARCH')" angewendet
I just tried to understand the examples, but they do not work?

2013/5/8 Neuman Co <neumancohu at gmail.com>:
#
I mean, I only try to get the one-step ahead in-sample predictions. So
I do NOT want to leave out any observations in the estimation stage? I
want to use all observations, estimate the GARCH and use the estimated
parameters and data to get one step ahead in sample predictions so I
can use these one-step-ahead-in-sample predictions of my cond.
volatility and cond. mean to calculate the VaR.

2013/5/8 Neuman Co <neumancohu at gmail.com>:
#
Please TRY to send one question, wait for a reply and then ask another 
rather than sending 3 emails in a row.

1. The only reason that you would not get an xts returned object in the 
example below is that you are using an old version of rugarch. Make sure 
you download the latest version (which requires R>=3.0.0) and update 
your packages. It is also good practice to send the output of
sessionInfo() in such cases.

2. I'm not sure what you mean by "one step ahead in sample predictions".
- If you want the 1-ahead forecast use ugarchforecast(alvnomodelgarch, 
n.ahead = 1)
- If you want the in-sample estimated values use 'fitted' and 'sigma'
(in which case the terminology you used in not 'standard').

3. There are enough examples in 'rugarch.tests' folder in the source 
distribution, online blog examples, previous mailing list posts, other 
online resources, for you to be able to do what you need.

Finally, I am more likely to respond to future requests for help if you 
provide minimally reproducible examples without sending zip files of 
your data (this is rarely needed), showed that you made an effort to 
read the documentation, and have signed your emails with your real name.

Regards,
Alexios
On 08/05/2013 11:28, Neuman Co wrote:
#
well but this is exactly the point, that

ugarchforecast(alvnomodelgarch, n.ahead = 1)

just gives me one forecast, but I want to have this from my beginning
of the time series up to the end. So I do not want to exclude these
observations in my fitting. I want to use all observations for my
fitting and then do one step ahead in sample predictions. So e.g. I
have a time series of 1000 observations. I to the fitting with the
1000 observations and then I want to predict the one step ahead
in-sample forecasts of the cond. mean and the cond. volatility?

I do not understand the thing with the fitted and the sigma. Correct
me if I am wrong, but these are the fitted values, these are not
1-step-ahead forecasts?

I try to give a more simpler example to make the understanding easier:
consider a simple linear regression:
The fitted values of y at time point t use the realization of x at
time point t. The one step ahead forecast uses the realization of
timepoint minus 1.

So I do NOT want to have the fitted values of the cond. volatility and
the cond. mean (in my case the cond. mean is zero, because I have
specified no model for it), but the one step ahead forecasts of it. So
R should do the n.ahead=1 not only for the last value, but from the
beginning up to the end?

I mean I only get the forecast of the next day, the 2013-03-04, but I
want to have it from the beginning up to the end?






2013/5/8 alexios ghalanos <alexios at 4dscape.com>:
#
On 05/08/2013 06:06 AM, Neuman Co wrote:
There is no such thing as a one step ahead in-sample  *prediction*.

Please stop and think a moment about how absurd that is before you waste 
more of everyone's time.

At each time t, I may make a prediction for time t+1

ugarchroll will give you a set of rolling one or n step ahead 
*predictions*, as Alexios has also already told you.

If you want the in-sample *estimate* from the fitted model values (it is 
not a prediction), then you want the 'fitted' and 'sigma' values, as 
Alexios has told you at least twice now.

...and I still don't see you signing your name.

Brian
#
That may be the case for your linear regression example, but certainly 
NOT the case in the ARMA-GARCH models. Everything is based on 
information upto time T-1. There is no "contemporaneous" data, as a 
reading of the vignette (where the models are clearly explained and 
formulated) shows. Therefore, and as pointed out before, USE the fitted, 
sigma, and quantile methods.

-Alexios
On 08/05/2013 12:06, Neuman Co wrote:
#
Thanks a lot for your help, ok, I added the signature in my mail and
the r sig list accout.

But to be honest neither I am not really understanding you nor are
your hints helping me. From someone other, I was told to use the
following code to get what I want:

(I add my model here specification again, so you do not have to search
in my posts before)

-----------------------------------------------------------------------------------
library(rugarch)
alvdatemod<-alvdate[-1]
alvgarchdata<-data.frame(alvlloss,row.names=alvdatemod)

alvnomodel<-ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0), include.mean = FALSE),
distribution.model = "norm")

alvnomodelgarch<-ugarchfit(spec=alvnomodel,data=alvgarchdata)
#plot(alvnomodelgarch)
-------------------------------------------------------------------------------------


and here comes the relevant part:


spec = getspec(alvnomodelgarch);
setfixed(spec) <- as.list(coef(alvnomodelgarch));
forecast = ugarchforecast(spec, n.ahead = 1, n.roll = 2579, data =
alvgarchdata[1:2580, ,drop=FALSE], out.sample = 2579);
sigma(forecast);
fitted(forecast)


I get the (german) error message after the sigma command:
Fehler in UseMethod("sigma") :
  nicht anwendbare Methode f?r 'sigma' auf Objekt der Klasse
"c('uGARCHforecast', 'GARCHforecast', 'rGARCH')" angewendet

And the forecast specification is also not what I want, if you just
look at forecast, you will see, that again I just got one value, this
is NOT what I want!

Also, I am not understanding why I have to use this n.roll number and
this out.sample number. Also, why can't I just use my dataset, but
instead use [1:2580, ,drop=FALSE] ? But these are just some smaller
unimportant questions.



Later on, I want to calculate the VaR and compare it to the quantile
method, therefore I would continue with the following idea, first,
calculate VaR:

VaR=fitted(forecast)+sigma(forecast)*qnorm(0.99)

and then compare it to the quantile method:

quantile(forecast,probs=0.99)

but it seemed, that I also did a syntax error here.


2013/5/8 alexios ghalanos <alexios at 4dscape.com>:
--
Neumann, Conrad