Did I find the right examples for you? yes no

# sympy.stats.E

All Samples(60)  |  Call(56)  |  Derive(0)  |  Import(4)
```Returns the expected value of a random expression

Parameters
----------
expr : Expr containing RandomSymbols
The expression of which you want to compute the expectation value
given : Expr containing RandomSymbols
A conditional expression. E(X, X>0) is expectation of X given X > 0
numsamples : int
Enables sampling and approximates the expectation with this many samples(more...)
```

```        def expectation(expr, condition=None, numsamples=None, evaluate=True, **kwargs):
"""
Returns the expected value of a random expression

Parameters
----------
expr : Expr containing RandomSymbols
The expression of which you want to compute the expectation value
given : Expr containing RandomSymbols
A conditional expression. E(X, X>0) is expectation of X given X > 0
numsamples : int
Enables sampling and approximates the expectation with this many samples
evalf : Bool (defaults to True)
If sampling return a number rather than a complex expression
evaluate : Bool (defaults to True)
In case of continuous systems return unevaluated integral

Examples
========

>>> from sympy.stats import E, Die
>>> X = Die('X', 6)
>>> E(X)
7/2
>>> E(2*X + 1)
8

>>> E(X, X>3) # Expectation of X given that it is above 3
5
"""

if not random_symbols(expr):  # expr isn't random?
return expr
if numsamples:  # Computing by monte carlo sampling?
return sampling_E(expr, condition, numsamples=numsamples)

# Create new expr and recompute E
if condition is not None:  # If there is a condition
return expectation(given(expr, condition), evaluate=evaluate)

# A few known statements for efficiency

if expr.is_Add:  # We know that E is Linear
for arg in expr.args])

# Otherwise case is simple, pass work off to the ProbabilitySpace
result = pspace(expr).integrate(expr)
if evaluate and hasattr(result, 'doit'):
return result.doit(**kwargs)
else:
return result
```

```from sympy import (EmptySet, FiniteSet, S, Symbol, Interval, exp, erf, sqrt,
symbols, simplify, Eq, cos, And, Tuple, integrate, oo, sin, Sum, Basic,
DiracDelta)
from sympy.stats import (Die, Normal, Exponential, P, E, variance, covariance,
skewness, density, given, independent, dependent, where, pspace,
```
```def test_E():
assert E(5) == 5

def test_Sample():
```
```
# Make sure this doesn't raise an error
E(Sum(1/z**Y, (z, 1, oo)), Y > 2, numsamples=3)

```

```from sympy.stats import (P, E, where, density, variance, covariance, skewness,
given, pspace, cdf, ContinuousRV, sample,
Arcsin, Benini, Beta, BetaPrime, Cauchy,
Chi, ChiSquared,
ChiNoncentral, Dagum, Erlang, Exponential,
```
```    Y = X*sigma + mu

assert simplify(E(Y)) == mu
assert simplify(variance(Y)) == sigma**2
pdf = density(Y)
```
```    assert P(X**2 < 1) == erf(2**S.Half/2)

assert E(X, Eq(X, mu)) == mu

```
```
assert Y.pspace.domain.set == Interval(0, oo)
assert E(Y) == sqrt(2) / sqrt(pi)

assert E(X**2) == E(Y**2)
```

```from sympy import (EmptySet, FiniteSet, S, Symbol, Interval, exp, erf, sqrt,
symbols, simplify, Eq, cos, And, Tuple, Or, Dict, sympify, binomial,
factor)
from sympy.stats import (DiscreteUniform, Die, Bernoulli, Coin, Binomial,
Hypergeometric, P, E, variance, covariance, skewness, sample, density,
```
```    X = DiscreteUniform('X', [a, b, c])

assert E(X) == (a + b + c)/3
assert simplify(variance(X)
- ((a**2 + b**2 + c**2)/3 - (a/3 + b/3 + c/3)**2)) == 0
```
```
# Numeric
assert E(Y) == S('-1/2')
assert variance(Y) == S('33/4')

```
```    a, b = symbols('a b')

assert E(X) == 3 + S.Half
assert variance(X) == S(35)/12
assert E(X + Y) == 7
```

```from sympy.stats.drv_types import (PoissonDistribution, GeometricDistribution,
Poisson)
from sympy.abc import x
from sympy import S, Sum
from sympy.stats import E, variance, density
```
```def test_Poisson():
l = 3
x = Poisson('x', l)
assert E(x) == l
assert variance(x) == l
assert density(x) == PoissonDistribution(l)
assert isinstance(E(x, evaluate=False), Sum)
assert isinstance(E(2*x, evaluate=False), Sum)
```