Random Walks in 2-D, 3-D



Roland Spiek
Physics 305 Computational Physics
2/13/2006

1. Introduction

We will look at two different programs involving random walks. In the first case we will consider an unconstrained 2-dimensional random walk which can move in any direction at each step, up to a maximum step length. This is just one of an infinite variety of random walk possibilities, many of which have interesting physics applications. [1]

Here are some of the possible applications: [2]

       In all these cases, random walk is often substituted for Brownian motion.

In mathematics and physics, a random walk is a formalization of the intuitive idea of taking successive steps, each in a random direction. [2]

Random walk can also be looked at as a Markov chain whose state space is given by the integers i=0,\pm 1,\pm 2,..., for some number 0 < p < 1, Pi,i + 1 = p = 1 − Pi,i − 1. We can call it a random walk because we may think of it as being a model for an individual walking on a straight line who at each point of time either takes one step to the right with probability p or one step to the left with probability 1 − p. [2]

A random walk is a simple stochastic process. [2]

A random walk is sometimes called a "drunkard's walk". Drunkard's Walk is also the name of a 1960 science fiction novel by Frederik Pohl. [2]

Assume now that our city is no longer orderly. When our drunkard reaches a certain junction he picks between the various available roads with equal probability. Thus, if the junction has seven exits the drunkard will go to each one with probability one seventh. This is a random walk on a graph. Will our drunkard reach his home? It turns out that under rather mild conditions, the answer is still yes. For example, if the lengths of all the blocks are between a and b (where a and b are any two finite positive numbers), then the drunkard will, almost surely, reach his home. Notice that we do not assume that the graph is planar, i.e. the city may contain tunnels and bridges. One way to prove this result is using the connection to electrical networks. Take a map of the city and place a one ohm resistor on every block. Now measure the "resistance between a point and infinity". In other words, choose some number R and take all the points in the electrical network with distance bigger than R from our point and wire them together. This is now a finite electrical network and we may measure the resistance from our point to the wired points. Take R to infinity. The limit is called the resistance between a point and infinity. It turns out that the following is true: [2]

Theorem: a graph is transient if and only if the resistance between a point and infinity is finite. It is not important which point is chosen. [2]

In other words, in a transient system, one only needs to overcome a finite resistance to get to infinity from any point. In a recurrent system, the resistance from any point to infinity is infinite. [2]

It also turns out that this characterization of recurrence and transience is very useful, and specifically it allows to analyze the case of a city drawn in the plane with the distances bounded. [2]

A random walk on a graph is a very special case of a Markov chain. Unlike a general Markov chain, random walk on a graph enjoys a property called time symmetry or reversibility. Roughly speaking, this property, also called the principle of detailed balance, means that the probabilities to traverse a given path in one direction or in the other have a very simple connection between them (if the graph is regular, they are just equal). It turns out that this property has important consequences. [2]

Starting from the 80s, much research has gone into connecting properties of the graph to random walks. In addition to the electrical network connection described above, there are important connections to isoperimetric inequalities, see more here, functional inequalities such as Sobolev and Poincaré inequalities and properties of solutions of Laplace's equation. A significant portion of this research was focused on Cayley graphs of finitely generated groups. For example, the proof of Persi Diaconis that 7 riffle shuffles are enough to mix a pack of cards (see more details under shuffle) is in effect a result about random walk on the group Sn, and the proof uses the group structure in an essential way. In many cases these discrete results carry over to, or are derived from Manifolds and Lie groups. [2]

 

2. A least-squares fit to the random walk distance use Gnuplot


The graph below depicts a sample 2-D random walk. This is a template from reference [1] that is used as a check or template for the following graphs. From this graph it is easy to see why some people call this a drunkard's walk, the lines are connected but there is no apparent trend, it just starts somewhere and travels randomly... Note a drunken man has to be really messed up, and apparently he falls at the last plotted point.

The graph below depicts the same picture given on the left hand side of figure 6.7 in Computational Physics: Problem Solving with Computers by Rubin Landau and Manuel Paez . This is not part of the assignment but it is neccessary for proving that I did the full lecture directions, and it produced a nice graph. I should be noted that this graph has a standard y axis and x axis.

The graph below represents the right hand side of figure 6.7 in Computational Physics: Problem Solving with Computers by Rubin Landau and Manuel Paez.

The graph below is part of the assignment. This graph shows the least-squares fit of the distance vs. number of steps data from walk.c to a model y = f(x) = xp. This model is also known as a "power law" because it expresses the ordinate (y-axis) value as a function of the abscissa (x-axis) raised to some exponent--we encountered such models already in the lab on cosmic ray models. In this case the exponent is a parameter that will be determined by the fit, but we have an expectation that it should be p= 0.5, since theory says that 2-dim random walks should have a distance that depends on the sqrt(number of steps). [1]

The graph shows a thick tornado looking phenomenon constructed by the y error bars that are graphed. The centerline is the best fit curve for the data. The x axis is the square root of the x-value. Note this graph is not a log-log plot, that graph is plotted later in the report, for now these are linear plots with the x axis having the square root of the x-value. The gnuplot code is provided at the end of the linear graph section, it relates to the next four graphs.  

The graph below shows the change when the walk.c program has new values of Ntrials and max, and recompiled. The new values of Ntrials for this graph is 10, and new value of max is set at 10000. The reduced number of averages produces a graph which has greater deviations in the y error bars. The graph becomes more non linear and shows the even around x=60 the error bars don't even touch the fitted line.

The graph below shows the new values for max, Ntrials stays the same from the previous graph. The new value of  is  100000. This seems to show about the same plot as our first origional plot, only more plotted points. The error shows to have the same y error bar deviation.

The next graph produces another plot with new Ntrials and max in the program walk.c. The new values for Ntrials is 4 and max is set at 1000. From below it is clear that the graph deviates greatly from the origional plot. the y error bars are very long and show that the precision decreased. Also at around x=10 the fitted line doesn't even come close to touching the y error bars below it. This graph looks like a horizontal tornado. I would expect to see a plot that shows a linear relationship between endpoints if we were to modify our program to output only the endpoints of each trial. This would show the a plot of roughly the fitted curve y = f(x) = xp. Below this graph is the gnuplot program script which runs these graphs. Note that in Gnuplot we are using the function f(x) = x**p, ** represents the power symbol. Also the fit command, " fit f(x) 'walk.dat' using (sqrt($1)):2 via p" is used obviously for the fitting function, and the very last statement says to plot f(x), these are very important commands to use for fitting lines- you would think that fitting a curve is hard but all you need is the intuition as to what function you should estimate the curve at. Data is displayed below the Gnuplot code, this is the last 17 data points in the walk.dat file for this graph below.

# Gnuplot script file for fitting and plotting linear data
# This file is called walk.plt
set title ""   # Least-squares fit 2-D random walk
set xlabel "x values"
set ylabel "y=f(x)"
set autoscale x
f(x) = x**p;

fit f(x) 'walk.dat' using (sqrt($1)):2 via p   # column three is for std errors
set pointsize 1.5
set bar small
plot    "walk.dat" using (sqrt($1)):2:3 title 'Linear data' with yerrorbars, \
 f(x)

9983    9.701822e+01    9.701822e+00
9984    9.718578e+01    9.718578e+00
9985    9.722719e+01    9.722719e+00
9986    9.720248e+01    9.720248e+00
9987    9.739596e+01    9.739596e+00
9988    9.735747e+01    9.735747e+00
9989    9.761587e+01    9.761587e+00
9990    9.762328e+01    9.762328e+00
9991    9.770817e+01    9.770817e+00
9992    9.779851e+01    9.779851e+00
9993    9.783785e+01    9.783785e+00
9994    9.782462e+01    9.782462e+00
9995    9.785699e+01    9.785699e+00
9996    9.798136e+01    9.798136e+00
9997    9.801361e+01    9.801361e+00
9998    9.791385e+01    9.791385e+00
9999    9.798023e+01    9.798023e+00
10000    9.794811e+01    9.794811e+00



The graph below shows the log-log graph of the random walk. It should be noted that a power law model produces a linear slope in a log-log plot as shown below. The function plotted is in the form of log(f(x)) = p log(x). The Gnuplot and data is located after this graph. Notice that everything in the Gnuplot is pretty much the same except the equations for the x-axis and y-axis where we just did the logarithm of column 1 and 2 in the walk.dat file.

If we use the variable substitution y --> y' and x--> y'  with y'= log(f(x)) and x' = log(x), then the form of this equation becomes that of a straight line with slope p, eg.: y' = px'. For this reason, power law models are often represented with log-log plots so that the linearized aspects of the model can be easily displayed. Generate graphs for both cases in this assignment. [1]

# Gnuplot script file for fitting and plotting linear data
# This file is called walk.plt
set title ""   # Least-squares fit 2-D random walk
set xlabel "x values"
set ylabel "y=f(x)"
set autoscale x
f(x) = x**p;

fit f(x) 'walk.dat' using (log((sqrt($1)))):(log($2)) via p   # column three is for std errors
set pointsize 1.8
set bar small
plot    "walk.dat" using (log((sqrt($1)))):(log($2)) title 'Linear data' with linespoints, \
 f(x)


   


3. 3-dimensional Random Walks

In the second part of this assignment we were asked to do the same thing in section 2 but with a 3-D plot. In the 2-D walk.c program we find the algorithim that generates the unit step is "srand48(seed);", this command will help to seed the number generator, and if the seed is the same the randoms will also be the same. The step follows:  "for (i=0; i<=Nmax; i++) r[i]=0.0;" will clears the array, and "for (j=1; j<=(int)Ntrials; j++)" will average over Ntrials trials. The new program produced called walk3d.c extends the 2-D program for useage in 3-D. The program allows us to input the two boundary conditions; first is Ntrials, second is Nmax, which is the same as in the 2-D program. To do the same proceedure as in part two, the 3 different graphs with different values of max and Ntrials we need to create 4 different output data files. They are called "walkpathA.dat", "walkpathB.dat", "walkpathC.dat", "walkpathD.dat", and are shown in the 3-D graph below. Each color represents a different data file. The green represents the program walk3d.c max of 20000 and Ntrials of 100 compiled and run. The purple represents the Ntrials of 40 and max of 1000. The red indicates the Ntrials of 10 and max of 10000. The blue indicates the Ntrials of 5 and max of 1000. The Gnuplot code is also given below the two graphs. The second graph  is just a second view of the first, it is to show the height differences between the seperate data files. The Gnuplot was origionally taken from source [1] and it was modified for this set of data.


# Gnuplot script file for plotting data
# This file is called walk3D.plt

set title "3-dimensional Random Walks"
set xlabel "x values"
set ylabel "y=f(x)"
set zlabel 'Z, steps'

set ticslevel  0
set autoscale x
set origin 0,0
set size 1

set view 5,15
set pointsize 0.95

set nokey
splot "walkpathA.dat" using 2:3:4 with lines,\
      "walkpathB.dat" using 2:3:4 with lines,\
      "walkpathC.dat" using 2:3:4 with lines,\
      "walkpathD.dat" using 2:3:4 with lines
    


4. Conclusions

  From the looks of both 2-D and 3-D plots it is evedent that the given Ntrials and max do change the accuracy and shape of the curve and thus change the fitted function curve. The more Ntrials and max we have the more accurate out observations are. From the 3-D plot we can see that the plotted points are indeed random in x, y, z. But it seems that at a given x, y, z component the computer seems not to vary too far from the first plotted point. This is because the functions in both porgrams are allowed to compute random numbers in a predetermined range, without this limitation we might not see the drunkard's walk so easily. I would like to note that the log-log plot of the 2-D plot is nice since it is linear and shows a low y error bar reading.

References

[1] Gorham, Prof. Peter. Random Walks I. 7 Feb. 2006. UHM. 14 Feb. 2006 <http://www.phys.hawaii.edu/~gorham/P305/RandomWalks.html>.

[2]  "Random walk." Wikipedia, The Free Encyclopedia. 10 Feb 2006, 01:25 UTC. 14 Feb 2006, 09:32 <http://en.wikipedia.org/w/index.php?title=Random_walk&oldid=39001249>.

[3] , Robin Lanau and Manuel Paez. Computational Physics: Problem Solving with Computers . 1st ed. : , .