# expectation 函数

某变量的表达式在给定分布或过程下的期望值。

## 怎么写

### expectation(f, distribution)

该表达式的期望值。

**f**

该变量的表达式。接受一个表达式。

**distribution**

该变量及其分布律，写作 distributed(x, d)。接受一个表达式。

## 示例

- $\displaystyle E[x^{2}],\ x \sim \mathcal{N}(0, 1) = 1$

  `expectation(x^2, distributed(x, normal_distribution(0, 1)))`

  在求解器中，`expectation(x^2, distributed(x, normal_distribution(0, 1)))` 的结果是 `1`。

  [在求解器中打开](https://zh.hilberon.com/?q=expectation%28x%5E2%2C%20distributed%28x%2C%20normal_distribution%280%2C%201%29%29%29)
- $\displaystyle E[e^{t x}],\ x \sim \mathcal{N}(\mu, \sigma) = e^{\frac{\sigma^{2} t^{2}}{2}+\mu t}$

  `expectation(exp(t x), distributed(x, normal_distribution(mu, sigma)))`

  在求解器中，`expectation(exp(t x), distributed(x, normal_distribution(mu, sigma)))` 的结果是 `e^(sigma^2*t^2/2 + mu*t)`。

  [在求解器中打开](https://zh.hilberon.com/?q=expectation%28exp%28t%20x%29%2C%20distributed%28x%2C%20normal_distribution%28mu%2C%20sigma%29%29%29)

## 示例中用到的函数

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

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

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

  指数函数；exp(x) 就是 e^x 这个幂。

## 相关函数

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

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

  用若干函数的组合对点集作最小二乘拟合。
- [cvar](https://zh.hilberon.com/library/cvar)

  损失的条件风险价值：其在最差的占比 1 - level 上的期望值。
- [geometric_brownian_motion_process](https://zh.hilberon.com/library/geometric_brownian_motion_process)

  以速率 mu 增长、相对噪声为 sigma、从 x0 出发的过程。
- [covariance](https://zh.hilberon.com/library/covariance)

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

  dX = mu X dt + sigma X dW 的路径，表示为维纳过程在时刻 t 的取值 w 的函数。

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