# affine_sde_moments 函数

从 x0 出发的 dX = (a + b X) dt + sigma dW 在时刻 t 的均值和方差。

## 怎么写

### affine_sde_moments(a, b, sigma, x0, time)

给出 {mean: ..., variance: ...}，b = 0 的情形单独处理；有一条说明注明它何时成立。

**a**

漂移的常数部分。接受一个表达式。

**b**

速率：漂移为 a + b X。接受一个表达式。

**sigma**

噪声强度。接受一个表达式。

**x0**

起始值，不随时间变化。接受一个表达式。

**time**

时间，一个变量。接受一个变量或变量列表。

### affine_sde_moments(a, b, sigma, x0, time)

对最多六个状态同理；结果包含均值和协方差。

**a**

各状态漂移的常数部分，放在方括号中。接受一个列表。

**b**

漂移矩阵，每个状态一行。接受一个矩阵，写作行组成的列表。

**sigma**

噪声：每个状态一行，每个驱动一列。接受一个矩阵，写作行组成的列表。

**x0**

各状态的起始值。接受一个列表。

**time**

时间，一个变量。接受一个变量或变量列表。

## 示例

- $\displaystyle (E[X_{t}], \operatorname{Var}(X_{t})) \text{ 其中 } \mathrm{d}X_{t} = (a+b X_{t})\,\mathrm{d}t+\sigma\,\mathrm{d}W_{t},\ X_{0} = x_{0} \Rightarrow \begin{aligned} E[X_{t}] &= \begin{cases} x_{0} e^{b t}+\frac{a (e^{b t}-1)}{b} & \text{当 } b \neq 0 \text{ 时} \\ x_{0}+a t & \text{其他} \end{cases} \\ \operatorname{Var}(X_{t}) &= \begin{cases} \frac{\sigma^{2} (e^{2 b t}-1)}{2 b} & \text{当 } b \neq 0 \text{ 时} \\ \sigma^{2} t & \text{其他} \end{cases} \end{aligned}$

  `affine_sde_moments(a, b, sigma, x0, t)`

  在求解器中，`affine_sde_moments(a, b, sigma, x0, t)` 的结果是 `{mean: piecewise(x0*e^(b*t) + a*(e^(b*t) - 1)/b if b != 0, x0 + a*t otherwise), variance: piecewise((1/2)sigma^2*(e^(2b*t) - 1)/b if b != 0, sigma^2*t otherwise)}`。

  [在求解器中打开](https://zh.hilberon.com/?q=affine_sde_moments%28a%2C%20b%2C%20sigma%2C%20x0%2C%20t%29)
- $\displaystyle (E[X_{t}], \operatorname{Var}(X_{t})) \text{ 其中 } \mathrm{d}X_{t} = (1+2 X_{t})\,\mathrm{d}t+3\,\mathrm{d}W_{t},\ X_{0} = 0 \Rightarrow \begin{aligned} E[X_{t}] &= \frac{e^{2 t}-1}{2} \\ \operatorname{Var}(X_{t}) &= \frac{9 (e^{4 t}-1)}{4} \end{aligned}$

  `affine_sde_moments(1, 2, 3, 0, t)`

  在求解器中，`affine_sde_moments(1, 2, 3, 0, t)` 的结果是 `{mean: (e^(2t) - 1)/2, variance: 9(e^(4t) - 1)/4}`。

  [在求解器中打开](https://zh.hilberon.com/?q=affine_sde_moments%281%2C%202%2C%203%2C%200%2C%20t%29)
- $\displaystyle \operatorname{affine\_sde\_moments}\left(\left[ 0, 0 \right], \begin{pmatrix} -1 & 2 \\ 0 & -3 \end{pmatrix}, \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}, \left[ x_{0}, y_{0} \right], t\right) = \left[ \text{mean} \to \left[ \frac{x_{0}}{e^{t}}+y_{0} \left(e^{-t}-\frac{1}{e^{3 t}}\right), \frac{y_{0}}{e^{3 t}} \right], \text{covariance} \to \begin{pmatrix} -\frac{1}{e^{2 t}}+\frac{1}{2 e^{4 t}}-\frac{1}{6 e^{6 t}}+\frac{2}{3} & -\frac{1}{4 e^{4 t}}+\frac{1}{6 e^{6 t}}+\frac{1}{12} \\ -\frac{1}{4 e^{4 t}}+\frac{1}{6 e^{6 t}}+\frac{1}{12} & \frac{1}{6}-\frac{1}{6 e^{6 t}} \end{pmatrix} \right]$

  `affine_sde_moments([0, 0], [[-1, 2], [0, -3]], [[1, 0], [0, 1]], [x0, y0], t)`

  在求解器中，`affine_sde_moments([0, 0], [[-1, 2], [0, -3]], [[1, 0], [0, 1]], [x0, y0], t)` 的结果是 `{mean: [x0*e^-t + y0*(e^-t - e^(-3t)), y0*e^(-3t)], covariance: [[-e^(-2t) + e^(-4t)/2 - e^(-6t)/6 + 2/3, -e^(-4t)/4 + e^(-6t)/6 + 1/12], [-e^(-4t)/4 + e^(-6t)/6 + 1/12, 1/6 - e^(-6t)/6]]}`。

  [在求解器中打开](https://zh.hilberon.com/?q=affine_sde_moments%28%5B0%2C%200%5D%2C%20%5B%5B-1%2C%202%5D%2C%20%5B0%2C%20-3%5D%5D%2C%20%5B%5B1%2C%200%5D%2C%20%5B0%2C%201%5D%5D%2C%20%5Bx0%2C%20y0%5D%2C%20t%29)

## 相关函数

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

  dX = mu X dt + sigma X dW 的路径，表示为维纳过程在时刻 t 的取值 w 的函数。
- [binomial_distribution](https://zh.hilberon.com/library/binomial_distribution)

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

  带漂移 mu 和噪声 sigma、从 0 开始的布朗运动。
- [cdf](https://zh.hilberon.com/library/cdf)

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

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

  两个等长列表的相关系数。

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