2BCPST1 TD6 - Corrig´ e """ 2BCPST1 - TD 6 Variables aleatoires discr` etes I Lois discretes usuelles : loi binomiale et loi hypergeometrique """ plt.plot(x,y) plt.show() # EXERCICE 3 from random import shuffle from random import randint N = 100 p = 0.45 # EXERCICE 1 from scipy.misc import comb def loi_binomiale(k,n,p): if k in range(0,n+1): return comb(n,k) * p**k * (1-p)**(n-k) else: return 0 L = [1] * 45 + [0] * 55 shuffle(L) def tir(): T = [] for i in range(10): k = randint(0,N-1) T.append(L[k]) return T def loi_hypergeometrique(k,N,n,P): if k in range(0,n+1): return comb(N*P,k) * comb(N*(1-P),n-k) / comb(N,n) else: return 0 def frequence(m = 1000): Freq = [0] * 11 for i in range(m): T = tir() compteur = 0 for x in T: if x==1: compteur += 1 Freq[compteur] += 1/m return Freq # EXERCICE 2 import matplotlib.pyplot as plt import numpy as np n = 10 p = 0.4 x = np.arange(0,n+1) loi_binomiale_v = np.vectorize(loi_binomiale) y = loi_binomiale_v(x,n,p) plt.figure(1) plt.clf() plt.vlines(x,0,y,lw=2) plt.plot(x,y,’o’) plt.show() y = frequence() plt.figure(2) plt.clf() plt.vlines(x,0,y,lw=2) plt.plot(x,y,’o’) z = loi_binomiale_v(x,10,p) plt.plot(x,z) plt.title("Tir avec remise et loi binomiale") plt.show() loi_hypergeometrique_v = np.vectorize(loi_hypergeometrique) for N in range(20,50,10): y = loi_hypergeometrique_v(x,N,n,p) 1 2BCPST1 TD6 - Corrig´ e # EXERCICE 4 from random import sample def tir2(): return sample(L,10) def frequence2(m = 1000): Freq = [0] * 11 for i in range(m): T = tir2() compteur = 0 for x in T: if x==1: compteur += 1 Freq[compteur] += 1/m return Freq y = frequence2() plt.figure(3) plt.clf() plt.vlines(x,0,y,lw=2) plt.plot(x,y,’o’) z = loi_hypergeometrique_v(x,N,10,p) plt.plot(x,z) plt.title("Tir sans remise et loi hypergeometrique") plt.show() 2
© Copyright 2024 ExpyDoc