# affine_sde_moments function

The mean and variance at time t of dX = (a + b X) dt + sigma dW, started at x0.

## How to write it

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

Gives {mean: ..., variance: ...}, with the case b = 0 apart; a note says when it holds.

**a**

The constant part of the drift. Takes an expression.

**b**

The rate: the drift is a + b X. Takes an expression.

**sigma**

The noise strength. Takes an expression.

**x0**

The starting value, which does not depend on time. Takes an expression.

**time**

The time, a variable. Takes a variable, or a list of variables.

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

The same for up to six states; the answer has the mean and the covariance.

**a**

The constant part of the drift of each state, in brackets. Takes a list.

**b**

The matrix of the drift, one row for each state. Takes a matrix, written as a list of rows.

**sigma**

The noise: one row for each state, one column for each driver. Takes a matrix, written as a list of rows.

**x0**

The starting value of each state. Takes a list.

**time**

The time, a variable. Takes a variable, or a list of variables.

## Examples

- $\displaystyle (E[X_{t}], \operatorname{Var}(X_{t})) \text{ where } \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{if } b \neq 0 \\ x_{0}+a t & \text{otherwise} \end{cases} \\ \operatorname{Var}(X_{t}) &= \begin{cases} \frac{\sigma^{2} (e^{2 b t}-1)}{2 b} & \text{if } b \neq 0 \\ \sigma^{2} t & \text{otherwise} \end{cases} \end{aligned}$

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

  In the solver, `affine_sde_moments(a, b, sigma, x0, t)` gives `{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)}`.

  [Open in the solver](https://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{ where } \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)`

  In the solver, `affine_sde_moments(1, 2, 3, 0, t)` gives `{mean: (e^(2t) - 1)/2, variance: 9(e^(4t) - 1)/4}`.

  [Open in the solver](https://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)`

  In the solver, `affine_sde_moments([0, 0], [[-1, 2], [0, -3]], [[1, 0], [0, 1]], [x0, y0], t)` gives `{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]]}`.

  [Open in the solver](https://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)

## Related functions

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

  The path of dX = mu X dt + sigma X dW, as a function of the Wiener process's value w at time t.
- [binomial_distribution](https://hilberon.com/library/binomial_distribution)

  The number of successes in n independent trials that each succeed with chance p.
- [wiener_process](https://hilberon.com/library/wiener_process)

  Brownian motion with drift mu and noise sigma, started at 0.
- [cdf](https://hilberon.com/library/cdf)

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

  The lifetime of a unit whose chance of surviving to time x is e^(-(x/scale)^shape).
- [correlation](https://hilberon.com/library/correlation)

  The correlation coefficient of two lists of the same length.

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