Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. import numpy as np
  2. import random
  3.  
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6.  
  7. #PART A
  8. def nextStepEhr(X,M):
  9.     U = random.randint(0,1)
  10.     V = random.uniform(0,1)
  11.     Y=0
  12.    
  13.     if U == 0:
  14.         Y = X
  15.         return Y
  16.     elif U==1 and V <= (X/M):
  17.         Y = X-1
  18.         return Y
  19.     else:
  20.         Y = X+1
  21.         return Y
  22.     return
  23.  
  24. #PART C
  25. def matrixEhr(M):
  26.     T = []
  27.     for r in range(0,(M+1)):
  28.         if r==0:
  29.             P = np.zeros((M+1))
  30.             P[r] = 0.5
  31.             P[r+1]= 0.5
  32.             T.append(P)
  33.         elif r==(M):
  34.             P = np.zeros((M+1))
  35.             P[r] = 0.5
  36.             P[r-1] = 0.5
  37.             T.append(P)
  38.         else:
  39.             P = np.zeros((M+1))
  40.             P[r] = 0.5
  41.             P[r-1] = r/(2*(M))
  42.             P[r+1] = ((M)-r)/(2*(M))
  43.             T.append(P)
  44.     return T
  45.  
  46. #PART B
  47. X=0
  48. states = np.arange(0,1001)
  49. statecount = np.zeros(1001)
  50. for a in range(0,10000):
  51.     X = nextStepEhr(X,1000)
  52.  
  53.  
  54. for a in range(0,60000):
  55.     X = nextStepEhr(X,1000)
  56.     statecount[X] += 1
  57.  
  58. plt.bar(states, statecount,align='center', alpha=0.5)
  59. plt.ylabel('Time in state i')
  60. plt.xlabel('State')
  61. plt.title('Lazy Ehrenfest Coin Chain')
  62.  
  63.  
  64. #PART C
  65. mu = np.zeros(21)
  66. mu[0] = 1
  67.  
  68. v = np.zeros(21)
  69. v[0] = 0.5
  70. v[20] = 0.5
  71. k = np.arange(1,61)
  72.  
  73. F = np.zeros((60,21))
  74. F_2 = np.zeros((60,21))
  75. A = matrixEhr(20)
  76. for x in k:
  77.     F[x-1,:] = mu@(np.linalg.matrix_power(A, x))
  78.     F_2[x-1,:] = v@(np.linalg.matrix_power(A, x))
  79.  
  80. x= np.arange(1,22)
  81. X,Y = np.meshgrid(x,k)
  82.  
  83. fig = plt.figure()
  84. ax = fig.add_subplot(111, projection='3d')
  85. fig_2 = plt.figure()
  86. ax_2 = fig_2.add_subplot(111, projection='3d')
  87. ax.plot_wireframe(X,Y,F)
  88. ax.view_init(30,100)
  89. ax.set_title('F for μ')
  90. ax.set_xlabel('States')
  91. ax.set_ylabel('Time')
  92. ax.set_zlabel('F')
  93.  
  94. ax_2.plot_wireframe(X,Y,F_2)
  95. ax_2.view_init(30,100)
  96. ax_2.set_title('F for v')
  97. ax_2.set_xlabel('States')
  98. ax_2.set_ylabel('Time')
  99. ax_2.set_zlabel('F')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement