# cvar function

The conditional value at risk of a loss: its expected value over the worst share 1 - level.

## How to write it

### cvar(distribution, level)

The expected loss in the worst share 1 - level: the mean of quantile(distribution, u) for u from level to 1.

**distribution**

The law of the loss: a normal, lognormal, uniform, exponential or Weibull one. Takes an expression.

**level**

The level, a number above 0 and below 1 such as 19/20, or a symbol. Takes an expression.

### cvar(data, level)

The mean of the worst share 1 - level of the losses, with the part of a scenario that the share ends in.

**data**

The losses, as scenarios that are equally likely. They are put in order, so symbols are not allowed. Takes a list.

**level**

The level, from 0 to 1: 0 gives the mean and 1 the largest loss. Takes a number.

## Examples

- $\displaystyle \operatorname{cvar}\left(\operatorname{normal\_distribution}(0, 1), \frac{19}{20}\right) = \frac{10 \sqrt{2}}{\sqrt{\pi} e^{\operatorname{erf}^{-1}\left(\frac{9}{10}\right)^{2}}}$

  `cvar(normal_distribution(0, 1), 19/20)`

  In the solver, `cvar(normal_distribution(0, 1), 19/20)` gives `10sqrt(2)*e^-inverse_erf(9/10)^2/sqrt(pi)`.

  [Open in the solver](https://hilberon.com/?q=cvar%28normal_distribution%280%2C%201%29%2C%2019%2F20%29)
- $\displaystyle \operatorname{cvar}\left(\operatorname{exponential\_distribution}(2), \frac{3}{4}\right) = \frac{1}{2}+\ln(2)$

  `cvar(exponential_distribution(2), 3/4)`

  In the solver, `cvar(exponential_distribution(2), 3/4)` gives `1/2 + ln(2)`.

  [Open in the solver](https://hilberon.com/?q=cvar%28exponential_distribution%282%29%2C%203%2F4%29)
- $\displaystyle \operatorname{cvar}(\operatorname{uniform\_distribution}(a, b), \operatorname{level}) = \frac{(b-a) (\operatorname{level}+1)}{2}+a$

  `cvar(uniform_distribution(a, b), level)`

  In the solver, `cvar(uniform_distribution(a, b), level)` gives `(b - a)(level + 1)/2 + a`.

  [Open in the solver](https://hilberon.com/?q=cvar%28uniform_distribution%28a%2C%20b%29%2C%20level%29)
- $\displaystyle \operatorname{cvar}\left(\left[ 1, 2, 3, 4 \right], \frac{1}{2}\right) = \frac{7}{2}$

  `cvar([1, 2, 3, 4], 1/2)`

  In the solver, `cvar([1, 2, 3, 4], 1/2)` gives `7/2`.

  [Open in the solver](https://hilberon.com/?q=cvar%28%5B1%2C%202%2C%203%2C%204%5D%2C%201%2F2%29)
- $\displaystyle \operatorname{cvar}\left(\left[ 1, 2, 3, 4 \right], \frac{3}{5}\right) = \frac{29}{8}$

  `cvar([1, 2, 3, 4], 3/5)`

  In the solver, `cvar([1, 2, 3, 4], 3/5)` gives `29/8`.

  [Open in the solver](https://hilberon.com/?q=cvar%28%5B1%2C%202%2C%203%2C%204%5D%2C%203%2F5%29)

## Functions its examples use

- [normal_distribution](https://hilberon.com/library/normal_distribution)

  The normal distribution with mean mu and standard deviation sigma.
- [exponential_distribution](https://hilberon.com/library/exponential_distribution)

  The waiting time for events that come at rate lambda.
- [uniform_distribution](https://hilberon.com/library/uniform_distribution)

  The continuous distribution that is equally likely anywhere between a and b.

## Related functions

- [distributed](https://hilberon.com/library/distributed)

  A variable and the distribution it follows, as probability and expectation read it.
- [covariance](https://hilberon.com/library/covariance)

  The sample covariance of two lists of the same length.
- [expectation](https://hilberon.com/library/expectation)

  The expected value of an expression of a variable with a distribution or a process.
- [correlation](https://hilberon.com/library/correlation)

  The correlation coefficient of two lists of the same length.
- [cdf](https://hilberon.com/library/cdf)

  The probability that a variable with a distribution is at most a value.
- [fit](https://hilberon.com/library/fit)

  The least-squares fit of a combination of functions through points.

---
Source: https://hilberon.com/library/cvar
