Did I find the right examples for you? yes no

# sympy.stats.given

All Samples(12)  |  Call(9)  |  Derive(0)  |  Import(3)
```Conditional Random Expression
From a random expression and a condition on that expression creates a new
probability space from the condition and returns the same expression on that
conditional probability space.

Examples
========

>>> from sympy.stats import given, density, Die
>>> X = Die('X', 6)(more...)
```

```        def given(expr, condition=None, **kwargs):
""" Conditional Random Expression
From a random expression and a condition on that expression creates a new
probability space from the condition and returns the same expression on that
conditional probability space.

Examples
========

>>> from sympy.stats import given, density, Die
>>> X = Die('X', 6)
>>> Y = given(X, X>3)
>>> density(Y).dict
{4: 1/3, 5: 1/3, 6: 1/3}

Following convention, if the condition is a random symbol then that symbol
is considered fixed.

>>> from sympy.stats import Normal
>>> from sympy import pprint
>>> from sympy.abc import z

>>> X = Normal('X', 0, 1)
>>> Y = Normal('Y', 0, 1)
>>> pprint(density(X + Y, Y)(z), use_unicode=False)
2
-(-Y + z)
-----------
___       2
\/ 2 *e
------------------
____
2*\/ pi
"""

if not random_symbols(condition) or pspace_independent(expr, condition):
return expr

if isinstance(condition, RandomSymbol):
condition = Eq(condition, condition.symbol)

condsymbols = random_symbols(condition)
if (isinstance(condition, Equality) and len(condsymbols) == 1 and
not isinstance(pspace(expr).domain, ConditionalDomain)):
rv = tuple(condsymbols)[0]
results = solve(condition, rv)
return sum(expr.subs(rv, res) for res in results)

# Get full probability space of both the expression and the condition
fullspace = pspace(Tuple(expr, condition))
# Build new space given the condition
space = fullspace.conditional_space(condition, **kwargs)
# Dictionary to swap out RandomSymbols in expr with new RandomSymbols
# That point to the new conditional space
swapdict = rs_swap(fullspace.values, space.values)
# Swap random variables in the expression
expr = expr.xreplace(swapdict)
return expr
```

```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,
```
```def test_conditional_1d():
X = Normal('x', 0, 1)
Y = given(X, X >= 0)

assert density(Y) == 2 * density(X)
```
```    raises(ValueError, lambda: where(sin(X) > 1))

Y = given(X, X >= 0)

assert Y.pspace.domain.set == Interval(0, oo)
```
```def test_random_parameters_given():
mu = Normal('mu', 2, 3)
meas = Normal('T', mu, 1)
assert given(meas, Eq(mu, 5)) == Normal('T', 5, 1)

```

```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,
```
```    X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
assert where(And(X**2 <= 1, X >= 0)).set == Interval(0, 1)
XX = given(X, And(X**2 <= 1, X >= 0))
assert XX.pspace.domain.set == Interval(0, 1)
assert XX.pspace.domain.as_boolean() == \
And(0 <= X.symbol, X.symbol**2 <= 1)

with raises(TypeError):
XX = given(X, X + 3)
```
```def test_given():
X = Normal('X', 0, 1)
Y = Normal('Y', 0, 1)
A = given(X, True)
B = given(X, Y > 2)
```

```from sympy import (EmptySet, FiniteSet, S, Symbol, Interval, exp, erf, sqrt,