<jupyter_start><jupyter_text>MIT License (c) 2018 by <NAME>  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:<jupyter_code>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()
integrate((1/sqrt(2*pi))*exp(-u**2/2),(u,-oo,+oo))
AA=integrate(sqrt(1/(2*pi))*exp(-u**2/2),(u,-oo,x))
AA
(1+erf(sqrt(2)*x/2))/2-y
BB=solve((1+erf(sqrt(2)*x/2))/2-y,x)[0]
BB
sqrt(2)
sqrt(2.)
def F(xxx):
    return erf(sqrt(2)*xxx/2)/2 + 1/2

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

F(Finv(0.8))
Finv(F(5))
Finv(F(5.))<jupyter_output><empty_output><jupyter_text>Now plot the distribution function for the standard normal ${\cal N}(0,1)$ distribution law.<jupyter_code>t = arange(-3, 3, 0.01)
s=[F(u) for u in t]
fig, ax = plt.subplots()
ax.plot(t, s)
plt.show()
t = arange(.001, 1, 0.001)
s=[Finv(u) for u in t]
fig, ax = plt.subplots()
ax.plot(t, s)
plt.show()<jupyter_output><empty_output><jupyter_text>Plot the inverse: Compute the "exact" probability for falling outside the interval $[-3\sigma,3\sigma]$.<jupyter_code>2*(1-F(3.0))
1-(_)<jupyter_output><empty_output><jupyter_text>Now try to compute the above probability by using Monte Carlo simulation and sampling from the uniform distribution in $[0,1]$.<jupyter_code>2*(1-F(3.0))-0.002641
Finv(F(8))
1+1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16461916461916462, 'fraction_numerical': 0.045454545454545456, 'mean_word_length': 5.815899581589958, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '147291', 'license': 'MIT', 'n_tokens_mistral': 680, 'n_tokens_neox': 641, 'n_words': 172, 'path': 'Inverse_of_a_Distribution_Function_Example_Python.ipynb', 'repo_name': 'AndrewLyasoff/SMAP', 'size': '1636'}