Brief explanation about random walk in 1-D and 2-D with source code and results.

Attribution Non-Commercial (BY-NC)

3.3K views

Brief explanation about random walk in 1-D and 2-D with source code and results.

Attribution Non-Commercial (BY-NC)

- The Early Universe -Kolb & Turner
- Matlab Random Walk solution (code)
- Decision Analysis forPetroleum Exploration_Thomas W. Engler.pdf
- Notes on Quantum Mechanics
- problem-set-3-4.pdf
- Random Walk 1-D and 2-D
- Generate Two Correlated Random Vectors in MATLAB
- Stochastic Analysis Notes
- Random Walk
- ROLE OF MATHEMATICIAN
- 3fast 1-d Dct Algorithms With11 Mul
- Project on Random Walk
- PDEs
- Power Law
- Numerical Methods
- Matlab Partial Differential Equation Toolbox
- FEMPrimer-Part4
- Random Walk
- Schroeder - Introduction to Thermal Physics Solutions 3 of 5
- Watkins D. Fundamentals of Matrix Computations

You are on page 1of 11

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.

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 get expectation value of the squared value of random variable as follow: | 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.

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;

% 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

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)

% 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'

4

15

10

Value

Value

-2

-4

-5

-6

-10

-8

1 1 trial

st

2 2

3

nd

4 3

rd

6 8 trial

th

7 9 trial

th

9 10 trial

th

10

-15

10 1st trial

20

30 2nd trial

40

60 8th trial

70

80 9th trial

90 10th trial

100

trial

12 3 10

Distance <|Sn|>

Distance <|Sn|>

2.5

4 1.5 2

10

10

20

30

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

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

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

10

10

20

30

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

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

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

10

10

20

30

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

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

Distance <|Sn|>

Distance <|Sn|>

2.5

1.5

10

10

20

30

70

80

90

100

(a)

(b)

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

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

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.

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

0 x End

10 10th trial

15 End

trial

12

3.5

10

3

Distance <|Sn|>

Distance <|Sn|>

2.5

2

4

1.5

10

10

20

30

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 End

-1

-0.5 x End

0 99th trial

0.5 End

2 End

-8 End

-6 2nd trial

-4 x End

-2 99th trial

0 End

2 100th trial

4 End

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

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

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

10

10

20

30

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

th

3 End

-4 End

0 x 9999th trial

2 End

4 10000th trial

6 End

trial

10

8

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

2.5

1.5

2

10

10

20

30

70

80

90

100

(a)

(b)

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

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

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.

- The Early Universe -Kolb & TurnerUploaded bykeshav_prasad_1
- Matlab Random Walk solution (code)Uploaded byYussefRikli
- Decision Analysis forPetroleum Exploration_Thomas W. Engler.pdfUploaded bySuta Vijaya
- Notes on Quantum MechanicsUploaded byBruno Skiba
- problem-set-3-4.pdfUploaded byFrancis Philippe C. Cariño
- Random Walk 1-D and 2-DUploaded byAndri 'xflash' Rahmadhani
- Generate Two Correlated Random Vectors in MATLABUploaded byhammadmunawar
- Stochastic Analysis NotesUploaded bychristianblanco
- Random WalkUploaded bymuntaquir
- ROLE OF MATHEMATICIANUploaded byaayunurr
- 3fast 1-d Dct Algorithms With11 MulUploaded byAnil Kumar
- Project on Random WalkUploaded bychinsu87
- PDEsUploaded byblergfey
- Power LawUploaded byAndri 'xflash' Rahmadhani
- Numerical MethodsUploaded bysrichmech
- Matlab Partial Differential Equation ToolboxUploaded byelfrich
- FEMPrimer-Part4Uploaded bynilesh_raykar
- Random WalkUploaded byCiobanu Gheorghe
- Schroeder - Introduction to Thermal Physics Solutions 3 of 5Uploaded byjd24680
- Watkins D. Fundamentals of Matrix ComputationsUploaded byusersmall
- Matlab Random Walk problem statementUploaded byYussefRikli
- 2012,Wilde & Deutsch Kriging and Simulation in Presence of Stationary Domains.pdfUploaded byruribev2000
- Chaos RandomnessUploaded byMax Proctor
- Gauge Theory of Weak Interactions - Walter GreinerUploaded byAldo Fernando G G
- Computer Science and Engg PG CurriculumUploaded bylakshmi
- Craig Turnbull (Auth.)-A History of British Actuarial Thought-Palgrave Macmillan (2017)Uploaded byPolelar
- Applications of Discrete Mathematics, RosenUploaded byw
- Penelope 2014 NeaUploaded byJonathan Cristhian Muñoz León
- 03ME308DM.pdfUploaded byGanesh Chelluboyina

- Testing of SoftwareUploaded byEngr Syed Yahya
- AWS100_Ch05_Modal.pptUploaded byFredy Picauly
- BangaruBabu_Pureti_Hyderabad_3.01_yrsUploaded bySriman Ambati
- 01-06 Installing and Running the XML NBIUploaded bySergio Blasco
- CCMS Configuration - Adding New FilterUploaded byapi-3806579
- eBook - Web Programming UnleashedUploaded byadytzul89
- ps02solUploaded byAjith Suresh
- Excel With SASUploaded bySrinivas Chelikani
- Assembler Programming Complete ReferenceUploaded bySreenivas Rao
- Design and Implementation of Token Stealing Kernel Shellcode for Windows 8Uploaded bychuiyewleong
- PROFILE MANAGEMENT IN 11g.pdfUploaded byG.R.THIYAGU ; Oracle DBA
- Graph Theory PaperUploaded bylm1545
- 10 4 14 New Long Srp PriceUploaded byJon Snow-Stark
- 7-5Uploaded bysarasmile2009
- DocumentUploaded byas
- User GuideUploaded byDudi Kumar
- Geo-bombing: YouTube + Google EarthUploaded byammar
- eCRM in the Travel IndustryUploaded byankwah
- Seminar Report ‘04Uploaded byyuben joseph
- NCS V5 1.0 Layer Name FormatUploaded byGouhar Nayab
- Veeam One 9 5 Deployment Guide EnUploaded byMichele Brunelli
- Sales Rebate Processing (1B6)_ Process DiagramsUploaded byBijay Agarwal
- r05220501 Software EngineeringUploaded byandhracolleges
- Chapter 4: System ImplementationUploaded byZubaidah
- Loading Power Transformers Beyond Nameplate Ratings(1)Uploaded byHamayoun Murtaza
- CDAC2Uploaded byRohit Kshirsagar
- Competitors - Telecom Vendor Services (Current Analysis 2013-01-28)Uploaded byoatmill
- j2me unit - 2Uploaded bybhariprasad_msc
- Internet of ThingsUploaded byann maria
- Malware Removal ChecklistUploaded bySaileshResume

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.