# expectation function

The expected value of an expression of a variable with a distribution or a process.

## How to write it

### expectation(f, distribution)

The expected value of the expression.

**f**

The expression of the variable. Takes an expression.

**distribution**

The variable and its law, written distributed(x, d). Takes an expression.

## Examples

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

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

  In the solver, `expectation(x^2, distributed(x, normal_distribution(0, 1)))` gives `1`.

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

  In the solver, `expectation(exp(t x), distributed(x, normal_distribution(mu, sigma)))` gives `e^(sigma^2*t^2/2 + mu*t)`.

  [Open in the solver](https://hilberon.com/?q=expectation%28exp%28t%20x%29%2C%20distributed%28x%2C%20normal_distribution%28mu%2C%20sigma%29%29%29)

## Functions its examples use

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

  A variable and the distribution it follows, as probability and expectation read it.
- [normal_distribution](https://hilberon.com/library/normal_distribution)

  The normal distribution with mean mu and standard deviation sigma.
- [exp](https://hilberon.com/library/exp)

  Exponential function; exp(x) is the power e^x.

## Related functions

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

  The waiting time for events that come at rate lambda.
- [fit](https://hilberon.com/library/fit)

  The least-squares fit of a combination of functions through points.
- [cvar](https://hilberon.com/library/cvar)

  The conditional value at risk of a loss: its expected value over the worst share 1 - level.
- [geometric_brownian_motion_process](https://hilberon.com/library/geometric_brownian_motion_process)

  A process that grows at rate mu with relative noise sigma, started at x0.
- [covariance](https://hilberon.com/library/covariance)

  The sample covariance of two lists of the same length.
- [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.

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