You are on page 1of 11

RANDOM WALK IN 1-D AND 2-D

Andri Rahmadhani Department of Physics, Bandung Institute of Technology andrewflash@gmail.com

1. Theory 1.1 Random Walk in 1-D Random walk is a method or an algorithm that represents trajectory of random steps. Random walk is used in many fields including physics and economics. In physics, for example, we can simulate the Brownian motion of particles. In economics, we can simulate the behavior and analyze statistical properties of stock market. The direction of random walk cannot be predicted from the past actions. A simple example of one-dimensional random walk is the flipped coin problem. The two sides of coin, head and tails have their integer values that represent each step moves, such as +1 and -1 respectively. Each side has equal probability. We begin at 0 positions. If the coin lands on head (H), we move one step to the right. If the coin lands on tails (T), we move one step to the left.

Figure 1. One of possible random walks position after 5 flips

Lets talk about mathematical view of this random walk problem. Let , , be a random variable. In this example, the value of random variable is etiher 1 or -1 and each of them has equal probability which is 50%. Then, we define sum of n steps as with as . The expected value is zero. Expected value is the weighted average that the random variable can have. The expected value of random variable is given by: are the probability to be -1. Also is 0.5

The variables are the random variables and corresponded to the random variables. In this example, let to be 1 and and is 0.5. Therefore, the expected value is zero. Because

, we can simplify the expectation value formula,

The finite additivity property of expectation gives:

We can get expectation value of the squared value of random variable as follow: | that is the expected translation distance after

This value is useful for finding the magnitude | n steps. Therefore, | |

We can also draw the time-series graph of the value just like in the stock market graph.

Figure 2. Time-Series random walk with N (steps) = 1000 and equal probability 0.5

1.2 Random Walk in 2-D Random Walk method in 2-D is similar with 1-D. For general case, consider random direction is in the complex plane with range of [0, 2] and equal probability. Let to be random direction. Define the position in the complex plane after n steps such as which has absolute square (multiply with their conjugate) | |
( )

Therefore, | |
( )

The displacements and are random variables with identical means of zero. Their difference is also a random variable. If we take the mean of their difference (positive and negative values), the result will be 0, so | | | | This value is useful for finding root-mean-square distance after n steps. Therefore,

It is easy to understand the special case of 2-D random walk, such as the random walk in 2-D square lattice as shown below.

Figure 3. Two dimensional random walk in square lattice

2. Code We use Matlab software to simulate 1-D and 2-D random walk. 2.1 Random Walk in 1-D
clear; clc; X1 p1 X2 p2 = = = = 1; 0.5; -1; 0.5; % % % % First random variable Probability of the first random variable Second Random Variable Probability of the second random variable

Nj = 100; Ns = 1000;

% Number of Random Walk Iteration % Total steps

% Increasng steps for j=1:Ns % Iterate from N = 1 to Nj (Random Walk Number Iteration)

for k=1:Nj Sn(1) = 0; for i=1:j s = rand(1); if(s < p1) X = X2; else X = X1; end Sn(i+1) = Sn(i) end

% Starting Point % Generate Random Number % If the random number is less than p1, then % Move to the left (y-axis) % If not, move to the right (y-axis) + X; % Moves

% Plot sample Graph if(k==1 && j == Ns) figure(1); plot([0:Ns],Sn,'.-b','LineWidth',2); elseif(k==2 && j == Ns) hold on; plot([0:Ns],Sn,'.-r','LineWidth',2); elseif(k==3 && j == Ns) plot([0:Ns],Sn,'.-g','LineWidth',2); elseif(k==Nj-2 && j == Ns) plot([0:Ns],Sn,'.-y','LineWidth',2); elseif(k==Nj-1 && j == Ns) plot([0:Ns],Sn,'.-c','LineWidth',2); elseif(k==Nj && j == Ns) plot([0:Ns],Sn,'.-k','LineWidth',2); hold off; grid on; xlabel('Steps (N)'); ylabel('Value'); end % Calculating for distance Sfinal(k) = Sn(j+1); % Final Position for j steps Sfinalsqr(k) = Sn(j+1)*Sn(j+1); % Final Position square end Ssqravg(j) = mean(Sfinalsqr); Savg(j) = sqrt(Ssqravg(j)); end % Plot the distance graph figure(2); plot([1:Ns],Savg,'.-b','LineWidth',2); xlabel('Number of steps (N)'); ylabel('Distance <|Sn|>'); grid on; hold on; plot(sqrt(1:Ns),'--g','LineWidth',2); hold off; % Print Root-mean-square distance S = Savg' % Calculate mean

2.2 Random Walk in 2-D


clear; clc; X1 p1 X2 p2 Y1 q1 Y2 = = = = = = = 1; 0.5; -1; 0.5; 1; 0.5; -1; % % % % % % % First random variable (X-axis) Probability of the first random variable Second Random Variable (X-axis) Probability of the second random variable First random variable (Y-axis) Probability of the first random variable Second random variable (Y-axis)

q2 = 0.5; Nj = 10; Ns = 100;

% Probability of the second random variable % Number of Random Walk Iteration % Total steps

% Increasng steps for j=1:Ns % Iterate from N = 1 to Nj (Random Walk Number Iteration) for k=1:Nj Snx(1) = 0; % Starting Point Sny(1) = 0; for i=1:j s = rand(1,2); % Generate Random Number 2D if(s(1,1) < p1) % If the random number is less than p1, then if(s(1,2) < q1) X = X2; % Move to the left (x-axis) Y = 0; elseif(s(1,2) >= q1) X = X1; % Move to the right (x-axis) Y = 0; end elseif(s(1,1) >= p1) if(s(1,2) < q1) X = 0; Y = Y1; % Move to the top (y-axis) elseif(s(1,2) >= q1) X = 0; Y = Y2; % Move to the bottom (y-axis) end end Snx(i+1) = Snx(i) + X; % Moves Sny(i+1) = Sny(i) + Y; end % Plot sample Graph if(k==1 && j == Ns) figure(1); plot(Snx,Sny,'-k','LineWidth',4); hold on; plot(Snx(Ns+1),Sny(Ns+1),'.k','MarkerSize',40); elseif(k==2 && j == Ns) plot(Snx,Sny,'-r','LineWidth',3); plot(Snx(Ns+1),Sny(Ns+1),'.r','MarkerSize',50); elseif(k==Nj-1 && j == Ns) plot(Snx,Sny,'-b','LineWidth',2); plot(Snx(Ns+1),Sny(Ns+1),'.b','MarkerSize',50); elseif(k==Nj && j == Ns) plot(Snx,Sny,'-g','LineWidth',1); plot(Snx(Ns+1),Sny(Ns+1),'.g','MarkerSize',50); hold off; grid on; xlabel('x'); ylabel('y'); end % Calculating for distance Sfinalx(k) = Snx(j+1); % Final Position for j steps Sfinaly(k) = Sny(j+1); Sfinalsqrx(k) = Snx(j+1)*Snx(j+1); % Final Position square Sfinalsqry(k) = Sny(j+1)*Sny(j+1); end Ssqravgx(j) = mean(Sfinalsqrx); % Calculate mean Ssqravgy(j) = mean(Sfinalsqry); Savgx(j) = sqrt(Ssqravgx(j)); Savgy(j) = sqrt(Ssqravgy(j)); Savg(j) = sqrt((Savgx(j)*Savgx(j))+(Savgy(j)*Savgy(j))); end

% Plot the distance graph hold off; figure(2); plot([1:Ns],Savg,'.-b','LineWidth',2); xlabel('Number of steps (N)'); ylabel('Distance <|Sn|>'); grid on; hold on; plot(sqrt(1:Ns),'--g','LineWidth',2); hold off; % Print Root-mean-square distance S = Savg'

3. Result 3.1 Random Walk in 1-D


4
15

10

Value

Value

-2

-4

-5

-6

-10

-8

1 1 trial
st

2 2

3
nd

4 3
rd

5 Steps (N) trial

6 8 trial
th

7 9 trial
th

9 10 trial
th

10

-15

10 1st trial

20

30 2nd trial

40

50 Steps (N) 3rd trial

60 8th trial

70

80 9th trial

90 10th trial

100

trial

3.5 Distance Curve Fitting N1/2

14 Distance Curve Fitting N1/2

12 3 10

Distance <|Sn|>

Distance <|Sn|>

2.5

4 1.5 2

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 4. Value and distance for (a) 10 steps with 10 trials, (b) 100 steps with 10 trials

The distance graph then fitted with curve. The curve fitting equation for this graph is given by: In order to get curve, the value must be 1 (approximately close to 1) and (approximately close to 0). Using Matlab Curve Plotting Toolbox, we can get and as shown in table 1. value must be 0

15

10

5
1

0
Value

Value

-5
-1

-10
-2

-3

-15

-4

1 1st trial

3 2nd trial

5 Steps (N) 3rd trial

6 98th trial

8 99th trial

9 100th trial

10

-20

10 1st trial

20

30 2nd trial

40

50 Steps (N)

60 98th trial

70

80 99th trial

90 100th trial

100

3rd trial

3.5 Distance Curve Fitting N1/2

11 Distance 10 Curve Fitting N1/2

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 5. Value and distance for (a) 10 steps with 100 trials, (b) 100 steps with 100 trials
4
20

15

10

0
5

Value

-2

Value

-4
-5

-6

-10

-8

-15

1 1 trial
st

2 2
nd

3 trial

4 3
rd

5 Steps (N) trial


th

6 998 trial

7
th

8 999 trial

9
th

10

10 1st trial

20

30 2nd trial

40

50 Steps (N)

60 998th trial

70

80 999th trial

90

100

1000 trial

3rd trial

1000th trial

3.5 Distance Curve Fitting N1/2

11 Distance 10 Curve Fitting N1/2

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of step (N)

70

80

90

100

(a)

(b)

Figure 6. Value and distance for (a) 10 steps with 1000 trials, (b) 100 steps with 1000 trials

2 0

0 -5 -1
Value Value

-2 -10 -3

-4

-15

-5

-6

0
st

1 1 trial

2 2
nd

3 trial 3

4
rd

5 Steps (N) trial


th

7
th

8 9999 trial

9
th

10

-20

10 1 trial
st

20 2
nd

30 trial 3

40
rd

50 Steps (N)

60
th

70
th

80 9999 trial

90
th

100

9998 trial

10000 trial

trial

9998 trial

10000 trial

3.5 Distance Curve Fitting N1/2

11 Distance 10 Curve Fitting N1/2

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 7. Value and distance for (a) 10 steps with 10000 trials, (b) 100 steps with 10000 trials

Table 1. Curve fitting property for 10 steps 10 trials a b R


2

100 steps 1000 trials 0.9943 0.03825 0.9946 10 steps 10000 trials 1.003 -0.003868 0.9997 100 steps 10000 trials 1 0.006539 0.9995

100 steps 10 trials 0.9786 0.08314 0.7447

10 steps 100 trials 0.8612 0.1896 0.9554

100 steps 100 trials 0.9993 0.08167 0.9651

10 steps 1000 trials 1.006 -0.003658 0.9976

1.007 0.1199 0.8451

From table 1, we see that number of steps and trials affect the result. The more trials we have, the better result we can get. Increasing steps may affect the smoothness of the curve. More steps can make the distribution of final position of random walk become large. It means that the dispersion become large so the root-mean square value become affected too.

3.2 Random Walk in 2-D


1
15

0.5

10

-0.5

-1

-5

-1.5

-10

-2 -4 1 trial
st

-3

-2 End 2

-1
nd

0 x End

1 9 trial
th

2 End

4 10 trial
th

5 End

-15 -10 1st trial

-5 End 2nd trial

0 x End

5 9th trial End

10 10th trial

15 End

trial

4 Distance Curve Fitting N1/2

14 Distance Curve Fitting N1/2

12

3.5

10

3
Distance <|Sn|>
Distance <|Sn|>

2.5

2
4

1.5

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 8. Value and distance for (a) 10 steps with 10 trials, (b) 100 steps with 10 trials
3

0
2

-2

-4

-6
y
0

-8
-1

-10

-12
-2

-14

-3 -3

-2.5 1st trial

-2 End

-1.5 2nd trial

-1

-0.5 x End

0 99th trial

0.5 End

1.5 100th trial

2 End

-16 -10 1st trial

-8 End

-6 2nd trial

-4 x End

-2 99th trial

0 End

2 100th trial

4 End

3.5 Distance Curve Fitting N1/2

11 Distance 10 Curve Fitting N1/2

Distance <|Sn|>

Distance <|Sn|>
1 2 3 4 5 6 Number of steps (N) 7 8 9 10

2.5

1.5

2
1

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 9. Value and distance for (a) 10 steps with 100 trials, (b) 100 steps with 100 trials

15

2 10 1

0 5 -1
y

-2

-3 -5 -4

-5 -4 1st trial
3.5

-3

-2 End

-1 2nd trial

0 End

1 x

2 999th trial

3 End

5 1000th trial

6 End

-10 -14 1st trial


11

-12 End

-10

-8 2nd trial

-6 x End

-4 999th trial

-2 End

2 1000th trial

4 End

Distance

Curve Fitting N

1/2

Distance 10

Curve Fitting N

1/2

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 10. Value and distance for (a) 10 steps with 1000 trials, (b) 100 steps with 1000 trials

20

3 15 2 10 1

-1 0 -2 -5 -3

-4 -2 1 trial
st

-1.5 End

-1 2

-0.5
nd

0 End

0.5 x

1 9999 trial
th

1.5 End

2.5 10000 trial


th

3 End

-10 -6 1st trial

-4 End

-2 2nd trial End

0 x 9999th trial

2 End

4 10000th trial

6 End

trial

3.5 Distance Curve Fitting N1/2

12 Distance Curve Fitting N1/2

10

8
Distance <|Sn|> Distance <|Sn|>

2.5

1.5
2

5 6 Number of steps (N)

10

10

20

30

40 50 60 Number of steps (N)

70

80

90

100

(a)

(b)

Figure 11. Value and distance for (a) 10 steps with 10000 trials, (b) 100 steps with 10000 trials

Table 2. Curve fitting property for 10 steps 10 trials a b R


2

100 steps 1000 trials 1.002 -0.0141 0.9975 10 steps 10000 trials 0.9961 0.009183 0.9998 100 steps 10000 trials 1 -0.002094 0.9998

100 steps 10 trials 1.002 -0.1072 0.7645

10 steps 100 trials 1.01 0.003024 0.9865

100 steps 100 trials 1.019 -0.1196 0.9782

10 steps 1000 trials 1.004 0.007713 0.9975

1.168 -0.3342 0.8919

From that result we can conclude that root-mean-square of random walk final position or the distance is equal with root-square of number of random walk steps. This statement is satisfied for large number of random walks trials.

References : [1] Pearson, K. (1905). The problem of the Random Walk. Nature. 72, 294. [2] Van Kampen N. G., Stochastic Processes in Physics and Chemistry, revised and enlarged edition (North-Holland, Amsterdam) 1992. [3] McCrea, W. H. and Whipple, F. J. W. "Random Paths in Two and Three Dimensions." Proc. Roy. Soc. Edinburgh 60, 281-298, 1940.