Chi-squared test
To see:
https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.stats.chi2.html
http://glowingpython.blogspot.fr/2014/02/terms-selection-with-chi-square.html
https://stackoverflow.com/questions/22177576/python-minimizing-chi-squared
TODO:
Problèmatique: On suppose qu'on a un échantillon et que la loi de probabilité $L_{v}$ qui à généré cet échantillon est inconnue. Comment retrouver cette loi et ses paramètres à partir de l'échantillon dont on dispose ?
où :
| $x_i$ | pile | face |
|---|---|---|
| $n_{ei}$ | 47 | 53 |
| $n_{hi}$ | 50 | 50 |
La loi de probabilité à retrouver est une loi binomiale $\mathcal{b}(100, 0.25)$:
In [ ]:
n = 100
p = 0.25
In [ ]:
data = np.random.binomial(n=n, p=p, size=100000)
plt.hist(data,
bins=np.linspace(data.min(), data.max(), data.max() - data.min() + 1));
$E(X) = np = 25$
$V(X) = np(1-p) = 18.75$
$STD(X) = \sqrt{18.75} \simeq 4.33$
In [ ]:
import numpy as np
k = 100 # taille de l'echantillon
echantillon = np.random.binomial(n=n, p=p, size=k)
#np.random.normal(loc=m, scale=sigma, size=k)
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.hist(echantillon,
bins=np.linspace(echantillon.min(), echantillon.max(), echantillon.max() - echantillon.min() + 1));
In [ ]:
plt.hist(echantillon,
bins=np.linspace(echantillon.min(), echantillon.max(), echantillon.max() - echantillon.min() + 1),
cumulative=True);
In [ ]:
#print("moyenne:", m)
#print("écart type:", sigma)
print("moyenne empirique de l'échantillon:", echantillon.mean())
print("écart type empirique de l'échantillon:", echantillon.std())
In [ ]:
In [ ]:
def dist_chi2(x, *param):
n = x[0]
p = x[1]
dist = 0
n_xi = 10 # TODO
for xi in range(n_xi):
n_ei = 0 # TODO
n_hi = 0 # TODO
dist += ((n_ei - n_hi)**2) / n_hi
return dist
In [ ]:
from scipy import optimize
n_slice = slice(1., 200., 1.)
p_slice = slice(0.1, 1.0, 0.1)
search_ranges = (n_slice, p_slice)
#res = optimize.brute(dist_chi2,
# search_ranges,
# #args=params,
# full_output=True,
# finish=optimize.fmin)
#print("x* =", res[0])
#print("f(x*) =", res[1])