Pdfcode
- 1. import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm,binom,poisson,uniform,expon
from math import sqrt,exp
####Loi uniforme
N = 100
a = 0
b = 5
t=0.5
v = (exp(t*b)-exp(t*a))/(t*(b-a))
u = (a+b)/2
sigma = ((b-a)**2)/12
s = np.random.uniform(a,b,N)
def uni(k,N,s):
x3,x4 = 0,0
for i in range(N):
if np.abs(s[i]-u)>= k:
x3 += 1
x4=x3/N
return x4
def uni2(k,N,s):
x2 = 0
x1 = 0
for i in range(N):
if s[i]>= k:
x1 += 1
x2 = x1/N
return x2
xlist=np.arange(0.5,5,0.01)
def chebyshev(u,sigma,k):
return sigma/k**2
chebylist=[]
def markov(u,k):
return u/k
markovlist=[]
def chernoff(v,k,t):
return v/exp(t*k)
chernofflist = []
pklist = []
difflist= []
difflist2= []
- 2. difflist3= []
for k in xlist:
chebylist.append(chebyshev(u,sigma,k))
markovlist.append(markov(u,k))
chernofflist.append(chernoff(v,k,t))
pklist.append(uni(k,N,s))
difflist.append(np.abs(uni(k,N,s)-chebyshev(u,sigma,k)))
difflist2.append(np.abs(uni2(k,N,s)-markov(u,k)))
difflist3.append(np.abs(uni2(k,N,s)-chernoff(v,k,t)))
uniflist = uniform.sf(xlist,a,b)
#plt.plot(xlist,uniflist,label= 'Loi uniforme([{},{}])'.format(a,b))
#plt.plot(xlist,markovlist,label = 'Inégalité de Markov')
#plt.plot(xlist,chernofflist,label = 'Inégalité de Chernoff')
#plt.xlabel('k')
#plt.ylabel('P(X>k)')
#plt.plot(xlist,pklist,label = 'Loi uniforme([{},{}])'.format(a,b))
#plt.plot(xlist,chebylist,label = 'Inégalité de Chebyshev')
#plt.xlabel('k')
#plt.ylabel('P(|X-E(X)|>k)')
plt.plot(xlist,difflist,label = 'Inégalité de Chebyshev')
plt.plot(xlist,difflist2,label = 'Inégalité de Markov')
plt.plot(xlist,difflist3,label = 'Inégalité de Chernoff')
#plt.ylabel('Valeur de l'erreur')
#plt.xlabel('k')
plt.legend()
plt.show()
###### Loi de Bernoulli
N = 100
n = 1
p = 0.5
t=1
u = n*p
sigma = n*p*(1-p)
s = np.random.binomial(n,p,N)
xlist=np.arange(0.5,50,1)
v = (exp(t)*p + (1-p))**n
chernofflist = []
- 3. chebylist=[]
markovlist=[]
pklist = []
difflist= []
difflist2= []
difflist3= []
for k in xlist:
chebylist.append(chebyshev(u,sigma,k))
markovlist.append(markov(u,k))
chernofflist.append(chernoff(v,k,t))
pklist.append(uni(k,N,s))
difflist.append(np.abs(uni(k,N,s)-chebyshev(u,sigma,k)))
difflist2.append(np.abs(uni2(k,N,s)-markov(u,k)))
difflist3.append(np.abs(uni2(k,N,s)-chernoff(v,k,t)))
bernlist = binom.sf(xlist,1,p)
plt.plot(xlist,bernlist,label = 'Loi de Bernoulli({})'.format(p))
plt.plot(xlist,markovlist,label = 'Inégalité de Markov')
plt.plot(xlist,chernofflist,label = 'Inégalité de Chernoff')
#plt.xlabel('k')
#plt.ylabel('P(X>k)')
#plt.plot(xlist,pklist,'Loi de Bernoulli({})'.format(p))
#plt.plot(xlist,chebylist,label = 'Inégalité de Chebyshev')
#plt.xlabel('k')
#plt.ylabel('P(|X-E(X)|>k)')
#plt.plot(xlist,difflist,label = 'Inégalité de Chebyshev')
#plt.plot(xlist,difflist2,label = 'Inégalité de Markov')
#plt.plot(xlist,difflist3,label = 'Inégalité de Chernoff')
#plt.ylabel('Valeur de l'erreur')
#plt.xlabel('k')
plt.legend()
plt.show()
############ Loi Binomiale
N = 100
n = 10
p = 0.5
t=0.5
u = n*p
sigma = n*p*(1-p)
v = (exp(t)*p + (1-p))**n
- 4. s = np.random.binomial(n,p,N)
xlist=np.arange(0.5,50,1)
chernofflist = []
chebylist=[]
markovlist=[]
pklist = []
difflist3=[]
difflist= []
difflist2= []
for k in xlist:
chebylist.append(chebyshev(u,sigma,k))
markovlist.append(markov(u,k))
chernofflist.append(chernoff(v,k,t))
pklist.append(uni(k,N,s))
difflist.append(np.abs(uni(k,N,s)-chebyshev(u,sigma,k)))
difflist2.append(np.abs(uni2(k,N,s)-markov(u,k)))
difflist3.append(np.abs(uni2(k,N,s)-chernoff(v,k,t)))
binomlist = binom.sf(xlist,n,p)
plt.plot(xlist,binomlist,label='Loi Binomiale({},{})'.format(n,p))
plt.plot(xlist,markovlist,label = 'Inégalité de Markov')
plt.plot(xlist,chernofflist,label = 'Inégalité de Chernoff')
#plt.xlabel('k')
#plt.ylabel('P(X>k)')
#plt.plot(xlist,pklist,label='Loi Binomiale({},{})'.format(n,p))
#plt.plot(xlist,chebylist,label = 'Inégalité de Chebyshev')
#plt.xlabel('k')
#plt.ylabel('P(|X-E(X)|>k)')
#plt.plot(xlist,difflist,label = 'Inégalité de Chebyshev')
#plt.plot(xlist,difflist2,label = 'Inégalité de Markov')
#plt.plot(xlist,difflist3,label = 'Inégalité de Chernoff')
#plt.ylabel('Valeur de l'erreur')
#plt.xlabel('k')
plt.legend()
plt.show()
###### Loi de Poisson
N = 100
lam = 0.3
t=1
u = lam
sigma = lam**2
- 5. s = np.random.poisson(lam,N)
v = exp(lam*(exp(t)-1))
xlist=np.arange(0.5,10,0.01)
chebylist=[]
markovlist=[]
pklist = []
chernofflist = []
difflist= []
difflist2= []
difflist3= []
for k in xlist:
chebylist.append(chebyshev(u,sigma,k))
markovlist.append(markov(u,k))
chernofflist.append(chernoff(v,k,t))
pklist.append(uni(k,N,s))
difflist.append(np.abs(uni(k,N,s)-chebyshev(u,sigma,k)))
difflist2.append(np.abs(uni2(k,N,s)-markov(u,k)))
difflist3.append(np.abs(uni2(k,N,s)-chernoff(v,k,t)))
poisslist = poisson.sf(xlist,u)
plt.plot(xlist,poisslist,label= 'Loi de Poisson({})'.format(lam))
plt.plot(xlist,markovlist,label = 'Inégalité de Markov')
#plt.plot(xlist,chernofflist,label='Inégalité de Chernoff')
#plt.xlabel('k')
#plt.ylabel('P(X>k)')
#plt.plot(xlist,pklist,label='Loi de Poisson({})'.format(lam))
#plt.plot(xlist,chebylist,label='Inégalité de Chebyshev')
#plt.xlabel('k')
#plt.ylabel('P(|X-E(X)|>k)')
#plt.plot(xlist,difflist,label = 'Inégalité de Chebyshev')
#plt.plot(xlist,difflist2,label = 'Inégalité de Markov')
#plt.plot(xlis,difflist3,label = 'Inégalité de Chernoff')
#plt.ylabel('Valeur de l'erreur')
#plt.xlabel('k')
plt.legend()
plt.show()
##### Loi exponentille
N = 100
lam = 2
- 6. #t>lam
t=3
u = 1/lam
sigma = 1/lam**2
s = np.random.exponential(u,N)
v=lam/(t-lam)
xlist=np.arange(0.5,10,0.01)
explist = expon.sf(xlist,u)
chebylist=[]
markovlist=[]
chernofflist=[]
pklist = []
difflist= []
difflist2= []
difflist3=[]
for k in xlist:
chebylist.append(chebyshev(u,sigma,k))
markovlist.append(markov(u,k))
pklist.append(uni(k,N,s))
chernofflist.append(chernoff(v,k,t))
difflist.append(np.abs(uni(k,N,s)-chebyshev(u,sigma,k)))
difflist2.append(np.abs(uni2(k,N,s)-markov(u,k)))
difflist3.append(np.abs(uni2(k,N,s)-chernoff(v,k,t)))
plt.plot(xlist,explist,label='Loi Exponentielle({})'.format(lam))
plt.plot(xlist,markovlist,label = 'Inégalité de Markov')
plt.plot(xlist,chernofflist,label = 'Inégalité de Chernoff')
plt.xlabel('k')
plt.ylabel('P(X>k)')
#plt.plot(xlist,pklist,label = 'Loi Exponentielle({})'.format(lam))
#plt.plot(xlist,chebylist,label = 'Inégalité de Chebyshev')
#plt.xlabel('k')
#plt.ylabel('P(|X-E(X)|>k)')
#plt.plot(xlist,difflist,label = 'Inégalité de Chebyshev')
#plt.plot(xlist,difflist2,label = 'Inégalité de Markov')
#plt.plot(xlist,difflist3,label = 'Inégalité de Chernoff')
#plt.ylabel('Valeur de l'erreur')
#plt.xlabel('k')
plt.legend()
plt.show()
- 7. #############"" Loi normale standard
N = 100
u=0
sigma=1
s = np.random.normal(u,sigma,N)
t=1
xlist=np.arange(0.5,5,0.01)
v = exp(-u*t-0.5*sigma*(t**2))
chebylist=[]
markovlist=[]
chernofflist=[]
pklist = []
difflist= []
difflist2= []
difflist3=[]
for k in xlist:
chebylist.append(chebyshev(u,sigma,k))
markovlist.append(markov(u,k))
pklist.append(uni(k,N,s))
chernofflist.append(chernoff(v,k,t))
difflist.append(np.abs(uni(k,N,s)-chebyshev(u,sigma,k)))
difflist2.append(np.abs(uni2(k,N,s)-markov(u,k)))
difflist3.append(np.abs(uni2(k,N,s)-chernoff(v,k,t)))
normlist = norm.sf(xlist,u)
plt.plot(xlist,normlist,label = 'Loi Normale({},{})'.format(u,sigma))
plt.plot(xlist,markovlist,label = 'Inégalité de Markov')
plt.plot(xlist,chernofflist,label = 'Inégalité de Chernoff')
#plt.xlabel('k')
#plt.ylabel('P(X>k)')
#plt.plot(xlist,pklist,label='Loi Normale({},{})'.format(u,sigma))
#plt.plot(xlist,chebylist,label = 'Inégalité de Chebyshev')
#plt.xlabel('k')
#plt.ylabel('P(|X-E(X)|>k)')
#plt.plot(xlist,difflist,label = 'Inégalité de Chebyshev')
#plt.plot(xlist,difflist2,label = 'Inégalité de Markov')
#plt.plot(xlist,difflist3,label = 'Inégalité de Chernoff')
#plt.ylabel('Valeur de l'erreur')
#plt.xlabel('k')
plt.legend()
plt.show()