File: td_note_2013_preparation.tex, line 25
import random i = random.randint(0,5)
, correction 2013
#coding:latin-1 import random # question 1 def sequence () : res = [ ] nb = 1 while nb > 0 : i = random.randint(0,2) nb += i - 1 res.append (i) return res # question 2 def moyenne (nb_tirage) : somme = 0.0 for i in range(nb_tirage) : s = sequence() somme += len(s) return somme / nb_tirage s = sequence () print len(s),s m = moyenne (100) print m
, correction 2013
#coding:latin-1 import random def randomint (a,b,c) : x = random.random() if x <= a : return 0 elif x <= a+b : return 1 else : return 2 def sequence (a,b,c) : res = [ ] nb = 1 while nb > 0 : i = randomint(a,b,c) nb += i - 1 res.append (i) return res def moyenne (nb_tirage,a,b,c) : somme = 0.0 for i in range(nb_tirage) : s = sequence(a,b,c) somme += len(s) return somme / nb_tirage a,c = 0.3, 0.2 b = 1-a-c moy = 1.0 / (a-c) print "calcul",moy m1 = moyenne (100000, a,b,c) print "simulée", m1
, correction 2013
#coding:latin-1 import random, math # question 1 def calcul_suite_a (n, e, a,b,c) : p = {} p [0,0] = 1 for u in range (1,n+1) : for k in range (e,u+2) : if k == e : p [u,k] = a * p.get ( (u-1,k+1), 0 ) elif k == e+1 : p [u,k] = a * p.get ( (u-1,k+1), 0 ) + \ b * p.get ( (u-1,k ), 0 ) elif k > e+1 : p [u,k] = a * p.get ( (u-1,k+1), 0 ) + \ b * p.get ( (u-1,k ), 0 ) + \ c * p.get ( (u-1,k-1), 0 ) return p def affiche_proba (ps, e) : n = max ( [ k[1] for k,z in ps.iteritems () ] ) moy = 0.0 logru = [] logu = [] for u in range (1, n+1) : p = ps.get((u,e),0)*1.0 moy += p * u mes = "u % 3d P(U=u) %1.6g r_u %1.6g" % (u, p, moy) if u < 3 or u %50 == 0 : print mes logru.append(math.log(moy)) logu.append(math.log(u)) import pylab pylab.plot ( logu, logru, "o") pylab.show() a,c = 1.0/3, 1.0/3 b = 1-a-c e = -1 su = calcul_suite_a(600,e,a,b,c) affiche_proba(su, e)
File: td_note_2013_preparation.tex, line 186
u 1 P(U=u) 0.333333 r_u 0.333333 u 2 P(U=u) 0.111111 r_u 0.555556 u 50 P(U=u) 0.0013566 r_u 5.57241 u 100 P(U=u) 0.00048407 r_u 8.38753 u 150 P(U=u) 0.000264311 r_u 10.5627 u 200 P(U=u) 0.000171942 r_u 12.4016 u 250 P(U=u) 0.000123146 r_u 14.0242 u 300 P(U=u) 9.37388e-05 r_u 15.4926 u 350 P(U=u) 7.44204e-05 r_u 16.8438 u 400 P(U=u) 6.09325e-05 r_u 18.1021 u 450 P(U=u) 5.10779e-05 r_u 19.2843 u 500 P(U=u) 4.36202e-05 r_u 20.4028 u 550 P(U=u) 3.78157e-05 r_u 21.4669 u 600 P(U=u) 3.31933e-05 r_u 22.4839
File: td_note_2013_preparation.tex, line 311
import random N = 10 M = [ [ 1 if random.randint(1,5) == 1 else 0 for i in range (N) ] for j in range(N) ] for l in M : print l nb = 0 for i in range(N) : for j in range (N) : if i > 0 and M[i-1][j] == 1 : nb += 1 elif i < N-1 and M[i+1][j] == 1 : nb += 1 elif j > 0 and M[i][j-1] == 1 : nb += 1 elif j < N-1 and M[i][j+1] == 1 : nb += 1 print nb
File: td_note_2013_preparation.tex, line 329
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0] [0, 0, 0, 0, 0, 0, 0, 0, 1, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 1, 0, 1, 1] [0, 0, 0, 0, 1, 0, 1, 1, 0, 1] [0, 0, 1, 0, 1, 0, 0, 1, 1, 1] [0, 0, 0, 0, 0, 1, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 1, 0, 0, 0] [0, 1, 0, 0, 0, 1, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 46
File: td_note_2013_preparation.tex, line 345
[0, 1, 0] [0, 0, 1] [0, 0, 0] 4
File: td_note_2013_preparation.tex, line 391
import random, numpy N = 10 M = [ [ 1.0 if random.randint(1,5) == 1 else 0.0 for i in range (N) ] for j in range(N) ] M = numpy.matrix(M) MM = numpy.matrix(M) for i in range (N) : s = numpy.sum(M[i,:]) # ou M[i,:].sum() if s > 0 : MM [i,:] = M [i,:] / s print MM
File: td_note_2013_preparation.tex, line 406
import random, numpy N = 5 M = [ [ 1 if random.randint(1,5) == 1 else 0 for i in range (N) ] for j in range(N) ] M = numpy.matrix (M) MM = numpy.matrix(M) for i in range (N) : s = numpy.sum(M[i,:]) if s > 0 : MM [i,:] = M [i,:]*1.0 / s # multiplication par 1.0 print MM