# 方差（Variance，variance）

一组数的样本方差，或一个分布的方差。

## 怎么写

### variance(data)

各数与均值之差的平方和除以 n - 1。

**data**

至少两个数。允许使用符号。接受一个列表。

### variance(distribution)

一个分布的方差。

**distribution**

一个分布，例如 normal_distribution(mu, sigma)。接受一个表达式。

### variance(process)

随机过程在某个时刻的方差。

**process**

某个时刻的过程，写成 construct(p, t)，对于已存储的过程 p 可写成 p(t)。接受一个表达式。

### variance(process, t)

同上，时间写在过程旁边。

**process**

一个过程，例如 geometric_brownian_motion_process(mu, sigma, x0)。接受一个表达式。

**t**

时间，一个变量或一个数。接受一个表达式。

## 示例

- $\displaystyle 2, 4, 4, 4, 5, 5, 7, 9 = \frac{32}{7}$

  `variance([2, 4, 4, 4, 5, 5, 7, 9])`

  在求解器中，`variance([2, 4, 4, 4, 5, 5, 7, 9])` 的结果是 `32/7`。

  [在求解器中打开](https://zh.hilberon.com/?q=variance%28%5B2%2C%204%2C%204%2C%204%2C%205%2C%205%2C%207%2C%209%5D%29)
- $\displaystyle a, b, c = \frac{a^{2}-a b-a c+b^{2}-b c+c^{2}}{3}$

  `variance([a, b, c])`

  在求解器中，`variance([a, b, c])` 的结果是 `(a^2 - a*b - a*c + b^2 - b*c + c^2)/3`。

  [在求解器中打开](https://zh.hilberon.com/?q=variance%28%5Ba%2C%20b%2C%20c%5D%29)
- $\displaystyle \operatorname{Var}(X),\ X \sim \mathrm{B}(n, p) = n p (1-p)$

  `variance(binomial_distribution(n, p))`

  在求解器中，`variance(binomial_distribution(n, p))` 的结果是 `n*p*(1 - p)`。

  [在求解器中打开](https://zh.hilberon.com/?q=variance%28binomial_distribution%28n%2C%20p%29%29)
- $\displaystyle \operatorname{Var}(X),\ X \sim \mathrm{U}(a, b) = \frac{(b-a)^{2}}{12}$

  `variance(uniform_distribution(a, b))`

  在求解器中，`variance(uniform_distribution(a, b))` 的结果是 `(b - a)^2/12`。

  [在求解器中打开](https://zh.hilberon.com/?q=variance%28uniform_distribution%28a%2C%20b%29%29)
- $\displaystyle \operatorname{construct}(\operatorname{OU}(\mu, \sigma, \theta, x_{0}), t) = \begin{cases} \frac{\sigma^{2} \left(1-\frac{1}{e^{2 t \theta}}\right)}{2 \theta} & \text{if } \theta \neq 0 \end{cases}$

  `variance(construct(ornstein_uhlenbeck_process(mu, sigma, theta, x0), t))`

  在求解器中，`variance(construct(ornstein_uhlenbeck_process(mu, sigma, theta, x0), t))` 的结果是 `(1/2)sigma^2*(1 - e^(-2t*theta))/theta if theta != 0`。

  [在求解器中打开](https://zh.hilberon.com/?q=variance%28construct%28ornstein_uhlenbeck_process%28mu%2C%20sigma%2C%20theta%2C%20x0%29%2C%20t%29%29)
- $\displaystyle \operatorname{Var}(X_{t}),\ X \sim \operatorname{GBM}(\mu, \sigma, x_{0}) = x_{0}^{2} e^{2 \mu t} (e^{\sigma^{2} t}-1)$

  `variance(geometric_brownian_motion_process(mu, sigma, x0), t)`

  在求解器中，`variance(geometric_brownian_motion_process(mu, sigma, x0), t)` 的结果是 `x0^2*e^(2mu*t)*(e^(sigma^2*t) - 1)`。

  [在求解器中打开](https://zh.hilberon.com/?q=variance%28geometric_brownian_motion_process%28mu%2C%20sigma%2C%20x0%29%2C%20t%29)
- $\displaystyle \operatorname{variance}(\operatorname{wiener\_process}(\mu, \sigma), t) = \sigma^{2} t$

  `variance(wiener_process(mu, sigma), t)`

  在求解器中，`variance(wiener_process(mu, sigma), t)` 的结果是 `sigma^2*t`。

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

## 示例中用到的函数

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

  n 次独立试验中成功的次数，每次试验成功的概率为 p。
- [uniform_distribution](https://zh.hilberon.com/library/uniform_distribution)

  在 a 与 b 之间任何位置等可能的连续分布。
- [construct](https://zh.hilberon.com/library/construct)

  把本身是计算得到的头部作用于参数，例如 f'(0) 或 (x => body)(a)。
- [ornstein_uhlenbeck_process](https://zh.hilberon.com/library/ornstein_uhlenbeck_process)

  以速率 theta 被拉向 mu、噪声为 sigma、从 x0 开始的随机过程。
- [geometric_brownian_motion_process](https://zh.hilberon.com/library/geometric_brownian_motion_process)

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

  带漂移 mu 和噪声 sigma、从 0 开始的布朗运动。

## 相关函数

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

  一个单元存活到时间 x 的概率为 e^(-(x/scale)^shape) 时的寿命。
- [stationary_distribution](https://zh.hilberon.com/library/stationary_distribution)

  过程随时间趋于的分布，若存在的话。
- [affine_sde_moments](https://zh.hilberon.com/library/affine_sde_moments)

  从 x0 出发的 dX = (a + b X) dt + sigma dW 在时刻 t 的均值和方差。
- [standard_deviation](https://zh.hilberon.com/library/standard_deviation)

  一组数的样本标准差，或分布的标准差。
- [quantile](https://zh.hilberon.com/library/quantile)

  列表中至少有占比 q 的数据不超过的最小值，或分布中变量以概率 q 不超过的值。
- [cdf](https://zh.hilberon.com/library/cdf)

  服从某个分布的变量取值至多为某个值的概率。

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