CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign UpSign In
Views: 9
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204
Embed | Download | Raw |
Kernel: Python 3 (system-wide)

Section Two: Developing the Random Walk Tools

code needs to be documented. Taken from

%pylab inline # Pylab is a module that provides a Matlab like namespace by importing functions from the modules Numpy and Matplotlib. # %matplotlib inline turns on “inline plotting”, where plot graphics will appear in your notebook. from itertools import cycle # This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. # see <> from mpl_toolkits.mplot3d import Axes3D # see <> colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk') # see cycle description above. import numpy as np
Populating the interactive namespace from numpy and matplotlib
# Define parameters for the walk dims = 1 # in this case the walk is either forward or backward step_n = 10000 # There will be 10,000 steps step_set = [-1, 0, 1] # create an array of one step forward or one step backward, or no step origin = np.zeros((1,dims)) # create an array of zeros print(origin.shape)
(1, 1)
# Simulate steps in 1D step_shape = (step_n,dims) # step-shape array created with dimensions of (10,000, 1) print(type(step_shape)) steps = np.random.choice(a=step_set, size=step_shape) # a random sample is chosen from [step_set] and step_n * path = np.concatenate([origin, steps]).cumsum(0) start = path[:1] stop = path[-1:]
<class 'tuple'>
# Plot the path fig = plt.figure(figsize=(8,4),dpi=200) # create the plot frame ax = fig.add_subplot(111) # create one subplot ax.scatter(np.arange(step_n+1), path, c='blue',alpha=0.25,s=0.05); # plot the scatter diagram of the path data and the array index ax.plot(path,c='blue',alpha=0.5,lw=0.5,ls='-',); # plot the lines between the scatter points ax.plot(0, start, c='red', marker='+') # plot the start point ax.plot(step_n, stop, c='black', marker='o') # plot the stop point plt.title('1D Random Walk') # label the plot plt.tight_layout(pad=0) #plt.savefig('plots/random_walk_1d.png',dpi=250);
Image in a Jupyter notebook
# Define parameters for the walk for a two dimensional walk the same as the one dimensional dims = 2 step_n = 10000 step_set = [-1, 0, 1] origin = np.zeros((1,dims))
# Simulate steps in 2D step_shape = (step_n,dims) steps = np.random.choice(a=step_set, size=step_shape) # calculate the next step #print(steps) path = np.concatenate([origin, steps]).cumsum(0) # calculate the next position #print(path) start = path[:1] stop = path[-1:]
# Plot the path fig = plt.figure(figsize=(8,8),dpi=200) ax = fig.add_subplot(111) ax.scatter(path[:,0], path[:,1],c='blue',alpha=0.25,s=0.05); # here the scatter diagram shows the two dimensional position. ax.plot(path[:,0], path[:,1],c='blue',alpha=0.5,lw=0.25,ls='-'); # and here the dots are connected ax.plot(start[:,0], start[:,1],c='red', marker='+') ax.plot(stop[:,0], stop[:,1],c='black', marker='o') plt.title('2D Random Walk') plt.tight_layout(pad=0) #plt.savefig(‘plots/random_walk_2d.png’,dpi=250);