Random Walk Simulation & Graph

Define seed value (Subscript[M, 0]), “up/down” amount (Subscript[X, n])(ω),  and probability p that (Subscript[ω, n])=U, and number of iterations "Num";

Mzero = 0;
UpAmount = 1;
DownAmount = -1;
UpProb = .5;
Num=100;

Generate and plot random walk stochastic process (Subscript[M, n]), computing the mean and standard deviation of the generated data.

Mold = Mzero;

RandomWalk={};

Do[
If[RandomReal[]≥ 1-UpProb,

Mnew = Mold + 1;

,

Mnew = Mold - 1];

RandomWalk = Append[RandomWalk,Mnew];

Mold = Mnew;

,Num];

ListLinePlot[RandomWalk, AxesLabel->{"Step (n)", "(Subscript[M, n])"}]

MeanRW=N[Mean[RandomWalk]];
StanDevRW = N[Sqrt[Variance[RandomWalk]]];

Print["Mean of the random walk is "<>ToString[MeanRW]<>" and the standard deviation is "<>ToString[StanDevRW]<>"."]

Generate a sequence of n - step random walks representing the stochastic process (Subscript[M, n]).

Mzero = 0;
UpAmount = 1;
DownAmount = -1;
UpProb = .5;

Num=1000;

RWSeqData ={};

NumRW = 50;

 Mold = Mzero;

RWPlot={};

Do[

Mold=0;

Mnew=Mzero;

RandomWalk={};

Do[

If[RandomReal[]>1/2,

Mnew = Mold + 1;

,

Mnew = Mold - 1];

RandomWalk = Append[RandomWalk,Mnew];

Mold = Mnew;

,Num];


RWSeqData = Append[RWSeqData,RandomWalk];

,NumRW];

Plot (Subscript[M, n]) for many trials.  Note the (n^(1/2)) behavior where n is the number of steps.

RWListPlotAll = ListPlot[RWSeqData, AxesLabel->{"Step (n)", "(Subscript[M, n])"},PlotRange->All];
SQPlot = Plot[{2Sqrt[n],-2Sqrt[n]},{n,1,Num},PlotStyle->{Thick,Blue},AxesLabel->{"Step (n)", "(Subscript[M, n])"},PlotRange->All];
Show[SQPlot,RWListPlotAll]

Animate (Subscript[M, n]) for many trials.

Animate[ListPlot[RWSeqData[[n]], AxesLabel->{"Step (n)", "(Subscript[M, n])"},PlotRange->All],{n,1,NumRW,1}]