#### MIT License (c) 2018 by Andrew Lyasoff

#### Jupyter notebook written in Python 3. It illustrates the use of SymPy to compute the distribution function of the Gaussian law and its inverse, which is then used to transform a uniform Monte Carlo sample into a Gaussian Monte Carlo sample.

First, compute the distribution function and its inverse in symbolic form:


```python
import numpy as np
import matplotlib.pyplot as plt
from numpy import *
from sympy import *
x,y,n,u=symbols('x y n u')
init_printing()
```


```python
integrate((1/sqrt(2*pi))*exp(-u**2/2),(u,-oo,+oo))
```


```python
AA=integrate(sqrt(1/(2*pi))*exp(-u**2/2),(u,-oo,x))
AA
```


```python
(1+erf(sqrt(2)*x/2))/2-y
```


```python
BB=solve((1+erf(sqrt(2)*x/2))/2-y,x)[0]
BB
```


```python
sqrt(2)
```


```python
sqrt(2.)
```


```python
def F(xxx):
    return erf(sqrt(2)*xxx/2)/2 + 1/2

def Finv(yyy):
    return sqrt(2)*erfinv(2*yyy - 1)

```


```python
F(Finv(0.8))
```


```python
Finv(F(5))
```


```python
Finv(F(5.))
```

Now plot the distribution function for the standard normal ${\cal N}(0,1)$ distribution law. 


```python
t = arange(-3, 3, 0.01)
s=[F(u) for u in t]
fig, ax = plt.subplots()
ax.plot(t, s)
plt.show()
```


```python
t = arange(.001, 1, 0.001)
s=[Finv(u) for u in t]
fig, ax = plt.subplots()
ax.plot(t, s)
plt.show()
```

Plot the inverse:

Compute the "exact" probability for falling outside the interval $[-3\sigma,3\sigma]$.


```python
2*(1-F(3.0))
```


```python
1-(_)
```

Now try to compute the above probability by using Monte Carlo simulation and sampling from the uniform distribution in $[0,1]$.


```python
2*(1-F(3.0))-0.002641
```


```python
Finv(F(8))
```


```python
1+1
```


```python

```

5ed4c3761cef0d203f4bc7c34606e88fcb2e5ace
ipynb
Jupyter Notebook
Inverse_of_a_Distribution_Function_Example_Python.ipynb
AndrewLyasoff/SMAP
6eeea8953a26a05b1e23387067109d23b2011824
MIT
2018-09-04T19:12:32.000Z
2022-03-20T02:05:44.000Z
Inverse_of_a_Distribution_Function_Example_Python.ipynb
lhyzh/SMAP
f6687291769d4c16a0d51a06a941384f646bb432
MIT
2019-02-13T14:24:38.000Z
2019-02-13T14:24:38.000Z
Inverse_of_a_Distribution_Function_Example_Python.ipynb
lhyzh/SMAP
f6687291769d4c16a0d51a06a941384f646bb432
MIT
2019-02-10T03:43:40.000Z
2021-03-28T03:53:22.000Z
Qwen/Qwen-72B
1. YES
2. YES