[R-SIG-Finance] Rugarch for EWMA/VaR

alexios ghalanos alexios at 4dscape.com
Fri May 4 00:28:30 CEST 2012


Thomas,

You need to read the documentation more carefully (you forgot to declare 
the n.roll option).

Example:
library(rugarch)
data(sp500ret)

spec= ugarchspec (variance.model = list(model = "iGARCH", garchOrder =
c(1,1)), mean.model=list(armaOrder=c(0,0),include.mean=TRUE),
distribution.model = "norm",  fixed.pars = list(mu = 
mean(sp500ret[1:100,1]),omega=0, alpha1=0.06))

# forecast the model starting from t=101 to 1=1500
# forecast = 1-ahead x 1399 rolls = 1400 forecasts (includes the first # 
forecast as the 'zero' roll)

forecast = ugarchforecast(spec, sp500ret[1:1500,,drop=FALSE], n.roll = 
1399, n.ahead = 1, out.sample=1400)

# conditional mean (just a constant)
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))

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

dates = as.Date(rownames(sp500ret)[101:1500])
plot(dates, sp500ret[101:1500,1])
lines(dates, VaR1, col = "blue")
lines(dates, VaR5, col = "brown")

# VaR Exceedances Test
VaRTest(alpha = 0.01, actual = sp500ret[101:1500,1], VaR = VaR1)
VaRTest(alpha = 0.05, actual = sp500ret[101:1500,1], VaR = VaR5)

# VaR Duration Test
VaRDurTest(alpha = 0.01, actual = sp500ret[101:1500,1], VaR = VaR1)
VaRDurTest(alpha = 0.05, actual = sp500ret[101:1500,1], VaR = VaR5)



-Alexios


On 03/05/2012 23:05, ThomasF wrote:
>   Hello Alexios,
>
> thank you for your quick reply.
>
> I'm going to download the newest version from R-Forge.
>
> Concerning 4) I still have problems.
> I tried something like:
> forc2=ugarchforecast(spec=spec2, data=sp500returns, n.ahead=1,
> out.sample=500, calculate.VaR=TRUE, VaR.alpha=c(0.01, 0.05))
> and
> forc2=ugarchforecast(spec=spec2, data=sp500returns, n.ahead=1,
> out.sample=500)
> what didn't work for me.
>
> I would like to be able to generate VaR-Backtesting Results and
> VaR-exceedence-graphs like I did with the fitted model.
> Could you please help me out, maybe with some lines of code? I already
> checked the example codes I could find on how to combine forecasting with
> fixed parameters and getting the VaR-Output and still didn't manage to do
> it.
>
> Thomas
>
>
>
>
>
> alexios wrote
>>
>> Thomas,
>>
>> 1. The problem with the iGARCH model (related to a bad default setting)
>> was fixed some time ago in the r-forge version (will be available on
>> CRAN when I next update it there...since it is frowned upon to update a
>> CRAN package more than 12 times in a year, you should look to r-forge
>> for the 'newest' version at any time).
>> You have 2 options:
>> 1. Download the latest version from r-forge OR,
>> 2. In ugarchfit set the option "fit.control=list(stationarity=FALSE)"
>>
>> 2. That's definitely a problem in the summary method. Will investigate
>> when I get a moment.
>>
>> 3. The error with the ugarchfilter C function, related to the newest
>> version of R, was also fixed in the r-forge release.
>>
>> 4. You can't roll when all the parameters are fixed. It does not even
>> make sense! Roll implies that you are fitting, forecasting, re-fitting.
>> Since you have all parameters fixed then just use the filter method on
>> the data you want to forecast 1-ahead, else use the forecast method with
>> a spec object and out of sample specified.
>>
>> -Alexios
>>
>> On 03/05/2012 21:23, ThomasF wrote:
>>> Dear R-Users, dear Alexios,
>>>
>>> I am trying to use rugarch to forecast volatiliy and do VaR-backtests
>>> using
>>> the Exponentially weighted moving average.
>>> To achieve this I used the iGarch, set omega to 0 and either estimate or
>>> fix
>>> alpha1 and beta1. Example code is provided at the end of my post.
>>>
>>> Some issues came up which I'd be glad to solve with your help:
>>>
>>> 1) Whenever i try to fit alpha1 and beta1, I always get 0.5 as result. I
>>> tried this for different underlyings and time horizons. Any idea what the
>>> reason could be?
>>>
>>> 2) I also get an error (I translated the error message to english) after
>>> the
>>> ARCH LM Test output:
>>>
>>> ARCH LM Tests
>>> ------------------------------------
>>>                Statistic DoF P-Value
>>> ARCH Lag[2]      4.653   2 0.09765
>>> ARCH Lag[5]      7.991   5 0.15672
>>> ARCH Lag[10]    12.082  10 0.27959
>>> Error in names(ans) = c("10%", "5%", "1%") :
>>>     Attribut 'names' [3] must have same length as the vector [0]
>>>
>>> Is there something wrong with my code or the way i put the data into the
>>> model? The other part of the outputs seems legit.
>>>
>>> 3) when I tried to do the same calculations with fixed alpha i get the
>>> following error when i try to filter the model:
>>>
>>> 1: In doTryCatch(return(expr), name, parentenv, handler) :
>>>     passing an object of type 'NULL' to .C (arg 8) is deprecated
>>> 2: In doTryCatch(return(expr), name, parentenv, handler) :
>>>     passing an object of type 'NULL' to .C (arg 9) is deprecated
>>>
>>> The ugarchfilter output seems reasonable again.
>>>
>>> 4)  I am not able to generate a rolling forecast and VaR Output when i
>>> fixed
>>> alpha1. Could you please give me a hint on how to do this with filtered
>>> data?
>>>
>>> 5) Does any of those issues arise of the use of the data as "zoo" object?
>>>
>>>
>>> Thanks in advance for your effort to help me
>>>
>>> Sincerely
>>>
>>> Thomas
>>>
>>>
>>> -----------------Code----------
>>>
>>>
>>> ################
>>> ###Download Data&   Compute Returns
>>> ################
>>>
>>> library ("tseries")
>>>
>>> #adjusted close price of sp500
>>> sp500price=get.hist.quote(instrument="^gspc", start="2006-01-01",
>>> end="2011-12-31", quote="AdjClose")
>>>
>>> # returns
>>> sp500returns=diff(log(sp500price))
>>>
>>>
>>> library("rugarch")
>>>
>>>
>>>
>>> ##############
>>> ### Use iGarch for EWMA - fitted alpha and beta
>>> ##############
>>>
>>>
>>> ### Model spec
>>> spec1= ugarchspec (variance.model = list(model = "iGARCH", garchOrder =
>>> c(1,
>>> 1)), mean.model=list(armaOrder=c(0,0),include.mean=FALSE),
>>> distribution.model = "norm", fixed.pars = list(omega=0))
>>>
>>> ###Fit Model
>>> fit1= ugarchfit(spec=spec1, data=sp500returns, out.sample=0,
>>> solver="solnp",
>>> solver.control=list(trace=0))
>>> show(fit1)
>>>
>>> #### rolling forecast und VaR
>>> roll1=ugarchroll(spec=spec1, data=sp500returns, n.ahead=1,
>>> forecast.length=500, refit.every=25, refit.window="recursive",
>>> solver="solnp", solver.control=list(tol=1e-05, delta=1e-06, trace=0),
>>> calculate.VaR=TRUE, VaR.alpha=c(0.01, 0.05))
>>>
>>> report(roll1, type="VaR", n.ahead=1, VaR.alpha=0.01, conf.level=0.95)
>>> report(roll1, type="fpm")
>>> plot(roll1, which="all")
>>>
>>>
>>>
>>>
>>> ##############
>>> ### Use iGarch for EWMA - fixed alpha and beta with lambda=0.94
>>> ##############
>>>
>>>
>>> ### Model spec
>>> spec2= ugarchspec (variance.model = list(model = "iGARCH", garchOrder =
>>> c(1,
>>> 1)), mean.model=list(armaOrder=c(0,0),include.mean=FALSE),
>>> distribution.model = "norm",  fixed.pars = list(omega=0, alpha1=0.06))
>>>
>>> ###Fit Model???
>>>
>>> fit2= ugarchfit(spec=spec2, data=sp500returns, out.sample=0,
>>> solver="solnp",
>>> solver.control=list(trace=0))
>>> show(fit2)
>>>
>>> ### filter model
>>> filt2= ugarchfilter(spec=spec2, data=sp500returns)
>>> show(filt2)
>>>
>>> #### rolling forecast und VaR
>>>
>>> roll2=ugarchroll(spec=spec2, data=sp500returns, n.ahead=1,
>>> forecast.length=500,   calculate.VaR=TRUE, VaR.alpha=c(0.01, 0.05))
>>>
>>> report(roll2, type="VaR", n.ahead=1, VaR.alpha=0.01, conf.level=0.95)
>>>
>>> report(roll2, type="fpm")
>>> plot(roll2, which="all")
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Rugarch-for-EWMA-VaR-tp4607011.html
>>> Sent from the Rmetrics mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> R-SIG-Finance@ 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.
>>>
>>
>> _______________________________________________
>> R-SIG-Finance@ 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.
>>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Rugarch-for-EWMA-VaR-tp4607011p4607222.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> 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