# IBKR Quant Blog

### Modeling Tail Risk In R With Value at Risk

In the first installment for my series on using R for portfolio analysis I reviewed the steps for installing R and R Studio, followed by some basic techniques for downloading asset prices, backtesting portfolios and generating risk and return profiles on investment strategies. Let’s go a bit deeper and focus on tail risk – extreme events that can take a heavy toll on assets and portfolios.

Almost every investment is subject to tail risk at some point. Three key questions arise for the topic: What’s the probability that a tail-risk event will strike? What’s the likely loss associated with tail risk? Can tail risk be avoided or at least minimized to some degree?

The answers vary depending on the investment strategy and other factors. In this primer we’ll keep things simple and focus on the first two questions: probability and expected loss.

Overview
The topic of studying, estimating and otherwise dissecting rare but extreme market events can be a black hole of analytical possibilities and econometric complexity. Yet the subject is too important to be left as a plaything for rocket scientists. Fortunately, R makes it easy to investigate the statistical equivalent of the tail wagging the dog.

A reasonable way to begin is with value at risk (VaR), which is one of the more widely used risk metrics in finance. VaR offers a probabilistic summary of the typical extreme-loss potential for an asset or portfolio. Although VaR has well-known flaws (in particular, the tendency to underestimate the worst-case scenarios), it’s become a staple in financial risk management. Its key attributes include a simple calculation methodology and presenting risk as a single number. Another plus from a coding perspective: estimating VaR only requires a few lines of code for basic applications.

Despite its flaws, VaR is a useful metric as a starting point for estimating tail risk, which is generally defined as the potential for an asset or portfolio’s price moving more than two or three standard deviations relative to the current price over a specified time period. Modeling tail risk can be challenging, primarily because these events, by definition, are rare. As a result, the supply of relevant data in the historical record is scarce, perhaps non-existent, depending on one’s definition of tail risk and the market or security under review. Some methodologies attempt to overcome this limitation with sophisticated statistical tools. For better or worse, VaR favors simplicity for approximating relatively extreme scenarios. Therein lies its strength and weakness.

Parametric VaR
VaR comes in three basic varieties: parametric, simulated, and historical. Let’s take a brief tour of each through an R lens, starting with parametric VaR, which has broad appeal because the required number crunching is minimal. The computational engine is a statistical distribution for estimating the potential for loss, based on two parameters: the average return for the time series under scrutiny and the standard deviation (volatility) of the return, which can be derived from the historical record or via forecasts. For processing the data, a normal distribution is the standard choice, although a number of alternative (and probably superior) possibilities exist as well. Keep in mind, too, that VaR calculations are based on a confidence level, with 95% and 99% as the most popular options.

Suppose that a portfolio has an average annual mean return of 7% that’s normally distributed with a 10% standard deviation. VaR at a 95% confidence level – denoted as VaR95 – can be calculated using R’s quantile function for a normal distribution. Note the use of 0.05 in qnorm via the p argument (vector of probabilities) to represent a 95% confidence level: 100% - 95% = 5%.

qnorm(p=0.05, mean=0.07, sd=0.10)
[1] -0.09448536

The estimated VaR95 in this case is an expected loss of roughly 9.44%. In other words, 95% of the time we can expect that losses for the portfolio will be less than or equal to -9.44%. It’s also true that 5% of the time the losses will exceed -9.44%.

To check the math, we can use pnorm, which runs the cumulative distribution function. The inputs are the same as qnorm with one critical difference: instead of providing a confidence level, we enter the return quantile. Using a ~9.44% decline computes the related confidence level associated with that loss; in this case, the resulting 5% matches the input in the qnorm calculation above.

pnorm(q=-0.09448536, mean=0.07, sd=0.10)
[1] 0.05

We can also run VaR on multiple probabilities simultaneously. For instance, here’s how VaR stacks up at the 95%, 99%, and 99.9% confidence levels.

qnorm(p=c(0.05, 0.01, 0.001), mean=0.07, sd=0.10)
[1] -0.09448536 -0.16263479 -0.23902323

Simulated VaR
For simulated VaR, the rnorm function can be used to create synthetic data that mimics a set of normally distributed returns. Let’s generate 10,000 returns with a mean of 7% and a 10% standard deviation and then estimate VaR95, VaR99, and VaR99.9 with the quantile function.

set.seed(89)
ret.sim <-rnorm(10000, mean=0.07, sd=0.10)
ret.sim.var <-quantile(ret.sim, c(0.05, 0.01, 0.001))
ret.sim.var

0.1%        1%          5%
-0.09906645 -0.16616088 -0.24076812

The simulated results for VaR are very close to the output using the qnorm function above. Why? Because in both cases the specified mean and standard deviation are the same.

Historical VaR
For the historical VaR, the focus turns to analyzing the actual returns for a particular sample period in the past. As an illustration, let’s use monthly returns for the S&P 500 via the managers\$SP500 dataset for 1996-2006 in the PerformanceAnalytics library. In practice, a longer history is recommended. Ideally, decades of history for a security, asset class, fund, or strategy are available – enough to represent risk and return over several business cycles. But for purposes of code demonstration, a short performance run will suffice. With a dataset is hand, finding the historical VaR95, for example, is a simple matter of locating the return at the fifth percentile. The quantile function does the work.

library(PerformanceAnalytics)
data(managers)
quantile(managers\$SP500, 0.05)

5%
-0.06691

We can visually inspect VaR95 in the context of the full dataset with a cumulative distribution graph. One possibility is sorting the returns and instructing R to identify the 5% quantile of performance with a dotted horizontal line via the lty=2 argument in the abline function, as shown in the code below. The equivalent loss in this case is roughly -0.067 (-6.7%). Note that for coding the sorted returns are initially transformed to a numeric file, which is required for charting by ignoring the  managers\$SP500 file’s out-of-sequence dates that result from sorting the returns from large to small.

sp500.sort <-sort(as.numeric(managers\$SP500))
sp500.sort.var95 <-quantile(sp500.sort,0.05)
plot(sp500.sort,
type='l',
main="Sorted S&P 500 Returns: 1996-2006",
ylab="monthly % return")
abline(h=sp500.sort.var95, lty = 2)
legend(lty=2, "bottomright", c("5% quantile"))

A widely recognized limitation of VaR in its standard form is the assumption that the return distribution of financial assets is normal. In reality, returns exhibit fat tails (non-normal distributions), an empirical fact that’s been documented in the finance literature since at least the 1960s. As a result, various modifications to the standard VaR have been developed to provide a more realistic risk assessment, including what’s known as the Cornish-Fisher application, commonly referred to as modified VaR (MVaR) or Cornish-Fisher VaR (CFVaR). The basic concept is one of modifying or “correcting” the standard VaR analysis based on the skew and kurtosis of the dataset’s distribution. Calculating MVaR is straightforward with the PerformanceAnalytics package’s VaR command, which can generate several versions of the risk metric. (For details, run ?VaR in R after loading the PerformanceAnalytics package.)

Another alternative to the standard VaR is expected shortfall (ES), also known as conditional VaR (CVaR) or expected tail loss (ETL). The logic here is that CVaR estimates the potential for loss beyond VaR by modeling the average loss in the tail. Specifically, CVaR calculates the expected loss that’s conditional on the loss exceeding a specified quantile, i.e., the VaR loss. CVaR can be computed using an assumption of normal or non-normal return distributions via the ES command in PerformanceAnalytics.

In the next installment of this series I’ll review some R coding examples for using non-normal distributions with VaR and the finer points for calculating expected shortfall.

James Picerno’s latest book is Quantitative Investment Portfolio Analytics In R:An Introduction To R For Modeling Portfolio Risk and Return. He’s also the editor of CapitalSpectator.com and The US Business Cycle Risk Report. (www.capitalspectator.com/premium-research).

James is the editor of CapitalSpectator.com, a finance/investment/economics blog with a focus on macroeconomics, the business cycle and portfolio strategy (with an emphasis on asset allocation and related analytics).

Picerno is also the author of Dynamic Asset Allocation: Modern Portfolio Theory Updated for the Smart Investor (Bloomberg Press, 2010) and Nowcasting The Business Cycle: A Practical Guide For Spotting Business Cycle Peaks (Beta Publishing, 2014). In addition, Picerno publishes The US Business Cycle Risk Report, a weekly newsletter that quantitatively evaluates US recession risk in real time.

This article is from CapitalSpectator.com and is being posted with CapitalSpectator.com’s permission. The views expressed in this article are solely those of the author and/or CapitalSpectator.com and IB is not endorsing or recommending any investment or trading discussed in the article. This material is for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IB to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

20040