## Few examples from sympy.stats


```python
from sympy import *
init_session(quiet=True)
%matplotlib inline
```

    



```python
from sympy.stats import Die, P, E
```

Let's play with two random variables, each representing a throw with a 6-sided die.


```python
X, Y = Die("X"), Die("Y")
```

Probability that we get 6 on both dice


```python
P(Eq(X, 6) & Eq(Y, 6))
```


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

You can ask about conditional probabilities


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

and conditional expectations


```python
E(X, X+Y>3)
```

There are many more statistical distributions in the `sympy.stats` module


```python
import sympy.stats
" ".join([p for p in dir(sympy.stats) if p[0].isupper() & (len(p)>1)])
```




    'Arcsin Benini Bernoulli Beta BetaPrime Binomial Cauchy Chi ChiNoncentral ChiSquared Coin ContinuousRV Covariance Dagum Die DiscreteUniform Erlang Expectation Exponential FDistribution FiniteRV FisherZ Frechet Gamma GammaInverse Geometric Gompertz Gumbel Hypergeometric Kumaraswamy Laplace LogNormal Logarithmic Logistic Maxwell Nakagami NegativeBinomial Normal Pareto Poisson Probability QuadraticU Rademacher RaisedCosine Rayleigh ShiftedGompertz StudentT Trapezoidal Triangular Uniform UniformSum Variance VonMises Weibull WignerSemicircle YuleSimon Zeta'




```python
from sympy.stats import Normal, sample, std, cdf
mu1, mu2 = symbols("mu1, mu2")
sigma1, sigma2 = symbols("sigma1, sigma2", positive=True)
```


```python
X, Y = Normal("X", mu1, sigma1), Normal("Y", mu2, sigma2)
```


```python
cdf(X)
```

Probability of encounterin random value larger than $\mu+\sigma$


```python
simplify(P(X>mu1+sigma1))
```

its numerical value


```python
simplify(P(X>mu1+sigma1)).evalf()
```

and the expression used to calculate it:


```python
P(X>mu1+sigma1, evaluate=False)
```

Random samples from the distribution can be generated


```python
sample(X)
```

Standard deviation of sum of two independent normally distributed random variables...


```python
simplify(std(X+Y))
```

If the integrals cannot be calculated analytically in SymPy,


```python
X, Y = Normal("X", 0, 1), Normal("Y", 1, 1)
P(X>Y, evaluate=False)
```

the probabilities can be estimated by Monte Carlo method (random sampling)


```python
P(X>Y, numsamples=100000)
```

99f71c0f879ff079a2c0ed56337737ee4adbee1e
ipynb
Jupyter Notebook
notebooks/04 Statistics.ipynb
rouckas/sympy-slides
c2777f0eddedd19c4bf094d40489f49c1ef8ad28
BSD-3-Clause
2018-10-22T19:52:48.000Z
2018-12-01T16:59:45.000Z
notebooks/04 Statistics.ipynb
rouckas/sympy-slides
c2777f0eddedd19c4bf094d40489f49c1ef8ad28
BSD-3-Clause
notebooks/04 Statistics.ipynb
rouckas/sympy-slides
c2777f0eddedd19c4bf094d40489f49c1ef8ad28
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES