import matplotlib.pyplot as plt import numpy as np import numpy.random as rd import numpy.linalg as al def markov_avec_matrice (n, M, x0) : X = np.zeros(n) X[0]= x0 for k in range(n-1) : X[k+1]= np.random.choice(a=list(range(len(M))), p=M[int(X[k]), :]) return X ## Évolution d'une maladie, Programmes à trous. #def Exemple(n): # X=np.zeros(n) # X[0]=1 # for k in range(n-1) : # if X[k] == 1 : # X[k+1] = np.random.choice(a=[1,2,3], p=[1/2,1/2,0]) # elif X[k] == 2 : # X[k+1] = np.random.choice(a=[1,2,3], p=[0,1/5,4/5]) # else : # X[k+1] = np.random.choice(a=[1,2,3], p=[1/10,0,9/10]) # return X #y=Exemple(100) # #plt.plot(y) #plt.show() #U=np.zeros(100) #for k in range (100): # U[k]=Exemple(1000)[999]; # # #frequ = np.zeros(3) #for j in range(3): # for k in range(len(U)): # if U[k] == j+1 : # frequ[j]=frequ[j]+1 # # #plt.bar([1,2,3],frequ, width=0.1) #plt.show() ### Question 3 # #A=np.array([[1/2,1/2,0],[0,1/5,4/5],[1/10,0,9/10]]) #B=al.matrix_power(A,1000) # #u0=[1,0,0] # #prob = np.dot(u0,B) #print(prob) # # # ### Question 4 # ## ## #def Example_bis(n): # return markov_avec_matrice(n,A,0) # #plt.plot(Example_bis(100)) def google(n): X=np.zeros(n) X[0]=rd.randint(1,4) for k in range(n-1) : if X[k] == 1 : X[k+1] = np.random.choice(a=[1,2,3], p=[1/3,1/3,1/3]) elif X[k] == 2 : X[k+1] = np.random.choice(a=[1,2,3], p=[1/7,5/7,1/7]) else : X[k+1] = np.random.choice(a=[1,2,3], p=[1/12,7/12,4/12]) return X #U=np.zeros(1000) #for k in range (1000): # U[k]=google(1000)[999]; # # #frequ = np.zeros(3) #for j in range(3): # for k in range(len(U)): # if U[k] == j+1 : # frequ[j]=frequ[j]+1 # # #plt.bar([1,2,3],frequ, width=0.1) def google2(n): X=np.zeros(n) X[0]=rd.randint(1,6) for k in range(n-1) : if X[k] == 1 : if rd.random()<=0.15: X[k+1]=rd.randint(1,6) else : X[k+1]=rd.choice(a=[2,3,5],p=[1/3,1/3,1/3]) elif X[k] == 2 : if rd.random()<=0.15: X[k+1]=rd.randint(1,6) else : X[k+1]=rd.choice(a=[1,4],p=[1/2,1/2]) elif X[k] == 3 : if rd.random()<=0.15: X[k+1]=rd.randint(1,6) else : X[k+1]=rd.choice(a=[2,5],p=[1/2,1/2]) elif X[k] == 4 : if rd.random()<=0.15: X[k+1]=rd.randint(1,6) else : X[k+1]=rd.choice(a=[2,5],p=[1/2,1/2]) else : if rd.random()<=0.15: X[k+1]=rd.randint(1,6) else : X[k+1]=2 return X U=np.zeros(1000) for k in range (1000): U[k]=google2(1000)[999]; frequ = np.zeros(5) for j in range(5): for k in range(len(U)): if U[k] == j+1 : frequ[j]=frequ[j]+1 plt.bar([1,2,3,4,5],frequ, width=0.1) plt.show()