SymPy includes a module named stats that lets us create and manipulate random variables. This is useful when we work with probabilistic or statistical models; we can compute symbolic expectancies, variances, probabilities, and densities of random variables.




```python
from sympy import *
from sympy.stats import *
init_printing()
```

Let's roll two dice, X and Y, with six faces each:



```python
X, Y = Die('X', 6), Die('Y', 6)
```

We can compute probabilities defined by equalities (with the Eq operator) or inequalities:


```python
P(Eq(X, 3))
```


```python
P(X > 3)
```

Conditions can also involve multiple random variables:



```python
P(X > Y)
```

We can compute conditional probabilities:



```python
P(X + Y > 6, X < 5)
```

We can also work with arbitrary discrete or continuous random variables:



```python
Z = Normal('Z', 0, 1)  # Gaussian variable
```


```python
P(Z > pi)
```

We can compute expectancies and variances:



```python
E(Z**2), variance(Z**2)
```

We can also compute densities:



```python
f = density(Z)
var('x')
f(x)
```

We can plot these densities:



```python
%matplotlib inline
plot(f(x), (x, -6, 6))
```

SymPy's stats module contains many functions to define random variables with classical laws (binomial, exponential, and so on), discrete or continuous. It works by leveraging SymPy's powerful integration algorithms to compute exact probabilistic quantities as integrals of probability distributions. For example, P(Z > \pu is:




```python
Eq(Integral(f(x), (x, pi, oo)), simplify(integrate(f(x), (x, pi, oo))))
```


```python

```

3f7fa110b0a96db73c4d4cca01c17c5b79df3348
ipynb
Jupyter Notebook
Tutorial/Probabilities.ipynb
FallenGameR/LearningPython
95abec8a3cd31a35d117dcd472f320e6e4412f0d
MIT
Tutorial/Probabilities.ipynb
FallenGameR/LearningPython
95abec8a3cd31a35d117dcd472f320e6e4412f0d
MIT
Tutorial/Probabilities.ipynb
FallenGameR/LearningPython
95abec8a3cd31a35d117dcd472f320e6e4412f0d
MIT
Qwen/Qwen-72B
1. YES
2. YES