RANDOM WALK IN 1-D AND 2-D Andri Rahmadhani Department of Physics, Bandung Institute of Technology
[email protected]
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
∑ 〈〉 〈〉 〈 〉 〈〉
, Let’s 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:
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
are the probability to be -1. Also is 0.5
, 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:
This value is useful for finding the magnitude n steps. Therefore,
that is the expected translation distance after
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;
Nj = 100; Ns = 1000;
% % % %
First random variable Probability of the first random variable Second Random Variable 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 Sn(1) = 0; for i=1:j s = rand(1); if(s 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 if (k==1 && j == Ns) figure(1); plot([0:Ns],Sn,'.-b' plot([0:Ns],Sn, '.-b', ,'LineWidth' 'LineWidth',2); ,2); elseif(k==2 elseif (k==2 && j == Ns) hold on on; ; plot([0:Ns],Sn,'.-r' plot([0:Ns],Sn, '.-r', ,'LineWidth' 'LineWidth',2); ,2); elseif(k==3 elseif (k==3 && j == Ns) plot([0:Ns],Sn,'.-g' plot([0:Ns],Sn, '.-g', ,'LineWidth' 'LineWidth',2); ,2); elseif(k==Nj-2 elseif (k==Nj-2 && j == Ns) plot([0:Ns],Sn,'.-y' plot([0:Ns],Sn, '.-y', ,'LineWidth' 'LineWidth',2); ,2); elseif(k==Nj-1 elseif (k==Nj-1 && j == Ns) plot([0:Ns],Sn,'.-c' plot([0:Ns],Sn, '.-c', ,'LineWidth' 'LineWidth',2); ,2); elseif(k==Nj elseif (k==Nj && j == Ns) plot([0:Ns],Sn,'.-k' plot([0:Ns],Sn, '.-k', ,'LineWidth' 'LineWidth',2); ,2); hold off off; ; grid on on; ; xlabel('Steps xlabel( 'Steps (N)'); (N)' ); ylabel('Value' 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));
% Calculate mean
end % Plot the distance graph figure(2); plot([1:Ns],Savg,'.-b' plot([1:Ns],Savg, '.-b', ,'LineWidth' 'LineWidth',2); ,2); xlabel('Number xlabel('Number of steps (N)' ); ylabel('Distance ylabel('Distance <|Sn|>' ); grid on on; ; hold on on; ; plot(sqrt(1:Ns),'--g' plot(sqrt(1:Ns), '--g', ,'LineWidth' 'LineWidth',2); ,2); hold off off; ; % Print Root-mean-square distance S = Savg'
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) if (s(1,1) < p1) % If the random number is less than p1, then if(s(1,2) if (s(1,2) < q1) X = X2; % Move to the left (x-axis) Y = 0; elseif(s(1,2) elseif (s(1,2) >= q1) X = X1; % Move to the right (x-axis) Y = 0; end elseif(s(1,1) elseif (s(1,1) >= p1) if(s(1,2) if (s(1,2) < q1) X = 0; Y = Y1; % Move to the top (y-axis) elseif(s(1,2) 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 if (k==1 && j == Ns) figure(1); plot(Snx,Sny,'-k' plot(Snx,Sny, '-k', ,'LineWidth' 'LineWidth',4); ,4); hold on on; ; plot(Snx(Ns+1),Sny(Ns+1), '.k' '.k', ,'MarkerSize' 'MarkerSize',40); ,40); elseif(k==2 elseif (k==2 && j == Ns) plot(Snx,Sny,'-r' plot(Snx,Sny, '-r', ,'LineWidth' 'LineWidth',3); ,3); plot(Snx(Ns+1),Sny(Ns+1), '.r' '.r', ,'MarkerSize' 'MarkerSize',50); ,50); elseif(k==Nj-1 elseif (k==Nj-1 && j == Ns) plot(Snx,Sny,'-b' plot(Snx,Sny, '-b', ,'LineWidth' 'LineWidth',2); ,2); plot(Snx(Ns+1),Sny(Ns+1), '.b' '.b', ,'MarkerSize' 'MarkerSize',50); ,50); elseif(k==Nj elseif (k==Nj && j == Ns) plot(Snx,Sny,'-g' plot(Snx,Sny, '-g', ,'LineWidth' 'LineWidth',1); ,1); plot(Snx(Ns+1),Sny(Ns+1), '.g' '.g', ,'MarkerSize' 'MarkerSize',50); ,50); hold off off; ; grid on on; ; xlabel('x' xlabel('x'); ); ylabel('y' 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 off; ; figure(2); plot([1:Ns],Savg,'.-b' plot([1:Ns],Savg, '.-b', ,'LineWidth' 'LineWidth',2); ,2); xlabel('Number xlabel('Number of steps (N)' ); ylabel('Distance ylabel('Distance <|Sn|>' ); grid on on; ; hold on on; ; plot(sqrt(1:Ns),'--g' plot(sqrt(1:Ns), '--g', ,'LineWidth' 'LineWidth',2); ,2); hold off off; ; % Print Root-mean-square distance S = Savg'
3. Result 3.1 Random Walk in 1-D
e u l a V
4
15
2
10
0
5
e u l a V
-2
0
-4
-5
-6
-10
-8
0
1
2
3
st
nd
1 t rial
2
4
t rial
5 Steps (N) rd
3
t rial
6
7
th
8 th
8 t rial
9
t rial
9
10 10
-15 0
10
20 st
th
1
10 trial
3.5
30 nd
t rial
2
t rial
40
50 Steps (N) rd
3
t rial
60
70
th
80 th
8 t rial
9 t rial
90 10
th
100 trial
14 Curve Fitting N1/2
Distance
Curve Fitting N1/2
Distance
12 3 10
> | n S | < e c n a t s i D
2.5
> | n S | < e c n a t s i D
2
8
6
4 1.5 2
1
1
2
3
4
5 6 Number of steps (N)
7
8
9
10
0
0
(a)
10
20
30
40 50 60 Number of steps (N)
70
80
90
100
(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 by:
√
curve. The curve fitting equation for this graph is given
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
4
15
3
10
2
5
1 0 e u l a V
e u l a V
0
-5 -1 -10 -2
-15
-3
-4
0
1
2
3
st
nd
1 tri al
2
4
5 Steps (N) rd
trial
3
t rial
6
7
th
8
9
th
98 trial
99 tri al
100
-20 0
10 10 th
10
20 st
trial
1 t rial
3.5
30 nd
2
40
50 Steps (N) rd
t rial
3
trial
60
70
th
80 th
98 t rial
99
trial
90
100 th
100
trial
11 Curve Fitting N 1/2
Distance
Curve Fitting N 1/2
Distance 10
3
9
8
> | n S | < e c n a t s i D
2.5
7
> | n S | < e c n a t s i D
2
6
5
4
1.5
3
2
1
1
2
3
4
5 6 Number of steps (N)
7
8
9
1
10
0
10
20
30
40 50 60 Number of steps (N)
(a)
70
80
90
100
90
100
(b)
Figure 5. Value and distance for (a) 10 steps with 100 trials, (b) 100 steps with 100 trials 4
20
15
2
10 0 5 e u l a V
e u l -2 a V
0 -4 -5
-6
-8
-10
0
1
2
1st t ri al
3
4
2nd trial
5 Steps (N)
3rd t ri al
6
7
998th t ri al
8
9
999th t ri al
10 10
-15 0
10
20
1st t rial
1000th trial
30 2nd trial
40
50 Steps (N)
3rd trial
60
70
998th trial
80 999th trial
1000th trial
11
3.5 Curve Fitting N 1/2
Distance
Curve Fitting N 1/2
Distance 10
9
3
8
> | n S | < e c n a t s i D
2.5
> | n S | < e c n a t s i D
2
7
6
5
4
3
1.5
2
1
1
2
3
4
5 6 Number of steps (N)
(a)
7
8
9
10
1
0
10
20
30
40 50 60 Number of step (N)
70
80
(b)
Figure 6. Value and distance for (a) 10 steps with 1000 trials, (b) 100 steps with 1000 trials
90
100
3
5
2 0
1
0 -5 -1 e u l a V
e u l a V
-2
-10 -3
-4
-15
-5
-6
0
1
2
1st t rial
3
4
2nd t rial
5 Steps (N)
3rd t rial
6
7
9998th t rial
8
9
9999th t rial
-20
10
0
10000 th trial
10
20
1st t rial
3.5
30 2nd t rial
40
50 Steps (N)
3rd t rial
60
70
9998th t rial
80 9999th t rial
90
100
10000 th trial
11 Curve Fitting N1/2
Distance
Curve Fitting N1/2
Distance 10
3
9
8
> | n S | < e c n a t s i D
2.5
> | n S | < e c n a t s i D
2
7
6
5
4
1.5
3
2
1
1
2
3
4
5 6 Number of steps (N)
7
8
9
10
1
0
10
20
30
40 50 60 Number of steps (N)
(a)
70
80
90
100
(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
100 steps 10 trials
10 steps 100 trials
100 steps 100 trials
10 steps 1000 trials
a
1.007
0.9786
0.8612
0.9993
b
0.1199
0.08314
0.1896
0.8451
0.7447
0.9554
2
R
√ 100 steps 1000 trials
10 steps 10000 trials
100 steps 10000 trials
1.006
0.9943
1.003
1
0.08167
-0.003658
0.03825
-0.003868
0.006539
0.9651
0.9976
0.9946
0.9997
0.9995
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
y
1
15
0.5
10
0
5
-0.5
0
y
-1
-5
-1.5
-10
-2 -4
-3
-2
-1
0
1
2
3
4
-15 -10
5
-5
0
5
st
End
1 trial
nd
2
th
End
trial
10
15
x
x th
End
9 trial
1st trial
End
10 trial
4
End
2nd trial
9th trial
End
10th trial
End
End
14
Curve Fitting N 1/2
Distance
Curve Fitting N 1/2
Distance
12
3.5
10
3 > | n S | < e c n a t s i D
> | n S | < e 2.5 c n a t s i D
8
6
2 4
1.5
1
2
0
1
2
3
4
5 6 Number of steps (N)
7
8
9
10
0
10
20
30
40 50 60 Number of steps (N)
(a)
70
80
90
100
(b)
Figure 8. Value and distance for (a) 10 steps with 10 trials, (b) 100 steps with 10 trials 3
2
0 2 -2
1
-4
-6 y
0
y
-8 -1
-10
-12 -2 -14 -3 -3
-2.5 1st trial
-2
-1.5
-1
2nd trial
End
-0.5 x End
0
0.5
1
1.5
-16 -10
2
-8
-6
-4
-2
0
2
4
x 99th trial
100th trial
End
End
st
1 trial
3.5
End
nd
2
End
trial
th
th
End
99 trial
End
100 trial
11 Curve Fitting N 1/2
Distance
Curve Fitting N 1/2
Distance 10
3
9
8
> | n S | < e c n a t s i D
2.5
> | n S | < e c n a t s i D
2
7
6
5
4
1.5
3
2 1
1
2
3
4
5 6 Number of steps (N)
(a)
7
8
9
10
1
0
10
20
30
40 50 60 Number of steps (N)
70
80
(b)
Figure 9. Value and distance for (a) 10 steps with 100 trials, (b) 100 steps with 100 trials
90
100
3
15
2 10 1
0 5 y
-1
y
0
-2
-3 -5 -4
-5 -4
-3
-2
st
-1 nd
End
1 trial
2
0
1 x
3
4
5
-10 -14
6
-12
-10
-8
-6
-4
-2
0
2
4
x th
End
trial
2
th
End
999 trial
st
End
1000 trial
nd
End
1 trial
2
trial
th
End
th
End
999 trial
End
1000 trial
11
3.5 Distance
Curve Fitting N
1/2
Curve Fitting N 1/2
Distance 10
9
3
8
> | n S | < e c n a t s i D
2.5
> | n S | < e c n a t s i D
2
7
6
5
4
3
1.5
2
1
1
2
3
4
5 6 Number of steps (N)
7
8
9
1
10
0
10
20
30
40 50 60 Number of steps (N)
(a)
70
80
90
100
(b)
Figure 10. Value and distance for (a) 10 steps with 1000 trials, (b) 100 steps with 1000 trials
20
4
3 15 2 10 1
y
y
0
5
-1 0 -2 -5 -3
-4 -2 1
-1. 5
st
-1
-0.5 nd
End
trial
2
0
0. 5 x End
trial
1 9999
th
1.5
2 End
trial
2. 5 10000
th
-10 -6
3 End
trial
-4
1st trial
-2 2nd trial
End
0 x
2
4
9999th trial
End
6 10000 th trial
End
12
3.5 Curve Fitting N 1/2
Distance
Curve Fitting N 1/2
Distance
10 3
8 > | n S | < e c n a t s i D
2.5
> | n S | < e c n a t s i D
2
6
4
1.5 2
1
0 1
2
3
4
5 6 Number of steps (N)
(a)
7
8
9
10
0
10
20
30
40 50 60 Number of steps (N)
70
80
90
(b)
Figure 11. Value and distance for (a) 10 steps with 10000 trials, (b) 100 steps with 10000 trials
100
End
Table 2. Curve fitting property for 10 steps 10 trials
100 steps 10 trials
10 steps 100 trials
100 steps 100 trials
10 steps 1000 trials
a
1.168
1.002
1.01
1.019
b
-0.3342
-0.1072
0.003024
0.8919
0.7645
0.9865
2
R
√ 100 steps 1000 trials
10 steps 10000 trials
100 steps 10000 trials
1.004
1.002
0.9961
1
-0.1196
0.007713
-0.0141
0.009183
-0.002094
0.9782
0.9975
0.9975
0.9998
0.9998
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 Random Walk. Nature. 72, 294. [2] Van Kampen N. G., Stochastic Processes in Physics and Chemistry, revised and enlarged enlarged edition (North-Holland, Amsterdam) 1992. [3] McCrea, W. H. and Whipple, F. J. W. "Random Paths in Two and Three Dimensions." Dimensions." Proc. Roy. Soc. Edinburgh 60, 281-298, 1940.