More Related Content
Similar to Codecomparaison
Similar to Codecomparaison (20)
Codecomparaison
- 1. import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm,binom,poisson,uniform,expon
from math import sqrt,exp
from scipy.integrate import quad
def uni2(n,N,s):
x1,x2 = 0,0
for i in range(N):
if s[i]>= (n+sqrt(n))/2: #P(X >= n+sqrt(n))/2)
x1 += 1
x2 = x1/N
return x2
def uni(n,N,s):
x3,x4 = 0,0,
for i in range(N):
if np.abs(s[i]-u)>= sqrt(n)/2:
x3 += 1
x4=x3/N
return x4
def uni3(n,N,s,x):
x5,x6=0,0
for i in range(N):
if np.abs(s[i]-u)>= x*sqrt(n):
x5+=1
x6=x5/N
return x6
def chebyshev(u,sigma,n):
return sigma/((sqrt(n))/2)**2
chebylist=[]
def markov(u,n):
return u/((n+sqrt(n))/2)
markovlist=[]
def chernoff(v,n,t):
return v/exp(t*((n+sqrt(n))/2))
chernofflist = []
def hoeffding(x):
return 2*exp(-x**2)
def chebyshev2(p,x):
return p*(1-p)/x**2
- 2. ###### X suit B(n,1/2)
N= 100
p=0.5
t=0.5
x= 1
xlist = np.arange(0.5,100,0.1)
binom1=[]
binom2=[]
binom3=[]
chernofflist = []
chebylist=[]
chebylist2=[]
markovlist=[]
hoeffdinglist=[]
for n in xlist:
u=n*p
s = np.random.binomial(n,p,N)
sigma = n*p*(1-p)
v = (exp(t)*p + (1-p))**n
binom1.append(uni(n,N,s))
binom2.append(uni2(n,N,s))
binom3.append(uni3(n,N,s,x))
chebylist.append(chebyshev(u,sigma,n))
markovlist.append(markov(u,n))
chernofflist.append(chernoff(v,n,t))
hoeffdinglist.append(hoeffding(x))
chebylist2.append(chebyshev2(p,x))
plt.plot(xlist,binom1,label='P(|X- 'r'$frac {n}{2}| geqslantfrac {sqrt{n}} {2})$')
plt.plot(xlist,chebylist,label = 'Chebyshev')
plt.xlabel('n')
plt.ylabel('Probabilité')
plt.legend()
plt.show()
###############Chebyshev = 1 inutile et on remarque numériquement que : P(|X-n/2|⩾√n/2)⩽1/2
########*Chernoff et markov loin d'être optimaux et on remarque numériquement que :
P(X⩾(n+√n)/2)⩽1/4
plt.plot(xlist,binom2,label='P(X'r'$ geqslant frac {n+sqrt{n}} {2})$')
plt.plot(xlist,chernofflist, label = 'Chernoff')
plt.plot(xlist,markovlist,label = 'Markov')
- 3. plt.xlabel('n')
plt.ylabel('Probabilité')
plt.ylim(ymax=1)
plt.legend()
plt.show()
##############
### P(|X- n/2|⩾x√n)⩽p(1−p)/x²: Chebyshev
###P(|X- n/2|⩾x√n)⩽2exp(−2x²): Hoeffding
plt.plot(xlist,binom3,label='P(|X- 'r'$frac {n}{2}| geqslant xsqrt{n})$' )
plt.plot(xlist,hoeffdinglist,label = 'Hoeffding')
plt.plot(xlist,chebylist2,label = 'Chebyshev')
plt.xlabel('n')
plt.ylabel('P(|X- 'r'$frac {n}{2}| geqslant xsqrt{n})$')
plt.legend()
plt.show()
####### Comparaison Chebyshev et Hoeffding en fonction de x
blist = np.arange(0.5,10,0.1)
chebylist3=[]
hoeffdinglist2=[]
for x in blist :
hoeffdinglist2.append(hoeffding(x))
chebylist3.append(chebyshev2(p,x))
plt.plot(blist,hoeffdinglist2,label = 'Hoeffding')
plt.plot(blist,chebylist3,label = 'Chebyshev')
plt.xlabel('x')
plt.legend()
plt.show()