[R-SIG-Finance] [R] Fitting distributions to financial data using volatility model to estimate VaR

R. Michael Weylandt michael.weylandt at gmail.com
Wed Apr 10 01:28:04 CEST 2013


Moving to R-SIG-Finance which seems to be the correct mailing list.

On Sun, Apr 7, 2013 at 8:41 AM, Stat Tistician
<statisticiangermany at gmail.com> wrote:
> Ok,
> I try it again with plain text, with a simple R code example and just
> sending it to the r list and you move it to sig finance if it is
> necessary.
>
> I try to be as detailed as possible.
>
> I want to fit a distribution to my financial data using a volatility
> model to estimate the VaR. So in case of a normal distribution, this
> would be very easy, I assume the returns to follow a normal
> distribution and calculate a volatility forecast for each day, so I
> have sigma_1,sigma_2,...,sigma_n,. I can calculate the VaR via (mu
> constant, z_alpha quantile of standard normal):
> VaR_(alpha,t)=mu+sigma_t * z_alpha. This is in case, I have losses, so
> I look at the right tail. So for each day I have a normal density with
> a constant mu but a different sigma corrensponding to the volatility
> model. Let's assume a very simple volatility model, e.g. (empirical)
> standard deviation of the last 10 days and the mu is set to zero. The
> R code could look like (data):
>
> volatility<-0
> quantile<-0
> for(i in 11:length(dat)){
> volatility[i]<-sd(dat[(i-10):(i-1)])
> }
>

Let's clean this up:

library(xts)
# Load data into an xts object called "dat" here.

library(TTR)
vola <- runSD(dat, 10) # volatility is a ttr function name, so not using that

> for(i in 1:length(dat)){
> quantile[i]<-qnorm(0.975,mean=0,sd=volatility[i])
> }

# Similarly, quantile is a function name and not a good idea to override

quan <- qnorm(0.975, mean = 0, sd = vola)

> # the first quantile value is the VaR for the 11th date
>
> #plot the volatility
> plot(c(1:length(volatility)),volatility,type="l")

Can just do
plot(vola) # if vola is an xts object. Also, the c() is superfluous
(and arguably dangerous) there

>
> #add VaR
> lines(quantile,type="l",col="red")

library(PerformanceAnalytics)
chart.BarVaR(dat) # is probably easier.


>
> Now, I want to change the volatility model to a more advanced model
> (EWMA, ARCH, GARCH) and the distribution to a more sophisticated
> distribution (student, generalized hyperbolic distribution). My main
> question is now, how can I combine the volatility model and the
> distribution, since in case e.g. of a Student's-t distribution with
> parameters mu (location), v (df), beta (scale) I cannot just plug the
> sigma in, because the distribution has no sigma?

Be careful here -- it certainly has a standard deviation, just not a
parameter called sigma. Keeping those two ideas distinct is important
here. They only coincide for the normal distribution. (Among the major
ones)

You're also using something other than the classical t-distribution
(as defined by the all-hallowed Wikipedia) if it's a three parameter
distribution.

>
> One solution I already know is, that I take the variance formula of
> the corresponding distribution - in case of a Student's-t distribution
> this would be sigma=beta v/(v-2). I have an estimate for sigma. So for
> each day I do the ML estimation with a modified log-likelihood where I
> insert for the scale parameter: beta=sigma * (v-2)/v and do the
> estimation.
>
> First of all, is this correct?

No -- I believe that the t-distribution has sigma^2 = v / (v-2).

But even with that correction, it's not what I would do.

I would first fit beta, nu, and mu by ML and then estimate sigma from there.

Without checking, I'm not positive that sample standard deviation
gives a particularly good estimator for the df of a t-distribution. It
wouldn't surprise me if it did though.

In that same vein, I'm not sure this process gives an estimate that's
much superior to the rolling empirical standard deviation. Though it
might be useful for forward looking predictions...

Bigger question -- have you looked at TTR::volatility or
PerformanceAnalytics::VaR. There's a lot of useful stuff in there.

I'll try to read through the rest of this later -- about to have
dinner with the family,
Michael

>
> I looked at several papers, but I did not understand, how they did
> this? No matter what volatility model they use, I cannot understand
> the connection of distribution and volatility model. For example,
> consider this paper:
> http://www.math.chalmers.se/~palbin/mattiasviktor.pdf
>
> On page 50 they are showing the hyperbolical distribution with
> different volatility models, how did they do this?
>
> Also, I do not understand table 6.2 on page 49: If they have estimated
> several distributions over the time, they have lots of estimates, but
> they just show one distribution? I mean, where does it come from? The
> 3d picture clearly differnt distributions over time, so they have
> estimated the distribution after 5 days (page 48), but in the table is
> just one specific distribution with specific parameters? And they give
> the volatility models in the rows?
>
> A second famous paper is the Technial Document by JPMorgan:
> RiskMetrics Technical Document - Fourth Edition 1996, December:
>
> http://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDUQFjAA&url=http%3A%2F%2Fzhiqiang.org%2Fblog%2Fdownload%2FRiskMetrics%2520-%2520Technical%2520Document.pdf&ei=RSJhUd7YJIbktQaQ-YCAAw&usg=AFQjCNGpCXUdLSVHQtYJMl7MccLGQtdkDw&sig2=HBxWDrRTMN7rVqWu-Yp1zQ&bvm=bv.44770516,d.Yms
>
>
> Especially page 238 is interesting: "According to this model, returns
> are generated as follows"
>
> r_t=sigma_t xi_t
>
> sigma^2_t is calculated by EWMA
>
>
> xi is distributed according to the generalized error distribution. So
> they do not assume the returns to follow a certain distribution, but
> they assume the returns condition on the volatility to follow a
> certain distribution, right?
>
> Now my question is, how can one calculate the VaR in this case? On
> page 242 they give a short summary, describing the steps. The third
> step in the most intersting: "Third, we use [...] volatility estimated
> and the [...] probability distributions ([...] generalized error
> distribution) evaluated at the parameter estimates to construct VaR
> forecasts at the 1st and the 99th percentile."
>
> My question is now, how do they do it?
>
> They describe their fitting steps in the steps before, but I am not
> getting the following point:
>
> Do they fit the distribution to the original return series, calculate
> the volatility (σt) and then just calculate the VaR with
> VaR_t=sigma_t*q_alpha where q_alpha is the quantile of the fitted
> distribution
>
> or
>
> do they fit the distribution to the standardized returns
> (xi_t=r_t/sigma_t), calculate the volatility and then just calucate
> the VaR with VaR_t=sigma_t*q_alpha where q_alpha is now the quantile
> of the fitted distribution which was fitted using the standardized
> residuals?
>
> Another question is: Did they set the mean of the return to zero?
>
> My main point is, how to fit a sophisticated distribution to financial
> data using a volatility forecast for each day, which was generated by
> EWMA, ARCH or GARCH and how to calculate the VaR for each day over a
> time horizon and how to implement this. The paper is doing this, but I
> am simply not getting it. I worked on this for days now and I am
> really stuck here.
>
> And one other question: If I am doing the way with the modified
> log-likelihood: This is really challenging in case of a hyperbolic
> distribution, since the variance is not calculated easily anymore? See
> wikipedia.
>
> Thanks a lot for your help.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-SIG-Finance mailing list