Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
18 views
unlisted
ubuntu2204
Kernel: Python 3 (system-wide)

Section Two: Developing the Random Walk Tools

code needs to be documented. Taken from https://towardsdatascience.com/random-walks-with-python-8420981bc4bc

%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 <https://docs.python.org/3/library/itertools.html#itertools.cycle> from mpl_toolkits.mplot3d import Axes3D # see <https://matplotlib.org/stable/api/toolkits/mplot3d.html> 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);
Image in a Jupyter notebook
# Define parameters for the walk in three dimenions dims = 3 step_n = 1000 step_set = [-1, 0, 1] origin = np.zeros((1,dims))
# Simulate steps in 3D step_shape = (step_n,dims) steps = np.random.choice(a=step_set, size=step_shape) path = np.concatenate([origin, steps]).cumsum(0) start = path[:1] stop = path[-1:]
# Plot the path fig = plt.figure(figsize=(10,10),dpi=200) ax = fig.add_subplot(111, projection='3d') ax.grid(False) ax.xaxis.pane.fill = ax.yaxis.pane.fill = ax.zaxis.pane.fill = False ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.scatter3D(path[:,0], path[:,1], path[:,2], c='blue', alpha=0.25,s=1) ax.plot3D(path[:,0], path[:,1], path[:,2], c='blue', alpha=0.5, lw=0.5) ax.plot3D(start[:,0], start[:,1], start[:,2], c='red', marker='+') ax.plot3D(stop[:,0], stop[:,1], stop[:,2], c='black', marker='o') plt.title('3D Random Walk') #plt.savefig(‘plots/random_walk_3d.png’,dpi=250);
Text(0.5, 0.92, '3D Random Walk')
Image in a Jupyter notebook
# Define parameters for the walk for ten walks of 1000 steps in 3 dimensions dims = 3 n_runs = 10 step_n = 1000 step_set = [-1, 0 ,1] runs = np.arange(n_runs) step_shape = (step_n,dims)
# Plot fig = plt.figure(figsize=(10,10),dpi=250) ax = fig.add_subplot(111, projection='3d') ax.grid(False) ax.xaxis.pane.fill = ax.yaxis.pane.fill = ax.zaxis.pane.fill = False ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Y') for i, col in zip(runs, colors): # Simulate steps in 3D origin = np.random.randint(low=-10,high=10,size=(1,dims)) steps = np.random.choice(a=step_set, size=step_shape) path = np.concatenate([origin, steps]).cumsum(0) start = path[:1] stop = path[-1:] # Plot the path ax.scatter3D(path[:,0], path[:,1], path[:,2],c=col,alpha=0.15,s=1); ax.plot3D(path[:,0], path[:,1], path[:,2], c=col, alpha=0.25,lw=0.25) ax.plot3D(start[:,0], start[:,1], start[:,2],c=col, marker='+') ax.plot3D(stop[:,0], stop[:,1], stop[:,2],c=col, marker='o'); plt.title('3D Random Walk - Multiple runs') #plt.savefig(‘plots/random_walk_3d_multiple_runs.png’,dpi=250);
Text(0.5, 0.92, '3D Random Walk - Multiple runs')