Original link:tecdat.cn/?p=15929

Original source:Tuo End number according to the tribe public number

 


Value at risk (VaR) and expected loss (ES) are common risk measures.

Let’s be clear:

  1. Time frame – How many days are we looking ahead?
  2. Probability level – what do we think about tail distribution?

An example of the profit and loss forecast distribution over a given time horizon is shown in Figure 1.

Figure 1: Projected profit and loss distribution

The quantile predicted for a given level of probability.

Figure 2: Forecast profit and loss distribution with quantiles

Beyond the end of the quantile.

Figure 3: Forecast profit and loss distribution with quantile and tail markers

methods

 

Value at risk (VaR) is the negative quantile of the predicted distribution at the selected probability level. Thus, the VaR in Figures 2 and 3 is about 1.1 million yuan.

The expected value of loss (ES) is the negative value of the trailing expected value beyond the VaR (the gold area in Figure 3). Therefore, it is always larger than the corresponding VaR.

The alias

Expected loss

Expected loss has many nicknames:

  • Conditional value at risk (CVaR)
  • The average shortage
  • Average excess loss

I find “conditional value at risk” confusing. I can see that people think of it as a var under certain conditions, rather than an expected loss above var.

Average excess loss seems to be the most descriptive name.

At the top, we see a concept with multiple names. Below, we see a name with multiple concepts.

Probability level

When I say 5%, someone says 95%. We’re all dealing with tails, which means (in my terminology) it’s definitely less than 50%.

abbreviations

The abbreviation “value at risk” can be confused with two other concepts:

  • The variance
  • Vector autoregression

All of this can avoid conflicts with capitalized conventions:

  • VaR: value at risk
  • Variance var:
  • VAR: vector autoregression

To estimate the

The initial composition

There are two initial components:

  • Assets in a portfolio
  • The price history of the asset in question

Derived ingredients

The portfolio plus the current price gives the portfolio weight.

The price history matrix is used to get the return history matrix.

Given the return history of a portfolio, the forecast distribution can be obtained in a number of ways:

  • The fitting hypothesis distribution
  • Simulation (using a distribution of experience over time)
  • The overall forecast
  • Gradient simulation

If a normal distribution is assumed, the standard deviation can be estimated to obtain the predicted distribution. If you assume a T-distribution, you also need estimated degrees of freedom or hypothetical degrees of freedom.

What is often referred to as a simulation approach is really just an empirical distribution of portfolio returns that uses some specific number.

The univariate GARCH model can be used to estimate VaR and ES well.

 

Rlanguage

For VaR and ES, the R language is a very suitable environment.

Filled areas

You may be wondering how to fill the areas 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)
 
Copy the code

 

Portfolio variance calculation

Given the R command of the variance matrix and weight vector to obtain the portfolio variance:

weight %*% varianceMatrix %*% weight
Copy the code

Assume that the weight vector is perfectly aligned with the variance matrix.

weight %*% varianceMatrix[names(weight), 
   names(weight)] %*% weight
Copy the code

 

Estimate value at risk and expected loss

 

A brief introduction to estimating value-at-risk and expected loss, and estimating using R.

 

basic

Value at risk (VaR) and expected shortfall (ES) are always relevant to the portfolio.

You need two basic ingredients:

  • portfolio
  • The price history of the asset in question

 

These can be used to estimate market risk. Price history may not include other risks, such as credit risk.

Multiple estimates

When we start at the asset level, both VaR and ES are risk numbers at the portfolio level. One approach is to estimate the variance matrix of asset returns and then fold it into portfolio variance using portfolio weights.

 

Univariate estimation

It is easier to estimate from a single time series return of the portfolio (now the portfolio).

We can obtain this information by multiplying the simple return matrix of the assets in the portfolio by the matrix of the portfolio weights.

R1 <- assetSimpRetMatrix %*% portWts
Copy the code

Or:

R1 <- assetSimpRetMatrix[, names(portWts)] %*% portWts
Copy the code

 

The object calculated above R1 holds the (hypothetical) simple return on the portfolio.

r1 <- log(R1 + 1)
Copy the code

Of course, there are other options, but some common ones are:

  • Historical (using the most recent distribution of experiences)
  • Normally distributed (estimate parameters from data) and use appropriate quantiles
  • T-distribution (usually assumed degrees of freedom rather than estimated degrees of freedom)
  • The univariate GARCH model was fitted and simulated in advance

 

R analysis

Here’s an example, where SPXRET11 contains a vector of the daily logarithmic returns of the S&P 500 in 2011. As a result, we will have a measure of risk (return) for the first day of 2012.

< p style = "max-width: 100%; clear: both; min-height: 1px; [,1] Gaussian (gaussian) [,1] Gaussian (GaussianCopy the code

If the first parameter is a matrix, then each column can be treated 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
Copy the code

 

Historical estimates of value at risk

This is the definition of a simple function for historical estimates of value-at-risk:

 
Copy the code
VaRhistorical <- function(returnVector, prob=.05, 
    notional=1, digits=2) 
{
  if(prob > .5) prob <- 1 - prob
  ans <- -quantile(returnVector, prob) * notional
  signif(ans, digits=digits)
}
Copy the code

Portfolio, for example:

> VaRhistorical(spxret11, notional=13e6)
    5% 
330000
Copy the code

Expected loss:

EShistorical <- function(returnVector, prob=.05, 
    notional=1, digits=2) 
{
 
Copy the code

It can be used like this:

> EShistorical(spxret11, notional=13e6)
[1] 470000
Copy the code

Thus, var is 330,000 and expected loss 470,000.

Normal distribution

There will be a better version later (in a statistical sense), but this is an easy way to get “var” by assuming a normal distribution:

Usage:

> VaRnormalEqwt(spxret11, notional=13e6)
[1] 310000
> VaRnormalEqwt(spxret11, notional=13e6, 
+     expected.return=0)
[1] 310000
Copy the code

In this case, calculating the expected value of the loss is a little bit more complicated, because we need to find the expected value of the tail.

ESnormalEqwt <- function(returnVector, prob=.05, 
    notional=1, expected.return=mean(returnVector), 
    digits=2)
{




  ans <- -tailExp * notional
  signif(ans, digits=digits)
 
Copy the code

The result of this example is:

> ESnormalEqwt(spxret11, notional=13e6)
[1] 390000
Copy the code

A better approach is to exponentially smooth the resulting volatility:

VaRnormalExpsmo <- function(returnVector, prob=.05, 
     notional=1, expected.return=mean(returnVector), 
     lambda=.97, digits=2)
{



  signif(ans, digits=digits)
 
Copy the code

Where pp. Exponential. Smooth comes from “exponential decay model”.

> VaRnormalExpsmo(spxret11, notional=13e6)
[1] 340000
Copy the code

T distribution

 

VaRtExpsmo <- function(returnVector, prob=.05, 
    notional=1, lambda=.97, df=7, digits=2)
{
  if(prob > .5) prob <- 1 - prob
 


 
Copy the code

The result is:

 
Copy the code
> VaRtExpsmo(spxret11, notional=13e6)
2011-12-30 
    340000
Copy the code

 

reference

1. Empirical research on R language fitting and prediction based on ArMA-GarCH-VAR model

2. Stochastic model of time-varying parameter VAR in R language

3. Stochastic model of time-varying parameter VAR in R language

4. VAR fitting and prediction based on ARMA-GARCH process for R language

5. VaR comparison of GARCH (1,1), MA and historical simulation method

6. Stochastic model of time-varying parameter VAR in R language

7.R language to achieve vector automatic regression VAR model

8.R language random search variable selection SSVS estimation Bayesian vector autoregression (BVAR) model

9. Impulse response analysis of different types of VAR models in R language