# Symbolic Statistics with SymPy

[back to overview page](index.ipynb)

This notebook just lists the (sometimes slightly modified) examples from the paper [Symbolic Statistics with SymPy](http://scitation.aip.org/content/aip/journal/cise/14/3/10.1109/MCSE.2012.56) ([doi:10.1109/MCSE.2012.56]( http://dx.doi.org/10.1109/MCSE.2012.56)) by  Matthew Rocklin and Andy R. Terrel, published in Computing in Science & Engineering, Volume 14, Issue 3 (2012).


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


```python
sympy.__version__
```




    '0.7.7.dev'



## Example 1: Dice

[sympy.stats.Die](http://docs.sympy.org/dev/modules/stats.html#sympy.stats.Die), by default 6-sided.


```python
X = stats.Die('X')
Y = stats.Die('Y')
```


```python
X
```


```python
type(X)
```




    sympy.stats.rv.RandomSymbol




```python
stats.density(X)
```


```python
stats.density(X).dict
```


```python
stats.P(X > 3)  # probability
```


```python
stats.E(X + Y)  # expectation
```


```python
mod_X = X + 3
mod_X
```


```python
stats.E(2*X + 4)
```

$P(X>3 \mid X+Y\ge 8)$


```python
stats.P(X > 3, X + Y >= 8)
```


```python
A = X > 3
B = X + Y >= 10
```

Note: this is not a proof for the general case:


```python
stats.P(B, A) == stats.P(A, B) * stats.P(B) / stats.P(A)
```




    True




```python
density = dict(heads=0.5, tails=0.5)
density
```




    {'heads': 0.5, 'tails': 0.5}




```python
coin = stats.FiniteRV('coin', density)
coin
```


```python
type(coin)
```




    sympy.stats.rv.RandomSymbol




```python
stats.density(coin)
```


```python
stats.density(coin).dict
```

## Table 1


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


```python
stats.E(2 * X)
```


```python
stats.variance(X + 3)
```


```python
stats.density(2 * X)
```

## Table 2


```python
X2 = stats.given(X, X < 3)
X2
```


```python
stats.density(X, X > 3)
```


```python
stats.E(X, X > 3)
```

## Example 2: Data Assimilation


```python
T = stats.Normal('T', 30, 3)
T
```


```python
type(T)
```




    sympy.stats.rv.RandomSymbol




```python
stats.density(T)
```


```python
stats.P(T > 33)
```


```python
_.n()
```


```python
noise = stats.Normal('noise', 0, 1.5)
noise
```


```python
type(noise)
```




    sympy.stats.rv.RandomSymbol




```python
observation = T + noise
observation
```


```python
T_posterior = stats.given(T, sympy.Eq(observation, 26))
T_posterior
```


```python
stats.density(T_posterior)
```

## Example 3: Kinematics


```python
# TODO
```

<p xmlns:dct="http://purl.org/dc/terms/">
  <a rel="license"
     href="http://creativecommons.org/publicdomain/zero/1.0/">
    
  </a>
  <br />
  To the extent possible under law,
  <span rel="dct:publisher" resource="[_:publisher]">the person who associated CC0</span>
  with this work has waived all copyright and related or neighboring
  rights to this work.
</p>

bc183bf29c3a2020f13384eea210085481c613d1
ipynb
Jupyter Notebook
statistics/symbolic-statistics.ipynb
mgeier/python-audio
70d4d62b148c08c50ec0057f8d4fd9876ce67a13
CC0-1.0
2015-04-14T20:13:25.000Z
2022-03-26T20:00:27.000Z
statistics/symbolic-statistics.ipynb
mgeier/python-audio
70d4d62b148c08c50ec0057f8d4fd9876ce67a13
CC0-1.0
2017-05-02T13:22:41.000Z
2017-05-03T13:17:15.000Z
statistics/symbolic-statistics.ipynb
mgeier/python-audio
70d4d62b148c08c50ec0057f8d4fd9876ce67a13
CC0-1.0
2015-04-19T14:08:37.000Z
2021-04-21T14:24:37.000Z
Qwen/Qwen-72B
1. YES
2. YES