1 22 - 23Shares

It is important to have an estimate of risk for a portfolio when trading with financial assets. Two basic and commonly used risk measures are **value-at-risk** and **expected shortfall**. We will illustrate the two concepts with an example and provide the necessary R code to actually calculate the values of both measures.

If we take the price movements of an asset and draw the distribution of returns in a diagram, we might get a picture like the one below the code which shows the distribution of returns of BTC in USD from January 1, 2016 to August 1, 2017:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# In R # Let p be the price vector of a currency. Here: BTC/USD from CryptoCompare. # The Return vector contains the daily percentage of loss/profit. See below for its definition # # install.packages("lubridate") library("lubridate") # # Read in the data after downloading the .csv file from CryptoCompare p ‹- read.csv(file.choose()) # # Choose time period start ‹- ymd("2016-01-01") end ‹- ymd("2017-08-01") # # Select the period of prices p$time ‹- ymd(p$time) int ‹- interval(start, end) p ‹- p[p$time %within% int,] # # Calculate the returns of closing price numerator ‹- diff(p[,2], k=1) denominator ‹- head(p[,2], -1) Return ‹- numerator/denominator hist(Return) |

The left side of the histogram depicts the frequency of losses. We can see that the right side seems to have more “weight” predicting more profit on average (without considering fees,…). But what is the risk? The **value-at-risk** measure (see below) tells us the minimum we should expect to lose in one event within a given time period. We use the value-at-risk to calculate the **expected shortfall** which predicts our actual loss during that same event.

Suppose we have invested in one currency whose price movement is p_{i} where p_{i} is the closing price of this currency in USD at some exchange and the variable i varies over the days of a specified time period. The **simple return** at day i+1 is then defined as r_{i+1} := (p_{i+1}– p_{i}) / p_{i}. (Note the logarithmic return is calculated differently: log(p_{i+1}/p_{i})). In the sequel, we let r:=(r_{1}, … , r_{n}) be the vector of returns.

Suppose further that we have collected return values r_{i} over a hundred days. The **value-at-risk** gives us an estimate for the value x such that 5% of the time we lose more than x. It is common to denote the value-at-risk of a return vector r by VaR_{α}(r). So, for example, if VaR_{.95}(r) = 0.01 then we should expect to lose more than 1% once every 5 days (note: 5 = (1 – 0.95) x 100).

Let us look at the example of BTC price in USD from January 1, 2016 to August 1, 2017 (579 days). If we again choose a 95 % confidence level, we obtain VaR_{.95}(r) = 0.055. This result means that approximately once every 29 days (0.05 x 579), we will lose at least 5.5% in this day. You can use the following R code to determine the *non-parametric* VaR which is based on historical data:

1 2 3 4 5 6 7 8 |
VaRhistorical ‹- function(returnVector, alpha=.95, digits=2) { result ‹- -quantile(returnVector, 1- alpha) signif(result, digits=digits) } # The result for the Return of BTC from above VaRresult ‹- VaRhistorical(Return) print(VaRresult) |

1 2 |
## 5% ## 0.055 |

*The mathematical definition is: Let P be a probability density of the returns X, then the value-at-risk of level α is defined as* VaR_{α}(X):= inf { x ∈ **R** | P(X<-x) ≤ 1- α}.

Next, we can ask what loss should we expect on that day. The *historical* **expected shortfall** is just the mean of all losses which are higher than the value-at-risk. For the BTC returns example the historical expected shortfall ES_{.95}(r) is 0.086 which means that on the day we lose more than 5.5%, we can expect to actually lose 8.6%. In R this can easily caclulated from historical data as follows:

1 2 3 4 5 6 7 8 |
EShistorical ‹- function(returnVector, alpha=.95, digits=2) { v ‹- quantile(returnVector, 1- alpha) result ‹- -mean(returnVector[returnVector <= v]) signif(result, digits=digits) } ESresult ‹- EShistorical(Return) print(ESresult) |

1 |
## [1] 0.086 |

*The mathematical definition: Let X be the returns, then the expected shortfall of level α is defined as* ES_{α}(X) := E[X | X > VaR_{α}(X)], *where E denotes the expected value. Note that the historical expected shortfall uses the mean instead of expected value.*

The value-at-risk and expected shortfall can give a portfolio manager a useful and complementary tool in estimating the risk of the assets in his or her portfolio. If you are working with more than one asset in your portfolio, the value-at-risk of the entire portfolio may be erroneously higher than the sum of the individual value-at-risk of your assets, apparently contradicting the idea of diversification. Please refer to this article [3] for more about the limitations of VaR. There are also ohter variations of VaR, e.g. parametric VaR or VaR with Monte Carlo Simulation. To learn more about complementary risk measure tools, see e.g. this article [1].

Download the R script below to calculate the VaR and ES of your preferred cryptocurrency after downloading the necessary price charts as a csv file from CryptoCompare. To get the csv file, go to their website and choose the cryptocurrency you are interested in. Go to charts where you can download the data as a csv file. Alternatively, one could also use their API.

We close with a list of VaR and ES of some common cryptocurrencies with parameter (α = .95) for the period from January 1, 2016 to August 1, 2017 and include EUR as a benchmark. The interpretation is as above: E.g., approximately every 29 days one should expect to lose at least 6,1% of LTC, and on this specific day one should expect to lose 10%.

. | BTC | LTC | XRP | Dash | ETC | ETH | XMR | EURO |
---|---|---|---|---|---|---|---|---|

VaR | 0.055 | 0.061 | 0.077 | 0.078 | 0.081 | 0.085 | 0.11 | 0.0083 |

ES | 0.086 | 0.1 | 0.14 | 0.14 | 0.11 | 0.14 | 0.15 | 0.013 |

- Data: Our data is from CryptoCompare
- An advanced package for VaR and ES, Lubridate package to select time periods
- Our R code for VaR and ES

More information can be found in

[1]: Overview of Statistical and Extreme Value Analysis (by Osterrieder, Lorenz, and Strika)

[2]: Coherent Measure of Risk (by Artzner, Delbaen, Eber, and Heath)

[3]: Exploring the Limitations of Value at Rsik (by Krause)

## If you liked this article, you might also like:

- Live Updated Cryptocurrency Investment Spreadsheet
- Key Metrics of Blockchain Platforms
- Security Audit Real Estate Crypto Token

1 22 - 23Shares