# cvar 函数

损失的条件风险价值：其在最差的占比 1 - level 上的期望值。

## 怎么写

### cvar(distribution, level)

最差的占比 1 - level 上的期望损失：quantile(distribution, u) 在 u 从 level 到 1 时的均值。

**distribution**

损失的分布律：正态、对数正态、均匀、指数或威布尔分布。接受一个表达式。

**level**

水平，一个大于 0 小于 1 的数，例如 19/20，或一个符号。接受一个表达式。

### cvar(data, level)

损失中最差的占比 1 - level 的均值，并计入该占比最后落入的那个情景所占的部分。

**data**

这些损失，以等可能的情景给出。它们会被排序，因此不允许使用符号。接受一个列表。

**level**

水平，从 0 到 1：0 给出均值，1 给出最大的损失。接受一个数。

## 示例

- $\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)`

  在求解器中，`cvar(normal_distribution(0, 1), 19/20)` 的结果是 `10sqrt(2)*e^-inverse_erf(9/10)^2/sqrt(pi)`。

  [在求解器中打开](https://zh.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)`

  在求解器中，`cvar(exponential_distribution(2), 3/4)` 的结果是 `1/2 + ln(2)`。

  [在求解器中打开](https://zh.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)`

  在求解器中，`cvar(uniform_distribution(a, b), level)` 的结果是 `(b - a)(level + 1)/2 + a`。

  [在求解器中打开](https://zh.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)`

  在求解器中，`cvar([1, 2, 3, 4], 1/2)` 的结果是 `7/2`。

  [在求解器中打开](https://zh.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)`

  在求解器中，`cvar([1, 2, 3, 4], 3/5)` 的结果是 `29/8`。

  [在求解器中打开](https://zh.hilberon.com/?q=cvar%28%5B1%2C%202%2C%203%2C%204%5D%2C%203%2F5%29)

## 示例中用到的函数

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

  均值为 mu、标准差为 sigma 的正态分布。
- [exponential_distribution](https://zh.hilberon.com/library/exponential_distribution)

  以速率 lambda 发生的事件的等待时间。
- [uniform_distribution](https://zh.hilberon.com/library/uniform_distribution)

  在 a 与 b 之间任何位置等可能的连续分布。

## 相关函数

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

  变量及其所服从的分布，供概率和期望读取。
- [covariance](https://zh.hilberon.com/library/covariance)

  两个等长列表的样本协方差。
- [expectation](https://zh.hilberon.com/library/expectation)

  某变量的表达式在给定分布或过程下的期望值。
- [correlation](https://zh.hilberon.com/library/correlation)

  两个等长列表的相关系数。
- [cdf](https://zh.hilberon.com/library/cdf)

  服从某个分布的变量取值至多为某个值的概率。
- [fit](https://zh.hilberon.com/library/fit)

  用若干函数的组合对点集作最小二乘拟合。

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