Quantative Risk Management In R

profileMurthy
Chapter10.QuantitativeRiskManagementinR.pdf

Page 1 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

Chapter 10. Quantitative Risk Management in R

In this chapter, I explore how we can describe the risk of a single security or a portfolio (a set of assets).

Especially, I introduce the concept of value at risk (VaR) and expected shortfall (ES) here.

1. What is value at risk (VaR)?

Value at risk is one of the most widely used risk measure in finance. VaR was popularized by J.P. Morgan

in the 1990s. The executive at J.P. Morgan wanted their risk managers to generate one statistic that

summarized the risk of the firm’s entire portfolio at the end of each day. What they came up with was VaR,

which is now widely used by corporate treasurers and fund managers as well as by financial institutions.

VaR is a one-tailed confidence interval. If the 5-day 95% VaR of a portfolio is $1,000, then we expect the

portfolio will lose $1,000 or less in 95% of the scenarios and lose more than $1,000 in 5% of the scenarios

in 5 days. For example, we are interested in making a statement of the following form when using the VaR:

“We are 95 percent certain that we will not lose more than $1,000 in 5 days.”

It is a function of two parameters: the time horizon (e.g. 5-day in the example above) and the confidence

level (e.g. 95% in the example above). We can define VaR for any confidence level, but 95% has become an

extremely popular choice at many financial firms. The time horizon also needs to be specified for VaR. On

trading desks with liquid portfolios, it is common to measure the one-day 95% VaR.

The following figure provides a graphical representation of VaR at the 95% confidence level. The figure

shows the probability density function for the returns of a portfolio. Because VaR is measured at the 95%

confidence level, 5% of the distribution is to the left of the VaR level, and 95% is to the right.

Page 2 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

We now formally define VaR. Let L be a random variable, which represents the loss to our portfolio. L is

simply the opposite of the return to our portfolio. For example, if the return of our portfolio is -$1,000, the

loss, L, is +$1,000. For given confidence level α, VaR is defined as

P(L ≥ VaR𝛼) = 1 − 𝛼

We can also define VaR directly in terms of returns. If we multiply both sides of the inequality above by -1,

and replace -L with R, we come up with

P(R ≤ −VaR𝛼) = 1 − 𝛼

While both equations above are equivalent, defining VaR in terms of losses is more common. It has the

advantage that, for most portfolios for reasonable confidence levels, VaR will almost always be a positive

number. In practice, rather than saying that your VaR is $1,000, it is often best to resolve any ambiguity by

stating that your VaR is a loss of $1,000.

2. Why needs VaR?

There are many reasons why VaR has become so popular in risk management. One of the primary appeals

of VaR is its simplicity. The concept of VaR is intuitive, even to those not versed in statistics. Because it

boils risk down to a single number, VaR also provides us with a convenient way to track the risk of a

portfolio over time.

Another appealing feature of VaR is that is focuses on losses. This may seem like an obvious criterion for a

risk measure, but variance and standard deviation treat positive and negative deviations from the mean

equally. For many risk managers, VaR also seems to strike the right balance by focusing on losses that are

significant, but not too extreme.

VaR also allows us to aggregate risk across a portfolio with many different types of securities (e.g. stocks,

bonds, futures, options, etc.). Prior to VaR, risk managers were often forced to evaluate different segments

of a portfolio separately. For example, for the bonds in a portfolio they may have looked at the interest rate

sensitivities, and for the equities they may have looked at how much exposure there was to different

industries.

Finally, VaR is robust to outliers. As is true of the median or any quantile measure, a single large event in

our data set will usually not change our estimate of VaR. This advantage of VaR is a direct consequence of

Page 3 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

one of its deepest flaws, that it ignores the tail of the distribution. As we will see in the next section,

expected shortfall (ES), a closely related measure, has exactly the opposite problem: it incorporates the tail

of the distribution, but it is not robust to outliers.

3. Examples of the calculation of VaR

This section provides simple examples to illustrate the calculation of VaR.

Example 1.

Suppose that the gain from a portfolio during six months is normally distributed with a mean of $1,000,000

and a standard deviation of $5,000,000. From the properties of the normal distribution, the one-percentile

point of this distribution is 1,000,000 − 2.326 × 5,000,000 or −$10,630,000. The VaR for the portfolio

with a time horizon of six months and confidence level of 99% is therefore $10,630,000.

Example 2.

Suppose that for a one-year project all outcomes between a loss of $1,000,000 and a gain of $1,000,000 are

considered equally likely. In this case, the loss from the project has a uniform distribution extending from

−$1,000,000 to +$1,000,000. There is a 1% chance that there will be a loss greater than $990,000. The

VaR with a one-year time horizon and a 99% confidence level is therefore $990,000.

Example 3.

A one-year project has a 98% chance of leading to a gain of $1,000,000, a 1.5% chance of leading to a loss

of $2,000,000, and 0.5% chance of leading to a loss of $5,000,000. The cumulative loss distribution is

shown below. The point on this cumulative distribution that corresponds to a cumulative probability of 99%

is $2,000,000.

Example 4.

Page 4 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

Consider again the situation in Example 3. Suppose that we are interested in calculation a VaR using a

confidence level of 99.5%. In this case, the figure above shows that all losses between $2,000,000 and

$5,000,000 have a probability of 99.5% of not being exceeded. Equivalently, there is a probability of 0.5%

of any specified loss level between $2,000,000 and $5,000,000 being exceeded. VaR is therefore not

uniquely defined. One reasonable convention in this type of situation is to set VaR equal to the midpoint of

the range of possible VaR values. This means that, in this case, VaR would equal $3,500,000.

Example 5.

Suppose that the probability density function (pdf) of Square Inc.’s daily profits could be described by the

following piecewise function:

p = 1

20 for −10 ≤ π ≤ 10

The pdf is shown below.

What is the one-day 95% VaR for Square Inc.?

To find the 95% VaR, we need to find a, such that

−(−10 − a) × 1

20 = 0.05

Page 5 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

Rearranging terms,

a = −9

The one-day 95% VaR for Square Inc. is a loss of 9.

4. Expected Shortfall

One criticism of VaR is that it does not tell us anything about the tail of the distribution. Two portfolios

could have the exact same 95% VaR but very different distributions beyond the 95% confidence level.

Beyond VaR, we may also want to know how big the loss will be when we have an exceedance event. Using

the concept of conditional probability, we can define the expected value of a loss, given an exceedance, as

E[𝐿 | 𝐿 ≥ 𝑉𝑎𝑅𝛼] = 𝐸𝑆

We refer to this conditional expected loss, ES, as the expected shortfall. The use of the term expected

shortfall is not universal. Many practitioners refer to this statistic as conditional VaR (cVaR). If the expected

profit of a und can be described by a probability density function given by 𝑓(𝑥), and VaR is the VaR at the

α confidence level, we can find the expected shortfall as

𝐸𝑆 = − 1

1 − 𝛼 ∫ 𝑥𝑓(𝑥)𝑑𝑥

𝑉𝑎𝑅

−∞

As with VaR, risk managers tend to talk about expected shortfall in terms of losses. Just as VaR tends to be

positive for reasonable confidence levels for most portfolios, expected shortfall, as we have defined it

above, will also tend to be positive.

As risk managers, we want to know as much about the tail of the distribution as possible. Expected shortfall

tells us something about the tail. VaR does not tell us anything about the shape of the tail, but it is more

robust to outliers. Thus, it is important to understand these tradeoffs.

Example.

In a previous example, the probability density function (pdf) of Square Inc.’s daily profits could be

described by the following piecewise function:

p = 1

20 for −10 ≤ π ≤ 10

Page 6 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

The pdf is shown below.

We calculated Square Inc.’s one-day 95% VaR as a loss of 9. For the same confidence level and time

horizon, what is the expected shortfall?

𝐸𝑆 = − 1

1 − 𝛼 ∫ 𝑥𝑓(𝑥)𝑑𝑥

𝑉𝑎𝑅

−∞

= − 1

0.05 ∫ 𝜋

1

20 𝑑𝜋

−9

−10

= −20 [ 1

40 𝜋2]

−10

−9

= − 1

2 (81 − 100)

= 9.5

Thus, the expected shortfall is a loss of 9.5. Intuitively, this should seem reasonable. The expected shortfall

must be greater than the VaR, 9, but less than the maximum loss of 10. Because extreme events are equally

likely for this pdf, it also makes sense that the expected shortfall is the midpoint between maximum loss and

the VaR.

5. Calculation of VaR and ES with R

In this section, we will see how to calculate VaR and ES using R. To do this, we will use daily log returns of

Apple Inc. from 2010-01-02 to 2018-12-31 from Yahoo! Finance.

Page 7 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

# Create daily log returns of Apple Inc.

library(quantmod)

getSymbols("AAPL", src = "yahoo", from='2010-01-02', to='2018-12-31')

AAPL.Adj <- AAPL$AAPL.Adjusted

logreturns <- diff(log(AAPL.Adj))

# Delete the first row NA

logreturns <- logreturns[-1,]

Now, you are ready to estimate VaR and ES for daily log returns of Apple Inc. You will do this by two

methods. First, you will apply a simple non-parametric method using a sample quantile to estimate VaR and

the average of values exceeding the sample quantile to estimate ES.

Then, you will compare these estimates with the values obtained when you assume that the daily log returns

of Apple Inc. have a normal distribution.

We use quantile(...) function to estimate the 95th sample percentile of the distribution of daily log returns.

# Estimate the 95th sample percentile of the distribution of logreturns

VaR95 <- quantile(logreturns, 0.05)

VaR95

5%

-0.02540416

We estimate the 95% ES by computing the mean of the daily log returns that are at least as large as the VaR

estimate.

# Estimate the 95% ES

ES95 <- mean(logreturns[logreturns <= quantile(logreturns, 0.05)])

ES95

[1] -0.03695661

# Plot VaR and ES with sample distribution

plot(density(logreturns), col = "blue")

text(0.025, 27, "Sample Distribution", col = 'blue')

abline(v = VaR95, col = 'blue', lty = 2)

text(-0.01, 2, "95% VaR", col = 'blue')

abline(v = ES95, col = 'blue', lty = 1)

text(-0.045, 7, "95% ES", col = 'blue')

Page 8 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

Thus, according to the sample distribution of the daily log returns of Apple Inc., we have VaR of

0.02540416 and ES of 0.03695661. Note that these are the results from the sample distribution.

Now, we assume that the daily log returns follow normal distribution. Then, we estimate VaR and ES. We

estimate the mean and standard deviation of logreturns first.

# Estimate the mean and standard deviation of logreturns

mu <- mean(logreturns)

sigma <- sd(logreturns)

We compute the 95% quantile of a normal distribution.

# Compute the 95% quantile of a normal distribution

VaR95_normal <- qnorm(0.05, mean = mu, sd = sigma)

VaR95_normal

[1] -0.025945

The function ESnorm(...) from QRM package calculates the ES for a normal distribution from the

probability p, location parameter mu, and scale parameter sd.

# Compute the 95% ES of a normal distribution

install.packages('QRM')

library(QRM)

ES95_normal <- -ESnorm(0.95, mu = mu, sd = sigma)

[1] -0.03456113

Page 9 of 9

This material is only for the use of students enrolled in FIN 740 for purposes associated with the course and may not be

retained or further disseminated. All information in this material is proprietary to Dr. Sung Ik Kim. Scanning, copying,

posting to a website or reproducing and sharing in any form is strictly prohibited.

# Plot VaR and ES with normal distribution

x <- seq(-0.1, 0.1, by = .00001)

y <- dnorm(x, mean = mu, sd = sigma)

plot(x, y, col = "red", type = 'l')

text(0.03, 20, "Normal Distribution", col = 'red')

abline(v = VaR95_normal, col = "red", lty = 2)

text(-0.015, 2, "95% VaR", col = 'red')

abline(v = ES95_normal, col = "red", lty = 1)

text(-0.045, 5, "95% ES", col = 'red')