---
title: "Expectation"
language: "en"
type: "Symbol"
summary: "Expectation[expr, x \\[Distributed] dist] gives the expectation of expr under the assumption that x follows the probability distribution dist. Expectation[expr, x \\[Distributed] data] gives the expectation of expr under the assumption that x follows the probability distribution given by data. Expectation[expr, {x1, x2, ...} \\[Distributed] dist] gives the expectation of expr under the assumption that {x1, x2, ...} follows the multivariate distribution dist. Expectation[expr, {x1 \\[Distributed] dist1, x2 \\[Distributed] dist2, ...}] gives the expectation of expr under the assumption that x1, x2, ... are independent and follow the distributions dist1, dist2, .... Expectation[expr \\[Conditioned] pred, ...] gives the conditional expectation of expr given pred."
keywords: 
- expected value
- mean
- average
- average value
- central value
- population mean
- mathematical expectation
- first moment
- EV
- e.v.
- conditional expectation
- conditional expected value
- marginal expectation
- marginal expected value
- logarithmic expectation
- Shannon information
- Fisher information
- mutual information
canonical_url: "https://reference.wolfram.com/language/ref/Expectation.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Random Variables"
    link: "https://reference.wolfram.com/language/guide/RandomVariables.en.md"
  - 
    title: "Probability & Statistics"
    link: "https://reference.wolfram.com/language/guide/ProbabilityAndStatistics.en.md"
  - 
    title: "Actuarial Computation"
    link: "https://reference.wolfram.com/language/guide/ActuarialComputation.en.md"
  - 
    title: "Probability & Statistics with Quantities"
    link: "https://reference.wolfram.com/language/guide/ProbabilityWithQuantities.en.md"
  - 
    title: "Finite Mathematics"
    link: "https://reference.wolfram.com/language/guide/FiniteMathematics.en.md"
  - 
    title: "Numerical Data"
    link: "https://reference.wolfram.com/language/guide/NumericalData.en.md"
  - 
    title: "Descriptive Statistics"
    link: "https://reference.wolfram.com/language/guide/DescriptiveStatistics.en.md"
  - 
    title: "Reliability"
    link: "https://reference.wolfram.com/language/guide/Reliability.en.md"
  - 
    title: "Solvers over Regions"
    link: "https://reference.wolfram.com/language/guide/GeometricSolvers.en.md"
  - 
    title: "Random Graphs"
    link: "https://reference.wolfram.com/language/guide/RandomGraphs.en.md"
related_functions: 
  - 
    title: "NExpectation"
    link: "https://reference.wolfram.com/language/ref/NExpectation.en.md"
  - 
    title: "AsymptoticExpectation"
    link: "https://reference.wolfram.com/language/ref/AsymptoticExpectation.en.md"
  - 
    title: "Distributed"
    link: "https://reference.wolfram.com/language/ref/Distributed.en.md"
  - 
    title: "Conditioned"
    link: "https://reference.wolfram.com/language/ref/Conditioned.en.md"
  - 
    title: "Probability"
    link: "https://reference.wolfram.com/language/ref/Probability.en.md"
  - 
    title: "NProbability"
    link: "https://reference.wolfram.com/language/ref/NProbability.en.md"
  - 
    title: "Mean"
    link: "https://reference.wolfram.com/language/ref/Mean.en.md"
  - 
    title: "Moment"
    link: "https://reference.wolfram.com/language/ref/Moment.en.md"
  - 
    title: "CentralMoment"
    link: "https://reference.wolfram.com/language/ref/CentralMoment.en.md"
  - 
    title: "FactorialMoment"
    link: "https://reference.wolfram.com/language/ref/FactorialMoment.en.md"
  - 
    title: "Sum"
    link: "https://reference.wolfram.com/language/ref/Sum.en.md"
  - 
    title: "Integrate"
    link: "https://reference.wolfram.com/language/ref/Integrate.en.md"
---
# Expectation

Expectation[expr, x\[Distributed]dist] gives the expectation of expr under the assumption that x follows the probability distribution dist. 

Expectation[expr, x\[Distributed]data] gives the expectation of expr under the assumption that x follows the probability distribution given by data.

Expectation[expr, {x1, x2, …}\[Distributed]dist] gives the expectation of expr under the assumption that {x1, x2, …} follows the multivariate distribution dist. 

Expectation[expr, {x1\[Distributed]dist1, x2\[Distributed]dist2, …}] gives the expectation of expr under the assumption that x1, x2, … are independent and follow the distributions dist1, dist2, …. 

Expectation[expr\[Conditioned]pred, …] gives the conditional expectation of expr given pred.

## Details and Options

* ``Expectation`` is also known as expected value.

* ``x\[Distributed]dist`` can be entered as ``x`` esc`` dist ``esc ``dist`` or ``x`` [`\[Distributed]`](https://reference.wolfram.com/language/ref/character/Distributed.en.md)``dist``.

* ``expr\[Conditioned]pred`` can be entered as ``expr`` esc`` cond ``esc ``pred`` or  ``expr`` [`\[Conditioned]`](https://reference.wolfram.com/language/ref/character/Conditioned.en.md)``pred``.

* For a continuous distribution ``dist``, the expectation of ``expr`` is given by $\int  \text{\textit{expr}} f(x) dx$ where $f(x)$ is the probability density function of ``dist`` and the integral is taken over the domain of ``dist``.

* For a discrete distribution ``dist``, the expectation of ``expr`` is given by $\sum \text{\textit{expr}} f(x)$ where $f(x)$ is the probability density function of ``dist`` and the summation is taken over the domain of ``dist``.

* For a dataset ``data``, the expectation of ``expr`` is given by ``Sum[expr, {x, data}] / Length[data]``.

* Univariate data is given as a list of values ``{v1, v2, …}`` and multivariate data is given as a list of vectors ``{{v11, …, v1m}, {v21, …, v2m}, …}``.

* ``Expectation[expr, {x1\[Distributed]dist1, x2\[Distributed]dist2}]`` corresponds to ``Expectation[Expectation[expr, x2\[Distributed]dist2], x1\[Distributed]dist1]`` so that the last variable is summed or integrated first.

* ``N[Expectation[…]]`` calls ``NExpectation`` for expectations that cannot be done symbolically. »

* The following options can be given:

|                    |               |                                              |
| ------------------ | ------------- | -------------------------------------------- |
| Assumptions        | \$Assumptions | assumptions to make about parameters  »      |
| GenerateConditions | False         | whether to generate conditions on parameters |
| Method             | Automatic     | what method to use  »                        |
| TargetUnits        | Automatic     | units to display in the output  »            |

---

## Background & Context

``Expectation[expr, x\[Distributed]\[ScriptCapitalD]]`` gives the expectation for an expression ``expr`` involving a random variable ``x`` under the assumption that ``x`` either follows an indicated probability distribution (i.e. ``\[ScriptCapitalD]`` is a distribution such as ``NormalDistribution``, ``BinomialDistribution``, ``ChiSquareDistribution``, etc.) or is taken from a given dataset (i.e. ``\[ScriptCapitalD]`` defines a dataset), and where ``\[Distributed]`` is a shorthand for ``Distributed``. The output of ``Expectation`` may be a number or symbolic expression involving the input parameters.

``Expectation`` also works with multivariate distributions, with nonparametric distributions such as ``EmpiricalDistribution``, ``HistogramDistribution``, and ``KernelMixtureDistribution``, and with derived distributions such as ``TransformedDistribution`` and ``ProductDistribution``. ``Expectation`` may also be applied to random processes, including those defined by ``ContinuousMarkovProcess``, ``DiscreteMarkovProcess``, ``WienerProcess``, and ``PoissonProcess``.

Expressions passed to ``Expectation`` may include linear and nonlinear expressions as well as functions of both real and complex variables. ``Expectation`` can compute conditional expectations using an expression of the form ``expr\[Conditioned]pred``, where ``\[Conditioned]`` is a shorthand for ``Conditioned``. A number of options may be passed to ``Expectation`` including ``Assumptions``, ``GenerateConditions``,  and ``Method``. The output provided by ``Expectation`` is calculated via exact methods, including symbolic integration and summation. Corresponding results involving numerical methods can be obtained via ``NExpectation``. 

By definition, the expectation ``Expectation[x, x\[Distributed]\[ScriptCapitalD]]`` of a random variable ``x`` distributed according to a distribution ``\[ScriptCapitalD]`` is the same as the first moment ``Moment[\[ScriptCapitalD], 1]`` and hence also as the mean ``Mean[\[ScriptCapitalD]]``. In addition, pairing ``Expectation`` with a ``Boole`` construct allows results obtained by ``Expectation`` to also be obtained by ``Probability``. For example, ``Expectation[Boole[1 < x < 3], x\[Distributed]NormalDistribution[]]`` is equivalent to ``Probability[1 < x < 3, x\[Distributed]NormalDistribution[]]``.

---

## Examples (73)

### Basic Examples (3)

Compute the expectation of a polynomial expression:

```wl
In[1]:= Expectation[2x + 3, x\[Distributed]NormalDistribution[]]

Out[1]= 3

In[2]:= Expectation[x ^ 2 + 7x + 8, x\[Distributed]PoissonDistribution[μ]]

Out[2]= 8 + 8 μ + μ^2

In[3]:= Expectation[x ^ 2 + 3y ^ 2 + 11, {x, y}\[Distributed]DirichletDistribution[{1, 4, 5}]]

Out[3]= (636/55)

In[4]:= Expectation[x ^ 3 + 4y - a z, {x, y, z}\[Distributed]MultinomialDistribution[10, {1 / 2, 1 / 3, 1 / 6}]]

Out[4]= -(5/6) (-211 + 2 a)
```

---

Compute the expectation of an arbitrary expression:

```wl
In[1]:= Expectation[(x - 1)^2 + E ^ (-x), x\[Distributed]NormalDistribution[]]

Out[1]= 2 + Sqrt[E]

In[2]:= Expectation[Abs[2x - 1], x\[Distributed]ExponentialDistribution[λ]]

Out[2]= (E^-λ / 2 (4 - 2 E^λ / 2 + E^λ / 2 λ)/λ)

In[3]:= Expectation[x ^ 2 + E ^ (x + y), {x, y}\[Distributed]DirichletDistribution[{2, 3, 4}]]

Out[3]= -(31852799/15) + 781200 E
```

---

Compute a conditional expectation:

```wl
In[1]:= Expectation[x^2 + 1\[Conditioned]x > 1 / 2, x\[Distributed]LaplaceDistribution[0, 1 / 2]]

Out[1]= (9/4)

In[2]:= Expectation[x + y^2 - (3/10)\[Conditioned](1 / 2 < x < 3 / 4), {x, y}\[Distributed]DirichletDistribution[{1, 3, 4}]]

Out[2]= (4613/15240)
```

### Scope (31)

#### Basic Uses (9)

Compute the expectation for an expression in a continuous univariate distribution:

```wl
In[1]:= Expectation[3x ^ 2 + 5, x\[Distributed]NormalDistribution[]]

Out[1]= 8
```

Discrete univariate distribution:

```wl
In[2]:= Expectation[2x + 7, x\[Distributed]PoissonDistribution[1]]

Out[2]= 9
```

Continuous multivariate distribution:

```wl
In[3]:= Expectation[5x + 3y ^ 2 + 11, {x, y}\[Distributed]BinormalDistribution[1 / 2]]

Out[3]= 14
```

Discrete multivariate distribution:

```wl
In[4]:= Expectation[3x ^ 2 + 8x y + 1, {x, y}\[Distributed]MultivariatePoissonDistribution[2, {2, 3}]]

Out[4]= 237
```

---

Find the expectation of an expression in a distribution specified by a list:

```wl
In[1]:= r = RandomVariate[NormalDistribution[], 1000];

In[2]:= Expectation[x ^ 2 + 3x + 11, x\[Distributed]r]

Out[2]= 11.9411
```

---

Compute the expectation using independently distributed random variables:

```wl
In[1]:= Expectation[2x + 3y ^ 2 + a, {x\[Distributed]ExponentialDistribution[1], y\[Distributed]NormalDistribution[]}]

Out[1]= 5 + a
```

---

Find the conditional expectation with general nonzero probability conditioning:

```wl
In[1]:= Expectation[x + 2\[Conditioned]1 / 2 < x ^ 2 < 5, x\[Distributed]ExponentialDistribution[λ]]

Out[1]= (-E^Sqrt[5] λ (2 + (4 + Sqrt[2]) λ) + 2 E^(λ/Sqrt[2]) (1 + (2 + Sqrt[5]) λ)/2 (E^(λ/Sqrt[2]) - E^Sqrt[5] λ) λ)

In[2]:= Plot[%, {λ, 0, 5}]

Out[2]= [image]
```

Discrete univariate distribution:

```wl
In[3]:= Expectation[x ^ 2 + 12\[Conditioned]x > 2, x\[Distributed]PoissonDistribution[λ]]

Out[3]= -(2 (-12 - 13 λ - 8 λ^2 + E^λ (12 + λ + λ^2))/2 - 2 E^λ + 2 λ + λ^2)

In[4]:= Plot[%, {λ, 0, 10}]

Out[4]= [image]
```

Multivariate continuous distribution:

```wl
In[5]:= Expectation[(x + y)\[Conditioned](y > 1 / 3), {x, y}\[Distributed]DirichletDistribution[{1, 3, 4}]]

Out[5]= (23/39)
```

Multivariate discrete distribution:

```wl
In[6]:= Expectation[x + y + 5\[Conditioned]z > 2, {x, y, z}\[Distributed]MultinomialDistribution[10, {1 / 3, 1 / 2, 1 / 6}]]

Out[6]= (39444545/3397794)
```

---

Compute the conditional expectation with a zero-probability conditioning event:

```wl
In[1]:= Expectation[x + y ^ 2\[Conditioned](x == 1 / 7), {x, y}\[Distributed]ProductDistribution[{ExponentialDistribution[3], 2}]]

Out[1]= (23/63)
```

---

Apply ``N[Expectation[…]]`` to invoke ``NExpectation`` if symbolic evaluation fails:

```wl
In[1]:= f[x_ ? NumericQ] := x ^ 2 + 3x ^ 4

In[2]:= Expectation[f[x], x\[Distributed]NormalDistribution[]]

Out[2]= Expectation[f[x], x\[Distributed]NormalDistribution[0, 1]]

In[3]:= N[%]

Out[3]= 10.

In[4]:= NExpectation[f[x], x\[Distributed]NormalDistribution[]]

Out[4]= 10.
```

---

With no ``Assumptions``, conditions are generated:

```wl
In[1]:= Expectation[UnitStep[x - a], x\[Distributed]UniformDistribution[{2, 8}]]

Out[1]= Piecewise[{{1, a <= 2}, {(8 - a)/6, 2 < a < 8}}, 0]
```

With ``Assumptions``, a result valid under the given assumptions is returned:

```wl
In[2]:= Expectation[UnitStep[x - a], x\[Distributed]UniformDistribution[{2, 8}], Assumptions -> 3 < a < 4]

Out[2]= (8 - a/6)

In[3]:= Assuming[3 < a < 4, Expectation[UnitStep[x - a], x\[Distributed]UniformDistribution[{2, 8}]]]

Out[3]= (8 - a/6)
```

---

Find the expectation of a rational function:

```wl
In[1]:= Expectation[(x + 3) / (x + 5), x\[Distributed]ExponentialDistribution[2]]

Out[1]= 1 + 4 E^10 ExpIntegralEi[-10]
```

Transcendental function:

```wl
In[2]:= Expectation[E ^ (-3x ^ 2) + 1, x\[Distributed]ExponentialDistribution[2]]

Out[2]= 1 + E^1 / 3 Sqrt[(π/3)] Erfc[(1/Sqrt[3])]
```

Piecewise function:

```wl
In[3]:= Expectation[Piecewise[{{5, x < 1}, {x ^ 2 + 3, x ≥ 1}}], x\[Distributed]ExponentialDistribution[2]]

Out[3]= (1 + 10 E^2/2 E^2)
```

Complex function:

```wl
In[4]:= Expectation[E ^ (I x), x\[Distributed]UniformDistribution[{3, 4}]]

Out[4]= -I (-1 + E^I) E^3 I
```

---

Compute an expectation for a time slice of a Poisson process:

```wl
In[1]:= Expectation[x[t] ^ 2 + x[t] + 3E ^ (-x[t]), x\[Distributed]PoissonProcess[μ]]

Out[1]= 3 E^(-1 + (1/E)) t μ + t μ (2 + t μ)
```

#### Quantity Uses (5)

Find expectations of quantity expressions:

```wl
In[1]:= Expectation[Quantity[x, "Meters"] * Quantity[3.5, "Feet"], x\[Distributed]LogNormalDistribution[2, 3]]

Out[1]= Quantity[709.5730941317252, "Meters"^2]

In[2]:= Expectation[Quantity[v, "Meters" / "Seconds"] Quantity[t, "Minutes"], {v\[Distributed]MaxwellDistribution[σ], t\[Distributed]GammaDistribution[a, b]}]

Out[2]= Quantity[120*a*b*Sqrt[2/Pi]*σ, "Meters"]
```

---

Find expectations specified using ``QuantityDistribution`` :

```wl
In[1]:= freq\[ScriptCapitalD] = UniformDistribution[{Quantity[2, "Hertz"], Quantity[5, "Hertz"]}]

Out[1]= QuantityDistribution[UniformDistribution[{2, 5}], "Hertz"]

In[2]:= dur\[ScriptCapitalD] = UniformDistribution[{Quantity[.5, "Seconds"], Quantity[.9, "Seconds"]}]

Out[2]= QuantityDistribution[UniformDistribution[{0.5, 0.9}], "Seconds"]

In[3]:= ampl\[ScriptCapitalD] = RayleighDistribution[Quantity[5, "Volts"]]

Out[3]= QuantityDistribution[RayleighDistribution[5], "Volts"]

In[4]:= Expectation[a Sin[ω t], {ω\[Distributed]freq\[ScriptCapitalD], t\[Distributed]dur\[ScriptCapitalD], a\[Distributed]ampl\[ScriptCapitalD]}]

Out[4]= Quantity[3.126859627736455, "Volts"]
```

---

Find conditional expectations:

```wl
In[1]:= Expectation[t1 - t2\[Conditioned]t1 > t2, {t1, t2}\[Distributed]CopulaDistribution[{"FGM", α}, {UniformDistribution[{Quantity[MixedMagnitude[{1, 15}], MixedUnit[{"Hours", "Minutes"}]], Quantity[2, "Hours"]}], UniformDistribution[{Quantity[80, "Minutes"], Quantity[MixedMagnitude[{1, 45}], MixedUnit[{"Hours", "Minutes"}]]}]}]]

Out[1]= Quantity[(4365 - 450*α)/(297 + 10*α), "Minutes"]
```

---

Calculate expectation with ``QuantityMagnitude`` :

```wl
In[1]:= \[ScriptCapitalD] = QuantityDistribution[NormalDistribution[0, .3], "Yards"];

In[2]:= Expectation[QuantityMagnitude[x, "Meters"] ^ 2, x\[Distributed]\[ScriptCapitalD]]

Out[2]= 0.0752515
```

Equivalent calculation:

```wl
In[3]:= Expectation[x ^ 2, Quantity[x, "Meters"]\[Distributed]\[ScriptCapitalD]]

Out[3]= 0.0752515
```

---

Calculate expectation with distribution given by ``Quantity`` data:

```wl
In[1]:= Expectation[x, x\[Distributed]Quantity[RandomReal[1, 100], "Meters"]]

Out[1]= Quantity[0.5015325721513135, "Meters"]
```

Distribution given by ``QuantityArray`` :

```wl
In[2]:= Expectation[x ^ 2, x\[Distributed]QuantityArray[RandomReal[1, 100], "Yards"]]

Out[2]= Quantity[0.2908378724343213, "Yards"^2]
```

#### Parametric Distributions (4)

Compute expectations for univariate continuous distributions:

```wl
In[1]:= Expectation[x + 1 / 2, x\[Distributed]BetaDistribution[α, β]]

Out[1]= (3 α + β/2 (α + β))

In[2]:= Expectation[x ^ 2 + 7E ^ x, x\[Distributed]UniformDistribution[{-2, 11}]]

Out[2]= (103/3) - (7/13 E^2) + (7 E^11/13)

In[3]:= Expectation[(x - 3) ^ 2 + 5UnitStep[x ^ 2 - 1], x\[Distributed]NormalDistribution[0, 1]]

Out[3]= 5 (2 + Erfc[(1/Sqrt[2])])

In[4]:= Expectation[E ^ (-x) + 3x, x\[Distributed]ChiSquareDistribution[ν]]

Out[4]= 3^-ν / 2 + 3 ν
```

---

Compute expectations for univariate discrete distributions:

```wl
In[1]:= Expectation[x ^ 2 + 9, x\[Distributed]BinomialDistribution[10, p]]

Out[1]= 9 + 10 p + 90 p^2

In[2]:= Expectation[x + 10E ^ x, x\[Distributed]DiscreteUniformDistribution[{5, max}]]

Out[2]= -(-20 + 20 E^5 - 20 E^1 + max + max + max^2 - E (-20 + max + max^2)/2 (-1 + E) (-4 + max))

In[3]:= Expectation[x ^ 3 + 12, x\[Distributed]ZipfDistribution[4]]

Out[3]= (π^2 + 72 Zeta[5]/6 Zeta[5])

In[4]:= Expectation[E ^ (2x) + 3, x\[Distributed]PoissonDistribution[λ]]

Out[4]= 3 + E^(-1 + E^2) λ
```

---

Expectations for multivariate continuous distributions:

```wl
In[1]:= Expectation[x + y ^ 2 + 7, {x\[Distributed]UniformDistribution[{0, 1}], y\[Distributed]LaplaceDistribution[0, 1]}]

Out[1]= (19/2)

In[2]:= Expectation[x ^ 6 + 3y + 1, {x\[Distributed]NormalDistribution[0, 1], y\[Distributed]ExponentialDistribution[m]}]

Out[2]= (3 + 16 m/m)

In[3]:= Expectation[2x ^ 2 + 3y ^ 2 + z, {x, y, z}\[Distributed]DirichletDistribution[{2, 3, 4, 7}]]

Out[3]= (29/68)
```

---

Expectations for multivariate discrete distributions:

```wl
In[1]:= Expectation[x + y ^ 2, {x, y}\[Distributed]ProductDistribution[DiscreteUniformDistribution[{2, 5}], BernoulliDistribution[p]]]

Out[1]= (1/2) (7 + 2 p)

In[2]:= Expectation[x y ^ 2, {x\[Distributed]GeometricDistribution[1 / 3], y\[Distributed]PoissonDistribution[λ]}]

Out[2]= 2 (λ + λ^2)

In[3]:= Expectation[2x + 3y + Abs[z - 2], {x, y, z}\[Distributed]MultinomialDistribution[10, {1 / 2, 1 / 3, 1 / 6}]]

Out[3]= (158411641/7558272)
```

#### Nonparametric Distributions (4)

Compute an expectation using a univariate ``EmpiricalDistribution`` :

```wl
In[1]:= \[ScriptCapitalD] = EmpiricalDistribution[RandomVariate[NormalDistribution[], 100]];

In[2]:= Expectation[x^2 + 1, x\[Distributed]\[ScriptCapitalD]]

Out[2]= 1.96046
```

Using a multivariate empirical distribution:

```wl
In[3]:= \[ScriptCapitalD] = EmpiricalDistribution[RandomVariate[BinormalDistribution[1 / 3], 100]];

In[4]:= Expectation[x + y^2 + 4, {x, y}\[Distributed]\[ScriptCapitalD]]

Out[4]= 5.23065
```

---

Using a univariate ``HistogramDistribution``:

```wl
In[1]:= \[ScriptCapitalD] = HistogramDistribution[RandomVariate[ExponentialDistribution[2], 100]];

In[2]:= Expectation[x ^ 2 + x + 3, x\[Distributed]\[ScriptCapitalD]]

Out[2]= 4.23833
```

A multivariate histogram distribution:

```wl
In[3]:= \[ScriptCapitalD] = HistogramDistribution[RandomVariate[BinormalDistribution[1 / 3], 100]];

In[4]:= Expectation[x + y^2 + 1, {x, y}\[Distributed]\[ScriptCapitalD]]

Out[4]= 2.16833
```

---

Using a univariate ``KernelMixtureDistribution``:

```wl
In[1]:= \[ScriptCapitalD] = KernelMixtureDistribution[RandomVariate[NormalDistribution[], 30]];

In[2]:= Expectation[x ^ 4 + 2x ^ 2 + 7, x\[Distributed]\[ScriptCapitalD]]

Out[2]= 16.9687
```

---

Using censored data with ``SurvivalDistribution``:

```wl
In[1]:= eventTimes = {10, 7, 23, 22, 6, 25, 20, 19, 6, 17, 6, 13};

In[2]:= censorIndicators = {0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0};

In[3]:= \[ScriptCapitalD] = SurvivalDistribution[EventData[eventTimes, censorIndicators]];

In[4]:= Expectation[x + 25, x\[Distributed]\[ScriptCapitalD]]

Out[4]= 40.6667
```

#### Derived Distributions (9)

Compute the expectation using a ``TransformedDistribution``:

```wl
In[1]:= Expectation[x + 2, x\[Distributed]TransformedDistribution[y ^ 2, y\[Distributed]NormalDistribution[0, 1]]]

Out[1]= 3
```

An equivalent way of formulating the same expectation:

```wl
In[2]:= Expectation[y ^ 2 + 2, y\[Distributed]NormalDistribution[0, 1]]

Out[2]= 3
```

---

Find the expectation using a ``ProductDistribution``:

```wl
In[1]:= Expectation[x + y + 4, {x, y}\[Distributed]ProductDistribution[ExponentialDistribution[m], TriangularDistribution[{3, 7}]]]

Out[1]= (1 + 9 m/m)
```

An equivalent formulation for the same expectation:

```wl
In[2]:= Expectation[x + y + 4, {x\[Distributed]ExponentialDistribution[m], y\[Distributed]TriangularDistribution[{3, 7}]}]//Together

Out[2]= (1 + 9 m/m)
```

---

Using a component mixture of normal distributions:

```wl
In[1]:= Expectation[E ^ x + 2, x\[Distributed]MixtureDistribution[{1, 2}, {NormalDistribution[2, 3], NormalDistribution[4, 5]}]]

Out[1]= (1/3) (6 + E^13 / 2 + 2 E^33 / 2)
```

---

Parameter mixture of exponential distributions:

```wl
In[1]:=
Expectation[2x + 3, x \[Distributed] ParameterMixtureDistribution[
   ExponentialDistribution[λ], λ \[Distributed] UniformDistribution[{1, 2}]]]

Out[1]= 3 + 2 Log[2]
```

---

Truncated Dirichlet distribution:

```wl
In[1]:= Expectation[x ^ 2 + y + 4, {x, y}\[Distributed]TruncatedDistribution[{{1 / 3, 1 / 2}, {1 / 6, 5 / 6}}, DirichletDistribution[{2, 4, 7}]]]

Out[1]= (191390016719/43388302230)
```

---

Censored triangular distribution:

```wl
In[1]:= Expectation[UnitBox[x ^ 2 - 35], x\[Distributed]CensoredDistribution[{5, 30}, TriangularDistribution[{1, 50}]]]

Out[1]= (2 (1 + Sqrt[138] - Sqrt[142])/2401)
```

---

Marginal distribution:

```wl
In[1]:= Expectation[x^2 + 13 x + 6E ^ x, x\[Distributed]MarginalDistribution[DirichletDistribution[{1, 2, 3}], 1]]

Out[1]= -(27269/14) + 720 E
```

An equivalent way of formulating the same expectation:

```wl
In[2]:= Expectation[x^2 + 13 x + 6E ^ x, {x, y}\[Distributed]DirichletDistribution[{1, 2, 3}]]

Out[2]= -(27269/14) + 720 E
```

---

Copula distribution:

```wl
In[1]:= Expectation[x + y ^ 2 + 1, {x, y}\[Distributed]CopulaDistribution[{"AMH", 1 / 5}, {UniformDistribution[{0, 1}], UniformDistribution[{0, 1}]}]]

Out[1]= (11/6)
```

---

Formula distribution:

```wl
In[1]:= Expectation[E ^ (-x ^ 2) + 2, x\[Distributed]ProbabilityDistribution[3 / 4(x ^ 2 + 2x), {x, 0, 1}]]

Out[1]= (1/16) (44 - (18/E) + 3 Sqrt[π] Erf[1])
```

### Generalizations & Extensions (2)

Use a pure function to compute an expectation for a list of values:

```wl
In[1]:= r = RandomVariate[NormalDistribution[], 1000];

In[2]:= Expectation[# ^ 2 + 3#&, r]

Out[2]= 1.08659
```

---

Compute an expectation for a mixture of continuous and discrete distributions:

```wl
In[1]:= Expectation[k ^ 2 + 3, k\[Distributed]MixtureDistribution[{1, 2}, {ExponentialDistribution[1 / 7], PoissonDistribution[2 / 7]}]]

Out[1]= (5279/147)
```

### Options (6)

#### Assumptions (1)

With no ``Assumptions``, conditions are generated:

```wl
In[1]:= Expectation[UnitStep[x - a], x\[Distributed]UniformDistribution[{2, m}]]

Out[1]= Piecewise[{{1, a <= 2 && m > 2}, {(-a + m)/(-2 + m), a > 2 && a - m < 0}}, 0]
```

With ``Assumptions``, a result valid under the given assumptions is returned:

```wl
In[2]:= Expectation[UnitStep[x - a], x\[Distributed]UniformDistribution[{2, m}], Assumptions -> 3 < a < 4 && m > a]

Out[2]= (-a + m/-2 + m)

In[3]:= Assuming[3 < a < 4 && m > a, Expectation[UnitStep[x - a], x\[Distributed]UniformDistribution[{2, m}]]]

Out[3]= (-a + m/-2 + m)
```

#### Method (4)

Compute the expectation of a polynomial function:

```wl
In[1]:= Expectation[x ^ 2 + x ^ 10, x\[Distributed]NormalDistribution[]]

Out[1]= 946
```

Obtain the same result using the moments of the distribution:

```wl
In[2]:= Expectation[x ^ 2 + x ^ 10, x\[Distributed]NormalDistribution[], Method -> "Moment"]//Timing

Out[2]= {0.004, 946}
```

The evaluation is slower using the definition of ``Expectation`` as an integral:

```wl
In[3]:= Expectation[x ^ 2 + x ^ 10, x\[Distributed]NormalDistribution[], Method -> "Integrate"]//Timing

Out[3]= {0.2, 946}
```

---

Compute the expectation of a transcendental function:

```wl
In[1]:= Expectation[E ^ (-x), x\[Distributed]PoissonDistribution[3]]

Out[1]= E^-3 + (3/E)
```

Here, the method based on moments fails because the expression is nonpolynomial:

```wl
In[2]:= Expectation[E ^ (-x), x\[Distributed]PoissonDistribution[3], Method -> "Moment"]

Out[2]= Expectation[E^-x, x\[Distributed]PoissonDistribution[3], Method -> "Moment"]
```

The result can be obtained using the definition of ``Expectation`` as a symbolic sum:

```wl
In[3]:= Expectation[E ^ (-x), x\[Distributed]PoissonDistribution[3], Method -> "Sum"]

Out[3]= E^-3 + (3/E)
```

---

Find the expectation of a function in a ``TukeyLambdaDistribution`` :

```wl
In[1]:= Expectation[UnitStep[2x + 1], x\[Distributed]TukeyLambdaDistribution[1 / 3]]

Out[1]= 1 - Root[-9938375 + 61167312 #1 - 121632192 #1^2 + 80621568 #1^3&, 1]
```

The PDF of this distribution is not available in closed form:

```wl
In[2]:= PDF[TukeyLambdaDistribution[1 / 3], x]

Out[2]= PDF[TukeyLambdaDistribution[(1/3)], x]
```

Hence a direct application of the definition fails:

```wl
In[3]:= Expectation[UnitStep[2x + 1], x\[Distributed]TukeyLambdaDistribution[1 / 3], Method -> "Integrate"]

Out[3]= Expectation[UnitStep[1 + 2 x], x\[Distributed]TukeyLambdaDistribution[(1/3)], Method -> "Integrate"]
```

The expectation can be computed using ``Quantile`` :

```wl
In[4]:= Expectation[UnitStep[2x + 1], x\[Distributed]TukeyLambdaDistribution[1 / 3], Method -> "Quantile"]

Out[4]= 1 - Root[-9938375 + 61167312*#1 - 121632192*#1^2 + 80621568*#1^3 & , 1, 0]

In[5]:= q = Quantile[TukeyLambdaDistribution[1 / 3], p]

Out[5]=
ConditionalExpression[Piecewise[{{3*(-(1 - p)^(1/3) + p^(1/3)), 0 < p < 1}, {-3, p <= 0}}, 3], 
 0 <= p <= 1]

In[6]:= Integrate[(UnitStep[2x + 1] /. {x -> q}), {p, 0, 1}]

Out[6]= 1 - Root[-9938375 + 61167312*#1 - 121632192*#1^2 + 80621568*#1^3 & , 1, 0]
```

---

Calculate the expectation of an expression:

```wl
In[1]:= Expectation[E ^ (2x + 1), x\[Distributed]NormalDistribution[]]

Out[1]= E^3
```

This example uses ``Integrate`` :

```wl
In[2]:= Expectation[E ^ (2x + 1), x\[Distributed]NormalDistribution[], Method -> "Trace"]

Out[2]= Inactive[Integrate][(E^1 + 2 x - (x^2/2)/Sqrt[2 π]), {x, -∞, ∞}, GenerateConditions -> False, Assumptions -> True]
```

Use ``Activate`` to evaluate the result:

```wl
In[3]:= Activate[%]

Out[3]= E^3
```

#### TargetUnits (1)

Create a distribution object with quantity:

```wl
In[1]:= \[ScriptCapitalD] = UniformDistribution[{1, 5} Quantity[1, "Seconds"]]

Out[1]= QuantityDistribution[UniformDistribution[{1, 5}], "Seconds"]
```

``Expectation`` uses the quantity provided in the distribution as default:

```wl
In[2]:= Expectation[x ^ 2, x\[Distributed]\[ScriptCapitalD]]

Out[2]= Quantity[31/3, "Seconds"^2]
```

Specify the target unit to ``"Hours"`` :

```wl
In[3]:= Expectation[x ^ 2, x\[Distributed]\[ScriptCapitalD], TargetUnits -> "Hours" ^ 2]

Out[3]= Quantity[31/38880000, "Hours"^2]
```

### Applications (20)

#### Distribution Properties (5)

Obtain the raw moments of a continuous distribution:

```wl
In[1]:= Expectation[x ^ n, x\[Distributed]ChiSquareDistribution[ν], Assumptions -> n > 0]

Out[1]= (2^n Gamma[n + (ν/2)]/Gamma[(ν/2)])

In[2]:= Moment[ChiSquareDistribution[ν], n]//FunctionExpand

Out[2]= (2^n Gamma[n + (ν/2)]/Gamma[(ν/2)])
```

---

Obtain the mean of a discrete distribution:

```wl
In[1]:= Expectation[x, x\[Distributed]PoissonDistribution[μ]]

Out[1]= μ

In[2]:= Mean[PoissonDistribution[μ]]

Out[2]= μ
```

---

Obtain the variance of a truncated distribution:

```wl
In[1]:= dist = TruncatedDistribution[{1, 3}, ExponentialDistribution[2]];

In[2]:= Expectation[(x - Mean[dist]) ^ 2, x\[Distributed]dist]

Out[2]= (1 - 18 E^4 + E^8/4 (-1 + E^4)^2)

In[3]:= Variance[dist]

Out[3]= (E^6 (-19 Sinh[2] + Sinh[6])/2 (-1 + E^4)^3)
```

---

Construct a mixture density, here a Poisson‐inverse Gaussian mixture:

```wl
In[1]:= Expectation[PDF[PoissonDistribution[μ], k], μ\[Distributed]InverseGaussianDistribution[m, λ]]

Out[1]=
Piecewise[{{(E^(λ/m)*Sqrt[2/Pi]*Sqrt[λ]*(λ/(2 + λ/m^2))^(k/2)*
     BesselK[-(1/2) + k, Sqrt[λ*(2*m^2 + λ)]/m])/(((m^2*λ)/(2*m^2 + λ))^(1/4)*k!), k >= 0}}, 0]
```

Obtain the same result directly using ``ParameterMixtureDistribution`` :

```wl
In[2]:= PDF[ParameterMixtureDistribution[PoissonDistribution[μ], μ\[Distributed]InverseGaussianDistribution[m, λ]], k]

Out[2]=
Piecewise[{{(E^(λ/m)*Sqrt[2/Pi]*Sqrt[λ]*((m^2*λ)/(2*m^2 + λ))^(-(1/4) + k/2)*
     BesselK[-(1/2) + k, Sqrt[λ*(2*m^2 + λ)]/m])/k!, k >= 0}}, 0]

In[3]:= FullSimplify[% - %%, m∈Reals && m > 0 && λ∈Reals && λ > 0]

Out[3]= 0
```

---

Verify Jensen inequality $\text{Mean} (u (X))\leq u(\text{Mean} (X))$ for a concave function $u=\log (x)$ and a lognormal distribution:

```wl
In[1]:= \[ScriptCapitalD] = LogNormalDistribution[μ, σ];

In[2]:= Expectation[Log[X], X\[Distributed]\[ScriptCapitalD]] ≤ Log[Expectation[X, X\[Distributed]\[ScriptCapitalD]]]

Out[2]= μ ≤ Log[E^μ + (σ^2/2)]

In[3]:= FullSimplify[%, σ > 0 && μ∈Reals]

Out[3]= True
```

#### Actuarial Science (5)

An insurance policy reimburses a loss up to a benefit limit of 10. The policy holder's loss $y$ follows a distribution with density function $2\left/y^3\right.$ for $y>1$ and 0 otherwise. Find the expected value of the benefit paid under the insurance policy:

```wl
In[1]:= \[ScriptCapitalD] = CensoredDistribution[{1, 10}, ProbabilityDistribution[2 / y ^ 3, {y, 1, Infinity}]];

In[2]:= Expectation[x, x\[Distributed]\[ScriptCapitalD]]//N

Out[2]= 1.9
```

---

An insurance company's monthly claims are modeled by a continuous, positive random variable $x$, whose probability density function is proportional to $1\left/(x+1)^4\right.$ where $0<x<\infty$. Determine the company's expected monthly claims:

```wl
In[1]:= claimDistribution = ProbabilityDistribution[3 / (1 + x) ^ 4, {x, 0, Infinity}];

In[2]:= Expectation[x, x\[Distributed]claimDistribution]

Out[2]= (1/2)
```

---

Claim amounts for wind damage to insured homes are independent random variables with common density function $3\left/x^4\right.$ for $x>1$ and 0 otherwise, where $x$ is the amount of a claim in thousands. Suppose three such claims will be made. Find the expected value of the largest of the three claims:

```wl
In[1]:= windClaims = ProductDistribution[{ProbabilityDistribution[3 / y ^ 4, {y, 1, Infinity}], 3}];

In[2]:= Expectation[1000Max[x, y, z], {x, y, z}\[Distributed]windClaims]

Out[2]= 2025
```

---

Let $x$ represent the age of an insured automobile involved in an accident. Let $y$ represent the length of time the owner has insured the automobile at the time of the accident. $x$ and $y$ have joint probability density function $\left.\left(10-x y^2\right)\right/64$ for $2\leq x\leq 10$ and $0\leq y\leq 1$, and 0 otherwise. Calculate the expected age of an insured automobile involved in an accident:

```wl
In[1]:= ageDistribution = ProbabilityDistribution[(10 - x y ^ 2) / 64, {x, 2, 10}, {y, 0, 1}];

In[2]:= Expectation[x, {x, y}\[Distributed]ageDistribution]

Out[2]= (52/9)

In[3]:= N[%, 2]

Out[3]= 5.8
```

---

Under an excess of loss reinsurance agreement, a claim is shared between the insurer and reinsurer only if the claim exceeds a fixed amount, called the retention level. Otherwise, the insurer pays the claim in full. Compute the expected value of the amounts $y$ and $z$, paid by the insurer and the reinsurer for a retention level of $m$ if the claims follow a lognormal distribution with parameters $\mu$ and $\sigma$. Find the expected insurer claim payouts:

```wl
In[1]:= insurerPayouts = TransformedDistribution[Min[x, m], x\[Distributed]LogNormalDistribution[μ, σ], Assumptions -> m > 0];

In[2]:= Expectation[y, y\[Distributed]insurerPayouts]

Out[2]= (1/2) (m + m Erf[(μ - Log[m]/Sqrt[2] σ)] + E^μ + (σ^2/2) Erfc[(μ + σ^2 - Log[m]/Sqrt[2] σ)])
```

Find the expected reinsurer payouts to the insurer:

```wl
In[3]:= reinsurerPayouts = TransformedDistribution[Max[0, x - m], x\[Distributed]LogNormalDistribution[μ, σ], Assumptions -> m > 0];

In[4]:= Expectation[z, z\[Distributed]reinsurerPayouts]

Out[4]= (1/2) (E^μ + (σ^2/2) - m - m Erf[(μ - Log[m]/Sqrt[2] σ)] + E^μ + (σ^2/2) Erf[(μ + σ^2 - Log[m]/Sqrt[2] σ)])
```

#### Finance (2)

Compute the expected time value of a death benefit of \$1 paid at time $t$, where $t$ is drawn from a Gompertz–Makeham distribution:

```wl
In[1]:= tv = TimeValue[Cashflow[{{t, 1}}], EffectiveInterest[r, 0], 0]

Out[1]= (E^r)^-t

In[2]:= nsp = Expectation[tv, t\[Distributed]GompertzMakehamDistribution[a, b]]

Out[2]= b E^b ExpIntegralE[(r/a), b]
```

Find the annual premium, which is usually paid at the beginning of a policy year, that is necessary to make the expected time value of that payment stream for $t$ periods (where $t$ is drawn from a Gompertz–Makeham distribution) equal to the net single premium:

```wl
In[3]:= nap = TimeValue[AnnuityDue[premium, t], EffectiveInterest[r, 0], 0]

Out[3]= ((E^r)^1 - t (-1 + (E^r)^t) premium/-1 + E^r)

In[4]:= etv = Expectation[nap, t\[Distributed]GompertzMakehamDistribution[a, b]]

Out[4]= -(E^r premium (-1 + b E^b ExpIntegralE[(r/a), b])/-1 + E^r)
```

The resulting net annual premium:

```wl
In[5]:= Solve[etv == nsp, premium]//FullSimplify

Out[5]= {{premium -> -(b E^b - r (-1 + E^r) ExpIntegralE[(r/a), b]/-1 + b E^b ExpIntegralE[(r/a), b])}}
```

---

The fractional change of stock price $S_t/S_0$ at time $t$ (in years) is assumed lognormally distributed with parameters $\mu  t$ and $\sigma  \sqrt{t}$ :

```wl
In[1]:= f\[ScriptCapitalD][μ_, σ_, t_] = LogNormalDistribution[μ t, σ Sqrt[t]];
```

Compute expected stock price at epoch $t$ :

```wl
In[2]:= PriceAtEpoch[S0_, μ_, σ_, t_] = S0 Mean[f\[ScriptCapitalD][μ, σ, t]]

Out[2]= E^t μ + (t σ^2/2) S0
```

Assuming an investor can invest money in a stock with dividend yield $q$ for a year at a continuously compounded yearly rate $r$ risk-free, the risk-neutral pricing condition requires:

```wl
In[3]:= PriceAtEpoch[Subscript[S, 0], μ, σ, t + 1] == PriceAtEpoch[Subscript[S, 0], μ, σ, t] Exp[r - q]

Out[3]= E^(1 + t) μ + (1/2) (1 + t) σ^2 Subscript[S, 0] == E^-q + r + t μ + (t σ^2/2) Subscript[S, 0]
```

Solve for parameter $\mu$ :

```wl
In[4]:= {sol} = Solve[ForAll[{t, Subscript[S, 0]}, %], μ, Reals]

Out[4]= {{μ -> (1/2) (-2 q + 2 r - σ^2)}}
```

Consider a call option to buy this stock a year from now, at a fixed price $\mathcal{K}$. The value of such an option is:

```wl
In[5]:= CallOptVal[s_, \[ScriptCapitalK]_] = Max[s - \[ScriptCapitalK], 0];
```

Similarly, consider a put option to sell this stock a year from now, at a fixed price $\mathcal{K}$. The value of such an option is:

```wl
In[6]:= PutOptVal[s_, \[ScriptCapitalK]_] = Max[\[ScriptCapitalK] - s, 0];
```

The risk-neutral price of the call and put options are determined as the present value of their expected option values:

```wl
In[7]:= BlackScholesCallOptionPrice[S0_, \[ScriptCapitalK]_, r_, q_, σ_, t_] = Assuming[S0 > 0 && r > 0 && q > 0 && σ > 0 && t > 0 && \[ScriptCapitalK] > 0, Exp[-r t]Expectation[CallOptVal[\[ScriptF] S0, \[ScriptCapitalK]], \[ScriptF]\[Distributed]f\[ScriptCapitalD][μ /. sol, σ, t]]//FullSimplify]

Out[7]= (1/2) E^-r t (E^(-q + r) t S0 (1 + Erf[(t (-2 q + 2 r + σ^2) + 2 Log[S0] - 2 Log[\[ScriptCapitalK]]/2 Sqrt[2] Sqrt[t] σ)]) - \[ScriptCapitalK] Erfc[(t (2 q - 2 r + σ^2) - 2 Log[S0] + 2 Log[\[ScriptCapitalK]]/2 Sqrt[2] Sqrt[t] σ)])

In[8]:= BlackScholesPutOptionPrice[S0_, \[ScriptCapitalK]_, r_, q_, σ_, t_] = Assuming[S0 > 0 && r > 0 && q > 0 && σ > 0 && t > 0 && \[ScriptCapitalK] > 0, Exp[-r t]Expectation[PutOptVal[\[ScriptF] S0, \[ScriptCapitalK]], \[ScriptF]\[Distributed]f\[ScriptCapitalD][μ /. sol, σ, t]]//FullSimplify]

Out[8]= (1/2) E^-r t (\[ScriptCapitalK] + \[ScriptCapitalK] Erf[(t (2 q - 2 r + σ^2) + 2 Log[(\[ScriptCapitalK]/S0)]/2 Sqrt[2] Sqrt[t] σ)] - E^(-q + r) t S0 Erfc[(t (-2 q + 2 r + σ^2) + 2 Log[S0] - 2 Log[\[ScriptCapitalK]]/2 Sqrt[2] Sqrt[t] σ)])
```

You can now establish the celebrated Put-Call Parity relationship that $C_t-P_t= \text{S0}-\mathcal{K} e^{-r t}$ :

```wl
In[9]:= (BlackScholesCallOptionPrice[S0, \[ScriptCapitalK], r, q, σ, t] - BlackScholesPutOptionPrice[S0, \[ScriptCapitalK], r, q, σ, t] == S0 E^-q t - \[ScriptCapitalK] E^-r t)//FullSimplify

Out[9]= E^-r t \[ScriptCapitalK] (Erf[(t (2 q - 2 r + σ^2) - 2 Log[S0] + 2 Log[\[ScriptCapitalK]]/2 Sqrt[2] Sqrt[t] σ)] - Erf[(t (2 q - 2 r + σ^2) + 2 Log[(\[ScriptCapitalK]/S0)]/2 Sqrt[2] Sqrt[t] σ)]) == 0
```

Assuming rate $r$ of 5%, dividend yield of 2%, volatility parameter $\sigma$ of 0.087, an initial price of \$200 per share of stock, and a strike price of \$190 per share, the Black–Scholes call and put option prices are:

```wl
In[10]:= values = {\[ScriptCapitalK] -> 190, t -> 1, r -> 0.05, σ -> 0.087, S0 -> 200, q -> 0.02};

In[11]:= BlackScholesCallOptionPrice[S0, \[ScriptCapitalK], r, q, σ, t] /. values

Out[11]= 16.8488

In[12]:= BlackScholesPutOptionPrice[S0, \[ScriptCapitalK], r, q, σ, t] /. values

Out[12]= 1.54261
```

The above results can be compared favorably with ``FinancialDerivative`` :

```wl
In[13]:= FinancialDerivative[{"European", "Call"}, Evaluate[{"StrikePrice" -> \[ScriptCapitalK], "Expiration" -> t} /. values],   Evaluate[{"InterestRate" -> r, "Volatility" -> σ, "CurrentPrice" -> S0, "Dividend" -> q} /. values]]

Out[13]= 16.8488

In[14]:= FinancialDerivative[{"European", "Put"}, Evaluate[{"StrikePrice" -> \[ScriptCapitalK], "Expiration" -> t} /. values],   Evaluate[{"InterestRate" -> r, "Volatility" -> σ, "CurrentPrice" -> S0, "Dividend" -> q} /. values]]

Out[14]= 1.54261
```

#### Risk and Reliability (2)

Study the tail value at risk (TVaR) for the exponential distribution:

```wl
In[1]:=
MeanExcessLoss[dist_, d_] := Expectation[\[FormalX] - d\[Conditioned]\[FormalX] > d, \[FormalX]\[Distributed]dist]
VaR[dist_, p_] := Quantile[dist, p]
TVaR[dist_, p_] := VaR[dist, p] + MeanExcessLoss[dist, Quantile[dist, p]]

In[2]:= Simplify[MeanExcessLoss[ExponentialDistribution[λ], d], d > 0]

Out[2]= (1/λ)

In[3]:= Simplify[VaR[ExponentialDistribution[λ], p], 0 < p < 1]

Out[3]= -(Log[1 - p]/λ)

In[4]:= Simplify[TVaR[ExponentialDistribution[λ], p], 0 < p < 1 && λ > 0]

Out[4]= (1 - Log[1 - p]/λ)

In[5]:= Plot[Table[%, { λ, 1, 3, 0.5}]//Evaluate, {p, 0, 1}, PlotLegends -> (StringJoin["λ = ", ToString[#]]& /@ Range[1, 3, 0.5])]

Out[5]= [image]
```

---

Find the mean time to failure (MTTF) for an exponential life distribution:

```wl
In[1]:= MTTF = Expectation[T, T\[Distributed]ExponentialDistribution[λ]]

Out[1]= (1/λ)
```

#### Random Experiments (2)

A random sample of size 10 from a continuous distribution $\mathcal{D}$ is sorted in ascending order. A new random variate is generated. Find the probability that the 11$$^{\text{th}}$$ sample falls between the fourth and fifth smallest values in the sorted list:

```wl
In[1]:= \[ScriptCapitalD] = UniformDistribution[];n = 10;k = 4;

In[2]:= Expectation[Probability[x < z < y, z\[Distributed]\[ScriptCapitalD]], {x, y}\[Distributed]OrderDistribution[{\[ScriptCapitalD], n}, {k, k + 1}]]

Out[2]= (1/11)
```

The probability equals $1/(n+1)$ and is independent of $k$ :

```wl
In[3]:= Table[Expectation[Probability[x < z < y, z\[Distributed]\[ScriptCapitalD]], {x, y}\[Distributed]OrderDistribution[{\[ScriptCapitalD], n}, {k, k + 1}]], {k, 1, n - 1}]

Out[3]= {(1/11), (1/11), (1/11), (1/11), (1/11), (1/11), (1/11), (1/11), (1/11)}
```

It is also independent of the distribution:

```wl
In[4]:= \[ScriptCapitalD] = ExponentialDistribution[1];n = 6;

In[5]:= Table[Expectation[Probability[x < z < y, z\[Distributed]\[ScriptCapitalD]], {x, y}\[Distributed]OrderDistribution[{\[ScriptCapitalD], n}, {k, k + 1}]], {k, 1, n - 1}]

Out[5]= {(1/7), (1/7), (1/7), (1/7), (1/7)}
```

---

Four six-sided dice are rolled. Find the expectation of the minimum value:

```wl
In[1]:= Expectation[x, x\[Distributed]OrderDistribution[{DiscreteUniformDistribution[{1, 6}], 4}, 1]]

Out[1]= (2275/1296)

In[2]:= N[%]

Out[2]= 1.7554
```

Find the expectation of the maximum value:

```wl
In[3]:= Expectation[x, x\[Distributed]OrderDistribution[{DiscreteUniformDistribution[{1, 6}], 4}, 4]]

Out[3]= (6797/1296)

In[4]:= N[%]

Out[4]= 5.2446
```

Find the expectation of the sum of the three largest values. Using the identity $x_{(4)}+ x_{(3)}+ x_{(2)}=x_1+ x_2+ x_3+ x_4- x_{(1)}$ and linearity of ``Expectation``, you get:

```wl
In[5]:= Expectation[x1 + x2 + x3 + x4, {x1, x2, x3, x4}\[Distributed]ProductDistribution[{DiscreteUniformDistribution[{1, 6}], 4}]] - Expectation[x, x\[Distributed]OrderDistribution[{DiscreteUniformDistribution[{1, 6}], 4}, 1]]

Out[5]= (15869/1296)

In[6]:= N[%]

Out[6]= 12.2446
```

#### Other Applications (4)

A player bets amount $a$ in a casino with no betting limit in a game with a chance of winning $p$. If he loses he doubles the bet, and if he wins he quits, hence the number of games played follows a geometric distribution, with expected number of games played represented as follows:

```wl
In[1]:= Expectation[k + 1, k\[Distributed]GeometricDistribution[p]]

Out[1]= (1/p)
```

The cash reserve needed to win the $k$$$^{\text{th}}$$ game:

```wl
In[2]:= cashReserve[k_] := Sum[a 2^n - 1, {n, 1, k - 1}]
```

The player always leaves the casino collecting the amount of the initial bet:

```wl
In[3]:= a 2^k - 1 - cashReserve[k]//Simplify

Out[3]= a
```

The cash reserve needed to execute the above strategy is finite only for strictly favorable games, where $\frac{1}{2}<p\leq 1$ :

```wl
In[4]:= Expectation[cashReserve[k + 1], k\[Distributed]GeometricDistribution[p], GenerateConditions -> True]

Out[4]= ConditionalExpression[-((a*(-1 + p))/(-1 + 2*p)), p > 1/2]
```

---

A drug has proven to be effective in 40% of cases. Find the expected number of successes when applied to 700 cases:

```wl
In[1]:= Expectation[x, x\[Distributed]BinomialDistribution[700, 0.4]]

Out[1]= 280.
```

---

A baseball player is a 0.300 hitter. Find the expected number of hits if the player comes to bat 3 times:

```wl
In[1]:= Expectation[x, x\[Distributed]BinomialDistribution[3, 0.300]]

Out[1]= 0.9
```

---

Find the mean if the signal-to-noise ratio has a Weibull distribution:

```wl
In[1]:= AverageSNR = Expectation[γ, γ\[Distributed]WeibullDistribution[α, β]]

Out[1]= β Gamma[1 + (1/α)]
```

### Properties & Relations (10)

The expectation of an expression in a continuous distribution is defined by an integral:

```wl
In[1]:= Expectation[E ^ x, x\[Distributed]NormalDistribution[]]

Out[1]= Sqrt[E]

In[2]:= Integrate[E ^ x PDF[NormalDistribution[], x], {x, -Infinity, Infinity}]

Out[2]= Sqrt[E]
```

---

The expectation of an expression in a discrete distribution is defined by a sum:

```wl
In[1]:= Expectation[E ^ x, x\[Distributed]PoissonDistribution[1]]

Out[1]= E^-1 + E

In[2]:= Sum[E ^ x PDF[PoissonDistribution[1], x], {x, -Infinity, Infinity}]

Out[2]= E^-1 + E
```

---

A conditional expectation is defined by a ratio of expectation and probability:

```wl
In[1]:= \[ScriptCapitalD] = ExponentialDistribution[λ];

In[2]:= expr = x ^ 2;cond = x > 1;

In[3]:= Expectation[expr\[Conditioned]cond, x\[Distributed]\[ScriptCapitalD]] == (Expectation[Piecewise[{{expr, cond}}], x\[Distributed]\[ScriptCapitalD]]/Probability[cond, x\[Distributed]\[ScriptCapitalD]])

Out[3]= True
```

---

Use ``NExpectation`` to find the numerical value of an expectation:

```wl
In[1]:= dist = ExponentialDistribution[1];

In[2]:= Expectation[E ^ (-x ^ 2), x\[Distributed]dist]

Out[2]= (1/2) E^1 / 4 Sqrt[π] Erfc[(1/2)]

In[3]:= N[%]

Out[3]= 0.545641

In[4]:= NExpectation[E ^ (-x ^ 2), x\[Distributed]dist]

Out[4]= 0.545641
```

---

Compute the probability of an event:

```wl
In[1]:= Probability[1 < x < 3, x\[Distributed]ExponentialDistribution[2]]

Out[1]= (-1 + E^4/E^6)
```

Obtain the same result using ``Expectation`` :

```wl
In[2]:= Expectation[Boole[1 < x < 3], x\[Distributed]ExponentialDistribution[2]]

Out[2]= (-1 + E^4/E^6)
```

---

``N[Expectation[…]]`` is equivalent to ``NExpectation`` if symbolic evaluation fails:

```wl
In[1]:= f[x_ ? NumericQ] := x ^ 2 + 2

In[2]:= Expectation[f[x], x\[Distributed]NormalDistribution[]]

Out[2]= Expectation[f[x], x\[Distributed]NormalDistribution[0, 1]]

In[3]:= N[%]

Out[3]= 3.

In[4]:= NExpectation[f[x], x\[Distributed]NormalDistribution[]]

Out[4]= 3.
```

---

Use ``AsymptoticExpectation`` to find an asymptotic approximation of an expectation:

```wl
In[1]:= dist[b_] := ExponentialDistribution[b];

In[2]:= AsymptoticExpectation[E ^ (-x ^ 3), x\[Distributed]dist[b], {b, 0, 6}]

Out[2]= (b^3/6) - (b^6/360) - (2 b^2 π/3 Sqrt[3] Gamma[(1/3)]) + (b^5 π/54 Sqrt[3] Gamma[(1/3)]) + (2 b π/3 Sqrt[3] Gamma[(2/3)]) - (b^4 π/27 Sqrt[3] Gamma[(2/3)])
```

Obtain the same result using ``Asymptotic[Expectation[…]]`` :

```wl
In[3]:= Asymptotic[Expectation[E ^ (-x ^ 3), x\[Distributed]dist[b]], {b, 0, 6}]

Out[3]= (b^3/6) - (b^6/360) - (2 b^2 π/3 Sqrt[3] Gamma[(1/3)]) + (b^5 π/54 Sqrt[3] Gamma[(1/3)]) + (2 b π/3 Sqrt[3] Gamma[(2/3)]) - (b^4 π/27 Sqrt[3] Gamma[(2/3)])
```

---

``Mean``, ``Moment``, ``Variance``, and other properties are defined as expectations:

```wl
In[1]:= dist = ExponentialDistribution[3];

In[2]:= {Mean[dist], Expectation[x, x\[Distributed]dist]}

Out[2]= {(1/3), (1/3)}

In[3]:= {Variance[dist], Expectation[(x - Mean[dist]) ^ 2, x\[Distributed]dist]}

Out[3]= {(1/9), (1/9)}

In[4]:= {Moment[dist, 5], Expectation[x ^ 5, x\[Distributed]dist]}

Out[4]= {(40/81), (40/81)}

In[5]:= {CentralMoment[dist, 5], Expectation[(x - Mean[dist]) ^ 5, x\[Distributed]dist]}

Out[5]= {(44/243), (44/243)}

In[6]:= {FactorialMoment[dist, 5], Expectation[FactorialPower[x, 5]//FunctionExpand, x\[Distributed]dist]}

Out[6]= {(178/81), (178/81)}
```

---

Generating functions including ``MomentGeneratingFunction`` are defined by an expectation:

```wl
In[1]:= dist = ExponentialDistribution[3];

In[2]:= {MomentGeneratingFunction[dist, t], Expectation[E ^ (t x), x\[Distributed]dist]}//Simplify

Out[2]= {-(3/-3 + t), -(3/-3 + t)}

In[3]:= {CharacteristicFunction[dist, t], Expectation[E ^ (I t x), x\[Distributed]dist]}//Simplify

Out[3]= {(3 I/3 I + t), (3 I/3 I + t)}

In[4]:= {CentralMomentGeneratingFunction[dist, t], Expectation[E ^ (t (x - Mean[dist])), x\[Distributed]dist]}//Simplify

Out[4]= {-(3 E^-t / 3/-3 + t), -(3 E^-t / 3/-3 + t)}

In[5]:= {FactorialMomentGeneratingFunction[dist, t], Expectation[t ^ x, x\[Distributed]dist]}//Simplify

Out[5]= {-(3/-3 + Log[t]), -(3/-3 + Log[t])}
```

---

For a distribution specified by a list, ``Expectation`` is equivalent to using ``Mean`` :

```wl
In[1]:= data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

In[2]:= Expectation[x ^ 2 + 3, x\[Distributed]data]

Out[2]= 49

In[3]:= Mean[Table[x ^ 2 + 3, {x, data}]]

Out[3]= 49
```

### Possible Issues (1)

By default, the ``GenerateConditions`` for ``Integrate`` is set to ``False`` and ``Expectation`` may give the wrong result:

```wl
In[1]:= Expectation[x, x\[Distributed]CauchyDistribution[a, b]]

Out[1]= a
```

In fact, this expectation is undefined:

```wl
In[2]:= Mean[CauchyDistribution[a, b]]

Out[2]= Indeterminate
```

Set ``GenerateConditions`` to ``True`` to ensure the correct result:

```wl
In[3]:= Expectation[x, x\[Distributed]CauchyDistribution[a, b], GenerateConditions -> True]

Out[3]= Expectation[x, x\[Distributed]CauchyDistribution[a, b], GenerateConditions -> True]
```

## See Also

* [`NExpectation`](https://reference.wolfram.com/language/ref/NExpectation.en.md)
* [`AsymptoticExpectation`](https://reference.wolfram.com/language/ref/AsymptoticExpectation.en.md)
* [`Distributed`](https://reference.wolfram.com/language/ref/Distributed.en.md)
* [`Conditioned`](https://reference.wolfram.com/language/ref/Conditioned.en.md)
* [`Probability`](https://reference.wolfram.com/language/ref/Probability.en.md)
* [`NProbability`](https://reference.wolfram.com/language/ref/NProbability.en.md)
* [`Mean`](https://reference.wolfram.com/language/ref/Mean.en.md)
* [`Moment`](https://reference.wolfram.com/language/ref/Moment.en.md)
* [`CentralMoment`](https://reference.wolfram.com/language/ref/CentralMoment.en.md)
* [`FactorialMoment`](https://reference.wolfram.com/language/ref/FactorialMoment.en.md)
* [`Sum`](https://reference.wolfram.com/language/ref/Sum.en.md)
* [`Integrate`](https://reference.wolfram.com/language/ref/Integrate.en.md)

## Related Guides

* [Random Variables](https://reference.wolfram.com/language/guide/RandomVariables.en.md)
* [Probability & Statistics](https://reference.wolfram.com/language/guide/ProbabilityAndStatistics.en.md)
* [Actuarial Computation](https://reference.wolfram.com/language/guide/ActuarialComputation.en.md)
* [Probability & Statistics with Quantities](https://reference.wolfram.com/language/guide/ProbabilityWithQuantities.en.md)
* [Finite Mathematics](https://reference.wolfram.com/language/guide/FiniteMathematics.en.md)
* [Numerical Data](https://reference.wolfram.com/language/guide/NumericalData.en.md)
* [Descriptive Statistics](https://reference.wolfram.com/language/guide/DescriptiveStatistics.en.md)
* [`Reliability`](https://reference.wolfram.com/language/guide/Reliability.en.md)
* [Solvers over Regions](https://reference.wolfram.com/language/guide/GeometricSolvers.en.md)
* [Random Graphs](https://reference.wolfram.com/language/guide/RandomGraphs.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2016 (10.4)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn104.en.md)