:

Simulation of Brownian Motion

One-dimensional Brownian motion can be simulated using the following (modified from the version posted here originally) procedure,
```BM1:=proc(n::posint,m::posint)
local X,W,A,i;
uses Statistics,ArrayTools;
X:=RandomVariable(Normal(0,1/sqrt(n)));
W:=Array(1..n+1,1..m,1..2,datatype=float[8]);
W[2..n+1,1..m,2]:=Alias(Sample(X,m*n),[2..n+1,1..m]);
for i to m do W[2..n+1,i,2]:=CumulativeSum(W[2..n+1,i,2]) od;
A:=Array(1..n+1,1..2,datatype=float[8]);
A[1..n+1,1]:=Array(1..n+1,i->(i-1)/n,datatype=float[8]);
W[1..n+1,1..m,1]:=Replicate(A[1..n+1,1],1,m);
print(PLOT(seq(CURVES(W[1..n+1,i],
COLOUR(HSV,.6-.1*i/m,1.0,.7+.3*i/m)),i=1..m),
CURVES(A,COLOUR(HSV,0.0,1.0,1.0),THICKNESS(2)),AXESSTYLE(BOX)))
end:```
I'd like to thank Alex Potapchik for the idea of using Alias and CumulativeSum commands for creating W, and Axel Vogt for interesting and useful comments. Here is the original blog entry:
```BM:=proc(n::posint,m::posint)
local X,W,A,c;
uses Statistics,ListTools;
X:=RandomVariable(Normal(0,1/sqrt(n)));
W:='<0,PartialSums([seq(i,i=Sample(X,n))])[]>'\$m;
A:=`+`(W)/m;
c:=[seq(evalhf(i/n),i=0..n)];
print(plots[display](LineChart([W],xcoords=c),
LineChart(A,xcoords=c,color=red,thickness=2),
symbolsize=1,axes=boxed))
end:```
The first argument is the number of points in a path; the second argument is the number of paths. The average path is red. For example,
`BM(1000,5);`
`BM(1000,100);`
I did also ` BM(1000,1000)`, but numbers on the y-axis were partially cut off. By the way, the worksheet size grew up to 87 MB and I couldn't open it in a new Maple session.
`BM(1000,1000);`
______________ Alec Mihailovs http://mihailovs.com/Alec/

﻿