Original link: http://tecdat.cn/?p=15929
VaR and ES are common risk measures.
First, make it clear:
- Time frame - how many days do we look forward to?
- Probability level - what do we think of the tail distribution?
An example of profit and loss forecast distribution within a given time range is shown in Figure 1.
Figure 1: forecast profit and loss distribution

Quantile of the prediction for a given probability level.
Figure 2: forecast profit and loss distribution with quantile

Tail beyond quantile.
Figure 3: forecast profit and loss distribution with quantile and tail mark

method
Value at risk (VaR) is the negative number of quantiles of the predicted distribution at the selected probability level. Therefore, the VaR in figures 2 and 3 is about 1.1 million yuan.
The loss expectation (ES) is a negative value that exceeds the tail expectation of VaR (the gold area in Figure 3). Therefore, it is always greater than the corresponding var.
alias
Loss expectation
Loss expectations have many aliases:
- Conditional value at risk (CVaR)
- Average shortage
- Average excess loss
I find "conditional value at risk" confusing. I can see that people think it is a value at risk under certain conditions, not an expected loss beyond the value at risk.
Average excess loss seems to be the most descriptive name.
Above, we see a concept with multiple names. Below, we see a name with multiple concepts.
Probability level
When I say 5%, someone says 95%. In fact, we are all dealing with tails, which means (in my terminology) it must be less than 50%.
abbreviation
The abbreviation "value at risk" may be confused with two other concepts:
- variance
- Vector autoregression
All of these can avoid conflicts with capitalization conventions:
- VaR: value at risk
- var: variance
- VAR: vector autoregressive
estimate
Initial composition
There are two initial components:
- Assets in the portfolio
- Price history of the assets involved
Derived component
The portfolio weight is obtained by adding the current price to the portfolio.
The price history matrix is used to obtain the return history matrix.
Given the return history of a portfolio, the predicted distribution can be obtained in a variety of ways:
- Fitting hypothetical distribution
- Simulation (using empirical distribution over a period of time)
- Overall forecast
- Gradient simulation
If a normal distribution is assumed, the standard deviation can be estimated to obtain the predicted distribution. If the t-distribution is assumed, it is also necessary to estimate the degrees of freedom or assumed degrees of freedom.
The method commonly known as the simulation method actually only uses the empirical distribution of some specific number of portfolio returns.
VaR and ES can be well estimated using univariate garch model.
R language
For VaR and ES, R language is a very suitable environment.
Filled area
You may want to know how to fill the area in the diagram, as shown in Figure 3. The trick is to use the polygon function.
plot(xseq, pd, type="l", col="steelblue", lwd=3, yaxt="n", ylab="", xlab="Predicted Profit/Loss (millions of dollars)") abline(v=qnorm(.05, mean=.5, sd=1), lty=2, lwd=3) polygon(c(xseqt, max(xseqt)), c(dnorm(xseqt, mean=.5, sd=1), 0), col="gold", border=NA) lines(xseq, pd, type="l", col="steelblue", lwd=3) abline(h=0, col="gray80", lwd=2)

Portfolio variance calculation
Given the R command of variance matrix and weight vector to obtain portfolio variance:
weight %*% varianceMatrix %*% weight
It is assumed that the weight vector is perfectly aligned with the variance matrix.
weight %*% varianceMatrix\[names(weight), names(weight)\] %*% weight
Estimation of value at risk and expected loss
An introduction to assessing VaR and loss expectations, and estimating using R.
basic
Value at risk (VaR) and expected shortfall (ES) are always related to the portfolio.
You need two basic ingredients:
- investment portfolio
- Price history of the assets involved
These can be used to estimate market risk. Other risks, such as credit risk, may not be included in the price history.
Multivariate estimation
When we start at the asset level, VaR and ES are both risk numbers at the portfolio level. One method is to estimate the variance matrix of asset return, and then fold it into portfolio variance using portfolio weight.
Univariate estimation
It is easier to estimate through the single time series return of the portfolio, which is now the portfolio.
We can obtain this information by multiplying the simple return matrix of assets in the portfolio by the matrix of portfolio weight.
R1 <- assetSimpRetMatrix %*% portWts
Or:
R1 <- assetSimpRetMatrix\[, names(portWts)\] %*% portWts
R1 the object calculated above holds the (hypothetical) simple return of the portfolio.
r1 <- log(R1 + 1)
Of course, there are other options, but some common methods are:
- Historical (using the experience distribution in the recent period)
- Normal distribution (estimate parameters from data) and use appropriate quantiles
- t-distribution (usually assuming degrees of freedom rather than estimating degrees of freedom)
- Fit the univariate garch model and simulate it in advance
R analysis
The following is an example where spxret11 contains a vector of daily log returns for the 2011 S & P 500 index. Therefore, we will get the risk measurement (return) on the first day of 2012.
> "historical") \[,1\] VaR -0.02515786 > "gaussian") \[,1\] VaR -0.0241509 > "gaussian" \[,1\] VaR -0.03415703 > "historical") \[,1\] ES -0.03610873 > "gaussian") \[,1\] ES -0.03028617
If the first parameter is a matrix, each column can be regarded as an asset in the portfolio.
no weights passed in, assuming equal weighted portfolio $MVaR \[,1\] \[1,\] 0.02209855 $contribution Convertible Arbitrage CTA Global 0.0052630876 -0.0001503125 Distressed Securities Emerging Markets 0.0047567783 0.0109935244 Equity Market Neutral 0.0012354711 $pct\_contrib\_MVaR Convertible Arbitrage CTA Global 0.238164397 -0.006801916 Distressed Securities Emerging Markets 0.215252972 0.497477204 Equity Market Neutral 0.055907342
Historical estimation of value at risk
This is the definition of a simple function for historical estimation of VaR:
VaRhistorical <- function(returnVector, prob=.05, notional=1, digits=2) { if(prob > .5) prob <- 1 - prob ans <- -quantile(returnVector, prob) * notional signif(ans, digits=digits) }
Portfolio, e.g.:
> VaRhistorical(spxret11, notional=13e6) 5% 330000
Loss expectation:
EShistorical <- function(returnVector, prob=.05, notional=1, digits=2) {
You can use this:
> EShistorical(spxret11, notional=13e6) \[1\] 470000
Therefore, the VaR is 330000 and the expected loss is 470000.
Normal distribution
There will be a better version later (statistically speaking), but this is a simple way to obtain "value at risk" by assuming a normal distribution:
The usage is as follows:
> VaRnormalEqwt(spxret11, notional=13e6) \[1\] 310000 > VaRnormalEqwt(spxret11, notional=13e6, + expected.return=0) \[1\] 310000
In this case, calculating the loss expectation is a little complicated because we need to find the expectation of the tail.
ESnormalEqwt <- function(returnVector, prob=.05, notional=1, expected.return=mean(returnVector), digits=2) { ans <- -tailExp * notional signif(ans, digits=digits)
The result of this example is:
> ESnormalEqwt(spxret11, notional=13e6) \[1\] 390000
A better way is to use the volatility obtained by exponential smoothing:
VaRnormalExpsmo <- function(returnVector, prob=.05, notional=1, expected.return=mean(returnVector), lambda=.97, digits=2) { signif(ans, digits=digits)
Including pp.exponential Smooth is taken from the exponential decay model.
> VaRnormalExpsmo(spxret11, notional=13e6) \[1\] 340000
t distribution
VaRtExpsmo <- function(returnVector, prob=.05, notional=1, lambda=.97, df=7, digits=2) { if(prob > .5) prob <- 1 - prob
The result is:
> VaRtExpsmo(spxret11, notional=13e6) 2011-12-30 340000