MDI 720 : Statistiques

IntroIC

Joseph Salmon

This notebook reproduces the pictures for the course "IntroStatics"

REM:

  • you need TeX install on your machine (otherwise errors could appends)
  • you need plot_species_kde.py in the same directory
In [ ]:
# import packages
import numpy as np
import matplotlib.pyplot as plt  # for plots
from matplotlib import rc
import seaborn as sns
from os import mkdir, path
from scipy import stats
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from statsmodels.nonparametric.kde import KDEUnivariate
from scipy.stats import norm
sns.set_style("white")
sns.set_palette("Blues")
%matplotlib notebook
In [ ]:
saving = True
dirname = "../prebuiltimages/"
imageformat = '.pdf'

if not path.exists(dirname):
    mkdir(dirname)

def my_saving_display(fig, dirname, filename, imageformat):
    """"Saving with personal function."""
    filename = filename.replace('.', 'pt')  # remove "." to avoid floats issues
    if saving is True:
        dirname + filename + imageformat
        image_name = dirname + filename + imageformat
        fig.savefig(image_name)
In [ ]:
n_repetition = 500
tab_n_samples =  [5, 20, 50, 500]
np.random.seed(seed=44)
fig = plt.figure()
for i, n_samples in enumerate(tab_n_samples):
    X = np.random.randn(n_samples, n_repetition)
    vect_1n = np.arange(1,n_samples + 1)    
    kde = KDEUnivariate(np.sort(np.mean(X,0) * np.sqrt(n_samples)))
    kde.fit(bw=0.25, kernel='gau')
    x_grid = np.linspace(-3, 3, 100)
    pdf_est = kde.evaluate(x_grid)
    plt.plot(x_grid, pdf_est,label=str(n_samples))

plt.plot(x_grid, norm.pdf(x_grid), c='k', linewidth=2,label="asymptotic")
plt.legend()
plt.title('TCL: convergence of $\\bar{y}_n$ density w.r.t the number of samples')
plt.show()

my_saving_display(fig, dirname, "TCL_illustration", imageformat)