Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 59 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. from numpy.random import random as rng # import the rng, random number generator
  2.  
  3. import numpy as np # making math functions available
  4. from pylab import * # get all pylab functions
  5.  
  6. num_steps = 1000 # number of steps per walk
  7. num_walks = 100000 # total number of walks
  8.  
  9. x_final = np.empty(num_walks) # an empty list, will store the final x positions
  10. y_final = np.empty(num_walks) # an empty list, will store the final y positions
  11. displacement = np.empty(num_walks) # an empty list, will store the distance from the origin of the walk
  12.  
  13. for i in range(num_walks): # create four plots
  14.    
  15.     samples1 = (rng(num_steps) < 0.5)  # create list of random values for sample 1
  16.     samples2 = (rng(num_steps) < 0.5)  # create list of random values for sample 2
  17.    
  18.     # convert random values into 1s and -1s  
  19.     x_steps = np.cumsum(2 * samples1 - 1)
  20.     y_steps = np.cumsum(2 * samples2 - 1)
  21.    
  22.     # add the final x, y positions to the list
  23.     x_final[i] = x_steps[-1]
  24.     y_final[i] = y_steps[-1]
  25.    
  26.     # add the displacement of this walk to the list of all displacements
  27.     displacement[i] = np.sqrt(x_steps[-1] ** 2 + y_steps[-1] ** 2)
  28.    
  29.  
  30. # scatter plot of final positions of all the random walks
  31. fig = plt.figure()
  32. axes = fig.gca()
  33.  
  34. axes.set_xlim([-150,150]) # set domain to -150 to 150
  35. axes.set_ylim([-150,150]) # set range to -150 to 150
  36. axes.set_aspect('equal') # set aspect ratio of graph to equal width and height
  37. grid(True) # include the cartesian dotted grid on the graph
  38.  
  39. # add the title, x, and y labels
  40. title("Location After 1000 Steps")
  41. xlabel("$\Delta$x")
  42. ylabel("$\Delta$y")
  43.  
  44. scatter(x_final[:1000], y_final[:1000]) # plot first 1,000 random walks
  45.  
  46. # begin a new figure for the displacement chart
  47. figure()
  48.  
  49. # set the x, y, and title labels
  50. xlabel("$\sqrt{\Delta x^2 + \Delta y^2}$")
  51. ylabel("Number of Walks")
  52. title("Displacement After 1000 Steps")
  53. hist(displacement, bins=25)
  54. print(mean(displacement))
  55. print(median(displacement))
  56.  
  57. # begin a new figure for the square displacement chart
  58. figure()
  59.  
  60. # set the x, y, and title labels
  61. xlabel("$\Delta x^2 + \Delta y^2$")
  62. ylabel("Number of Walks")
  63. title("Square Displacement After 1000 Steps")
  64.  
  65. n, bins, patches = hist(displacement ** 2, bins=25) # plot the histogram, and get the bins from it
  66. bins_mean = [0.5 * (bins[i] + bins[i+1]) for i in range(len(n))] # get the middle of the histogram bins
  67.  
  68. # begin a new figure for the logarithmic square displacement chart
  69. fig = figure()
  70.  
  71. # set the x, y, and title labels
  72. xlabel("$\Delta x^2 + \Delta y^2$")
  73. ylabel("Number of Walks")
  74. title("Square Displacement After 1000 Steps")
  75.  
  76. logged = np.log(n) # take the log of the counts
  77.  
  78. semilogy(bins_mean, n, "+", color='red', markersize=12, mew=3, ms=20) # plot the log scatter plot
  79.  
  80. z = np.polyfit(bins_mean, logged, 1) # get the slope and y-intercept
  81. p = np.poly1d(z)(bins_mean) # convert slope and y-intercept to y-values
  82.  
  83. semilogy(bins_mean, exp(p)) # plot the line of best fit, with log scale
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top