[R-SIG-Finance] rugarch VaR calculation "manually"

alexios ghalanos alexios at 4dscape.com
Wed May 8 12:55:14 CEST 2013


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:
> 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>:
>> 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>:
>>> 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>:
>>>> 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:
>>>>>
>>>>> 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>:
>>>>>>
>>>>>> Hello,
>>>>>>
>>>>>>
>>>>>> On 07/05/2013 12:15, Neuman Co wrote:
>>>>>>>
>>>>>>>
>>>>>>> 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?
>>>>>>
>>>>>>
>>>>>> Use the 'quantile' method i.e. 'quantile(model2, 0.01)'...also applies to
>>>>>> uGARCHforecast, uGARCHsim etc It IS documented.
>>>>>>
>>>>>>>
>>>>>>> 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))
>>>>>>>
>>>>>> NO. 'as.data.frame' has long been deprecated. Use the 'sigma' and
>>>>>> 'fitted'
>>>>>> methods (and make sure you upgrade to latest version of rugarch).
>>>>>>
>>>>>>
>>>>>>> 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?
>>>>>>
>>>>>>
>>>>>> 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.
>>>>>>
>>>>>>>
>>>>>>> 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?
>>>>>>>
>>>>>> 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)
>>>>>> #################################################
>>>>>>
>>>>>>
>>>>>>
>>>>>>> Thanks a lot for your help!
>>>>>>>
>>>>>>
>>>>>> Regards,
>>>>>>
>>>>>> Alexios
>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> R-SIG-Finance at r-project.org 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.
>>>>>>>
>>>>>>
>>>>>
>>>>
>



More information about the R-SIG-Finance mailing list