```python
%pylab inline
```

    Populating the interactive namespace from numpy and matplotlib



```python
import sympy as S
from sympy import stats
p = S.symbols('p',positive=True,real=True)
x=stats.Binomial('x',5,p)
t = S.symbols('t',real=True,positive=True)
mgf = stats.E(S.exp(t*x))
```


```python
from IPython.display import Math
```


```python
Math(S.latex(S.simplify(mgf)))
```




$$p^{5} e^{5 t} + 5 p^{4} \left(- p + 1\right) e^{4 t} + 10 p^{3} \left(p - 1\right)^{2} e^{3 t} - 10 p^{2} \left(p - 1\right)^{3} e^{2 t} + 5 p \left(p - 1\right)^{4} e^{t} - \left(p - 1\right)^{5}$$




```python
import scipy.stats
```


```python
def gen_sample(ns=100,n=10):
    out =[]
    for i in range(ns):
        p=scipy.stats.uniform(0,1).rvs()
        out.append(scipy.stats.binom(n,p).rvs())
    return out
```


```python
from collections import Counter
xs = gen_sample(1000)
hist(xs,range=(1,10),align='mid')
Counter(xs)
```

## sum of IID normally distributed random variables 


```python
S.var('x:2',real=True)
S.var('mu:2',real=True)
S.var('sigma:2',positive=True)
S.var('t',positive=True)
x0=stats.Normal(x0,mu0,sigma0)
x1=stats.Normal(x1,mu1,sigma1)
```


```python
mgf0=S.simplify(stats.E(S.exp(t*x0)))
mgf1=S.simplify(stats.E(S.exp(t*x1)))
mgfY=S.simplify(mgf0*mgf1)
```


```python
Math(S.latex(mgf0))
```




$$e^{\mu_{0} t + \frac{\sigma_{0}^{2} t^{2}}{2}}$$




```python
Math(S.latex(S.powsimp(mgfY)))
```




$$e^{\frac{t}{2} \left(2 \mu_{0} + 2 \mu_{1} + \sigma_{0}^{2} t + \sigma_{1}^{2} t\right)}$$




```python
S.collect(S.expand(S.log(mgfY)),t)
```




    t**2*(sigma0**2/2 + sigma1**2/2) + t*(mu0 + mu1)




```python

```

61294897e16749f56adb34f0e6b1238dcae0cbd3
ipynb
Jupyter Notebook
chapters/probability/notebooks/src-probability/MomentGeneratingFunction.ipynb
nsydn/Python-for-Probability-Statistics-and-Machine-Learning
d3e0f8ea475525a694a975dbfd2bf80bc2967cc6
MIT
2016-05-05T19:08:27.000Z
2022-03-31T05:09:19.000Z
chapters/probability/notebooks/src-probability/MomentGeneratingFunction.ipynb
crlsmcl/https-github.com-unpingco-Python-for-Probability-Statistics-and-Machine-Learning
6fd69459a28c0b76b37fad79b7e8e430d09a86a5
MIT
2016-05-12T22:18:58.000Z
2019-11-06T14:37:06.000Z
chapters/probability/notebooks/src-probability/MomentGeneratingFunction.ipynb
crlsmcl/https-github.com-unpingco-Python-for-Probability-Statistics-and-Machine-Learning
6fd69459a28c0b76b37fad79b7e8e430d09a86a5
MIT
2016-05-27T01:42:05.000Z
2022-03-27T11:20:27.000Z
Qwen/Qwen-72B
1. YES
2. YES