HLMA 408: Lois gaussiennes et visualisation interactive


Auteur: Joseph Salmon [email protected]

In [1]:
%matplotlib inline
from download import download
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
from scipy.stats import norm
from ipywidgets import interact  # widget manipulation
import seaborn as sns
In [2]:
sns.set_context("paper", font_scale=1)
sns.set_style("ticks")
sns.set_palette("colorblind")
In [3]:
dirname = "../prebuiltimages/"
imageformat = ".pdf"

Visualisation des densités gaussiennes: impact des paramètres de centrage ($\mu$) et de dispersion ($\sigma$)

In [4]:
x = np.linspace(-5, 5, num=400)
In [5]:
def gaussian_mean_and_std(mean=0, std=1):
    """Visualize the Gaussian quantile"""
    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))
    ax1.plot(x, norm.pdf(x, mean, std), '-', lw=2, label=r"$\varphi_{\mu, \sigma}$")
    ax1.set_ylim(0, 1)
    ax1.set_title(r"Densité d'une loi gaussienne en fonction des paramètres:" + "\n" +
                  r" Centrage $\mu = {0:.2f}$,  Dispersion: $\sigma = {1:.2f}$".format(mean, std), fontsize=10)
    plt.legend()
    plt.show()
In [6]:
interact(gaussian_mean_and_std, mean=(-3., 3., 0.01),std =(0.1, 4, 0.05))
Out[6]:
<function __main__.gaussian_mean_and_std>

Visualisation des quantiles gaussiens

In [7]:
def gaussian_quantile(alpha=0.9):
    """Visualize Gaussian quantiles"""

    quantile = norm.ppf(alpha, 0, 1)
    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))
    ax1.plot(x, norm.pdf(x, 0, 1), '-', lw=2, label=r"$\varphi$")
    ax1.set_ylim(0, 0.5)
    ax1.fill_between(x, 0, norm.pdf(x, 0, 1), where=x <= quantile)
    plt.axvline(x=quantile, c='k', ls="--", lw=1)
    ax1.set_title(r"Visualisation des quantiles:" + "\n" +
                  r"Aire $ \alpha = {0:.2f},$  seuil = $\Phi^{{-1}}(\alpha)={1:.2f}$".format(alpha, quantile), fontsize=10)
    plt.legend()
    plt.show()
In [8]:
interact(gaussian_quantile, alpha=(0.001, .999, 0.001))  # change the first and second value to check more quantiles 
Out[8]:
<function __main__.gaussian_quantile>
In [9]:
# Figures pour les slides:
In [10]:
# to use the default values of utils for instance
saving = True
path_target = "./utils.py"
url_shared_files = "http://josephsalmon.eu/enseignement/Montpellier/HLMA408/sharedcode/utils.py"
download(url_shared_files, path_target, replace=False)
from utils import my_saving_display
Replace is False and data exists, so doing nothing. Use replace==True to re-download the data.
In [11]:
x = np.linspace(-5, 5, 300)

fig, ax1 = plt.subplots(1, 1)
ax1.plot(x, norm.pdf(x, 0, 1), 'k-', lw=2, label=r"$\varphi$")
ax1.set_ylim(0, 0.5)
ax1.fill_between(x, 0, norm.pdf(x, 0, 1), where=x <= 0)
plt.axvline(x=0, c='k', ls="--", lw=1)
plt.title("$\Phi(0)=1/2$", fontsize=14)
plt.legend()
my_saving_display(fig, dirname, "Gaussian_phi0", imageformat, saving=saving)
In [12]:
x = np.linspace(-5, 5, 300)

lim = 1
fig, ax1 = plt.subplots(1, 1)
ax1.plot(x, norm.pdf(x, 0, 1), 'k-', lw=2, label=r"$\varphi$")
ax1.set_ylim(0, 0.5)
ax1.fill_between(x, 0, norm.pdf(x, 0, 1), where=x <= -
                 lim, color=sns.color_palette()[0])
ax1.fill_between(x, 0, norm.pdf(x, 0, 1), where=x >=
                 lim, color=sns.color_palette()[0])
# plt.axvline(x=0, c='k', ls="--", lw=1)
plt.title("$\Phi(-x)=1-\Phi(x)$", fontsize=14)
plt.legend()
my_saving_display(fig, dirname, "Gaussian_phi_moins_x",
                  imageformat, saving=saving)
In [13]:
norm.cdf(1, 0, 1)-norm.cdf(-1, 0, 1)
Out[13]:
0.6826894921370859
In [14]:
2 * norm.cdf(1, 0, 1)-1
Out[14]:
0.6826894921370859
In [15]:
2 * norm.cdf(2, 0, 1)-1
Out[15]:
0.9544997361036416
In [ ]: