Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
QRM 8 - Laptop session
Introduction
The objective of the session is
(a). To understand how to use the ghyp package to undertake parametric estimation
for normally distributed returns, and to obtain risk measures, in both the case
of a single asset portfolio, and a multiple asset portfolio.
The ghyp package
This package has a number of very useful features which allow the user to go from
fitting parametric model to data (single or multiple assets), to take those fitted mod-
els and obtain the PDF of the loss distribution, and thence to calculate some risk
measures. Furthermore, it allows us to perform these tasks for a range of quite flexi-
ble probability distribution, which is very important as it means we are not restricted
to using the Normal distribution.
To load the package, first make sure you have installed it (remember you need do
this only once), then type
library(ghyp)
and if all goes well it will load. There may be some benign messages; that is OK.
Before turning to estimation it would be helpful to gain some basic insights about
how to work with this package. The fundamental idea is that we work with ghyp
objects. These can be created either directly, as we see how to do in a moment, or
as a result of fitting, which we look at later.
As a simple example, we create a ghyp object which represents the PDF of the exam-
ple we looked at in lecture 5 of a Single Asset. The returns were assumed Normal,
with mean 0.01 and standard deviation 0.02.
To create this object and store it, we can do
g <- gauss(mu=0.01, sigma=0.02)
g
Typing g on its own shows that it has the Gaussian (ie Normal) distribution, and what
the parameters are, in terms of the mean and standard deviation.
Once we have created an object, we can get information about it using functions
which the package supplies. Some particularly useful ones are exhibited here.
Try these out and then refer to the table below for more details.
mean(g)
vcov(g)
plot(g,type='l')
dghyp(0.03,g)
pghyp(0.03,g)
qghyp(0.95,g)
rghyp(10,g)
In the following, the object’s name is represented by the term obj.
Function Description
mean(obj) Returns the expected value
vcov(obj) Returns the Variance in the case of a Univariate
object, and calculates the Variance-Covariance
matrix in the case of a multivariate object
dghyp(x,obj) Returns the PDF evaluated at the points con-
tained in the vector x
pghyp(x,obj) Returns the CDF evaluated at the points con-
tained in the vector x
qghyp(p,obj) Returns the quantiles evaluated at the probabil-
ities contained in the vector p
rghyp(n,obj) Returns a sample of n values drawn from the
density of obj (useful for Simulation)
Perhaps the most useful additional function is the transform function. Recall how
we wanted to find the loss distribution, given that the returns were distributed like
g, and given the current portfolio valuation was £1000. To transform returns into
losses, the rule was to multiply returns by −Pt, so in this case we should multiply by
-1000. The transform function performs this sort of task for us. We give the object
to be transformed, and the multiplier which is to be used in the transformation (here
-1000). Hence to find the loss distribution
pv <- 1000
g.l <- transform(g,multiplier = -pv)
g.l
plot(g.l, type = 'l')
which tells us the loss is Normal with mean -10 and standard deviation 20. We knew
this already, of course, but this saves a lot of time. It gets even better, as ghyp has
the function ES.ghyp to calculate the Expected shortfall from the parametric loss
distribution, and of course we can use qghyp to get the quantiles, and hence VaR.
ESghyp(0.95,g.l,distr="loss")
qghyp(0.95,g.l)
Note we give ESghyp the option ‘distr’ to tell is we are using the loss distribution. Of
course, these calculations are done without historical data, and simply rely on the
properties of the parametric probability distributions.
This makes the VarCov method very easy to apply. We don’t need to recall the rules
for relating the returns to the losses, and we don’t need to recall the formulae for
working out ES and VaR.
It is even more convenient when we consider Bivariate returns. We look at the second
example from the lecture using ghyp. The steps are similar to the above. With
multivariate densities it is usual to write the means as a vector and the variances
and covariances as a matrix. Referring to the example from Lecture 8, we had two
assets, A and B,
μA = 0.09, μB = 0.07
V(RA) = 0.004, V(RB) = 0.003
Cov(RA, RB) = 0.002
This information is succinctly represented by a vector of means (pronounced “mew”)
μ =
0.09
0.07
and by the Variance-Covariance Matrix (pronounced sigma)
=
0.004 0.002
0.002 0.003
In this matrix, top left is the variance of A, bottom right is the variance of B, and the
off-diagonal elements record the Covariance between A and B.
To create the object we first set up the mean vector and the Variance-Covariance
matrix
m <- c(0.09,0.07)
sig <- cbind(c(0.004,0.002),c(0.002,0.003))
m
sig
and next we can define the object:
gm <- gauss(mu=m,sigma=sig)
gm
Of the functions we used above, possibly the most useful in the Bivariate case is the
rghyp function, which now gives us a sample of pairs from the Bivariate normal:
rghyp(10,gm)
plot(rghyp(10000,gm),pch = '.')
For risk measurement, we need to convert the asset returns to losses. Again, we will
use the transform function. We need to identify what each asset gets multiplied by
to convert the returns to losses. In the bivariate case we saw in Lecture 5 that this is
achieved by the following transformation
Loss = −Pt × (A × RA + B × RB)
ie return A gets multiplied by the negative of the portfolio value times the weight of
A, and B gets multiplied by the negative of the portfolio value times the weight of B.
So the multiplier of A is −PtA and the multiplier of B is −PtB. We need to give the
transform function a vector containing each multiplier, in order first A then B:
pv <- 1000
wa <- 0.25
wb <- 0.75
mult <- -pv*c(wa,wb)
mult
gm.l <- transform(gm,multiplier = mult)
gm.l
As we can see, the loss distribution is univariate (as of course it must be). Given the
loss distribution, we find our risk measures as before:
ESghyp(0.95,gm.l,distr="loss")
qghyp(0.95,gm.l)
The above is mainly for illustration, since it supposes we have already obtained the
parameters of the returns’ distribution. However, you may appreciate how useful
this approach is when used in conjunction with parameter estimation, as we now
investigate.
Estimation
We now explore how to fit the parameters of a model using returns data, for the case
the reutrns are assumed Normal. We see there is very little extra work involved.
Essentially all we have to do is get estimated for the mean and standard deviation of
the returns from out data. ghyp comes with some data on Swiss stock returns, which
we now use for illustration. They are stored in the data set called smi.stocks, which
we first have to load into the workspace with the data command, see below. Then I
pick out columns 4 and 5 (Nestle and Swisscom) and store in d. Then I pick out the
first column of the result and store in d1 for Univariate estimation. are already
data(smi.stocks)
d <- smi.stocks[,c(4,5)]
d1 <- d[,1]
First we estimate the mean and standard deviation:
md1 <- mean(d1)
md1
vard1 <- var(d1)
vard1
Now we can use these to construct the gauss object:
g <- gauss(mu=md1, sigma=sqrt(vard1))
g
Then we can calculate VaR and ES as before:
pv <- 1000
g.l <- transform(g,multiplier = -pv)
g.l
plot(g.l, type = 'l')
ESghyp(0.95,g.l,distr="loss")
# qghyp(0.95,g.l)
For multivariate estimation using both stocks, the idea is similar, we estimate the
means and the covariance using the sample data, and use these values to greate
the ghyp object.
md <- colMeans(d)
md
covd <- cov(d)
covd
g <- gauss(mu = md, sigma = covd )
g
Now transform to loss and calculate risk measures:
pv <- 1000
wa <- 0.25
wb <- 0.75
mult <- -pv*c(wa,wb)
mult
g.l <- transform(g,multiplier = mult)
g.l
ESghyp(0.95,g.l,distr="loss")
qghyp(0.95,g.l)
Thanks for taking this tutorial.