How can I program random walk in Mathematica?

In the plane (two-dimensional), the path can be represented by a sequence of (n + 1) points (Xo, Yo), (X1, Y1), ..., (Xn, Yn) such that, for any i (integer 1 <in -one):

Pi(vector) = [Xi-X(i-1),Yi-Y(i-1)] 

representing the i-th step, is a vector with a length Pi, and the value of the change in direction between the vectors Pi and P (i + 1) is measured algebraically (I do not know) using the angle of rotation a (i).

Like any angular distribution (changes in direction), it is characterized by an average vector that is considered symmetrical and has an angular average value of ฮฆ = o. The approach to this analysis involves numerical modeling, since the algebraic approach seems too complicated, and I have to use a Gaussian pseudo-random generator to get continuous values โ€‹โ€‹from a normal distribution with an average value of 0 and a standard deviation of ฯƒ (0.1- 1.2) radians to model the path.

Thus, after each step with a length P (constant, i.e. 125 km), the value of the change in direction (angle of rotation a (i)) is determined by a pseudo-random generator for a given value of ฯƒ, which is equal to a constant along the path. And then it takes a step in the next direction, etc.

Some useful equations:

 a(i) ~ n(0,ฯƒ) ฮ˜(i+1) = ฮ˜(i) + a(i) X(i+1) = Xi + P Cos[ฮ˜(i+1)] Y(i+1) = Yi + P Sin[ฮ˜(i+1)] 

where ฮ˜i represents the direction of the ith step. The direction of the first step ฮ˜i is chosen randomly in accordance with the uniform distribution of the angular pseudo-random uniform generator. Angle of rotation recorded from -Pi to Pi

So my question is:

How can I take 12 families with 500 step paths, each of which is characterized by a given standard deviation ฯƒ from 0.1 to 1.2 radians, the distribution of direction changes between successive steps and PLOT in Mathematica? I don't know anything about Mathematica, especially how to write code for this problem.

+7
source share
3 answers

After your notation in the question, you use independent normal increments to plot the angle, and then use it as the direction for the next size step.

 Block[{size=0.5}, Graphics[ Line[Accumulate[ Function[x, size*{Re[x], Im[x]}, Listable][ Exp[I Accumulate[ RandomVariate[NormalDistribution[0, Pi/4], 10^3]]]]]] ]] 

enter image description here


EDIT . This is a response to G. Haraโ€™s request for visualization of a random walk on a Robinson sphere projection.
 RandomRobinsonWalk[coords_List] := Show[CountryData["World", {"Shape", "Robinson"}], Graphics[{Thick, Red, Line[Map[ GeoGridPosition[ GeoPosition[#], "Robinson"][[1]] & , coords]]}], Frame -> True] 

The random walk on a sphere is generated as follows:

 Coordinates[{\[Theta]_, \[Phi]_}, {cosa_, sina_}, \[CapitalDelta]\[Theta]_] := {ArcCos[ Cos[\[CapitalDelta]\[Theta]] Cos[\[Theta]] - cosa Sin[\[CapitalDelta]\[Theta]] Sin[\[Theta]]], ArcTan[cosa Cos[\[Theta]] Cos[\[Phi]] Sin[\[CapitalDelta]\[Theta]] \ + Cos[\[CapitalDelta]\[Theta]] Cos[\[Phi]] Sin[\[Theta]] + sina Sin[\[CapitalDelta]\[Theta]] Sin[\[Phi]], (cosa \ Cos[\[Theta]] Sin[\[CapitalDelta]\[Theta]] + Cos[\[CapitalDelta]\[Theta]] Sin[\[Theta]]) Sin[\[Phi]] - Cos[\[Phi]] sina Sin[\[CapitalDelta]\[Theta]]]}; Clear[SphereRandomWalk]; SphereRandomWalk[ipos_, steps_, stepsize_, prec_: MachinePrecision] := FoldList[Function[{up, cossin}, Coordinates[up, cossin, stepsize]], ipos, Function[u, {Re[u], Im[u]}, Listable][ Exp[I RandomVariate[UniformDistribution[{-Pi, Pi}], steps]]]] 

The formula used to obtain the following pair {\[Theta], \[Phi} was obtained as follows:

 Expand[Simplify[((RotationMatrix[\[Alpha], {Sin[\[Theta]] Sin[\[Phi]], Sin[\[Theta]] Cos[\[Phi]], Cos[\[Theta]]}].({Sin[\[Theta]] Sin[\[Phi]], Sin[\[Theta]] Cos[\[Phi]], Cos[\[Theta]]} /. {\[Theta] -> \[Theta] + \[CapitalDelta]\ \[Theta]}))) /. {Conjugate -> Identity} /. {Abs[x_]^2 :> x^2}]] 

that is, rotate a fixed size in [Theta], and then rotate a random angle \[Alpha] around the previous vector.

Using:

 ((# - {90, 0}) & /@ (SphereRandomWalk[{Pi/2, 0} // N, 2500, Pi*0.01]/ Degree)) // RandomRobinsonWalk 

enter image description here

+8
source

Sasha's answer is the way to go, but as you start with Mma, perhaps the procedural program is easier to understand. Please note that I am NOT saying that this is a good way to do something in Mme.

 P = 1; For[iter = 1, iter < 13, iter++, sigma = iter/10; a = RandomVariate[NormalDistribution[0, sigma], 500]; Clear[theta, x]; theta[i_] := theta[i] = theta[i - 1] + a[[i]]; x[i_] := x[i] = x[i - 1] + P {Cos[theta[i]], Sin[theta[i]]}; theta[0] = RandomReal[{0, 2 Pi}]; x[0] = {0, 0}; For[step = 1, step < 500, step++, r[iter, step] = x[step]; ] ] Show@Table [ ListLinePlot[Table[r[s, i], {i, 500}], PlotStyle -> ColorData[1][s], PlotRange -> 300 {{-1, 1}, {-1, 1}}], {s, 1, 12}] 

enter image description here

+5
source

The following code displays your twelve families in different colors, and if you hover over a line, you will get a sigma in the tooltip.

 Graphics[ Table[ {x, y} = {0, 0}; p = 1; \[Theta] = RandomReal[{-\[Pi], \[Pi]}]; Tooltip[ { Hue[\[Sigma]/1.3], Line[ NestList[(\[Theta] += RandomVariate[NormalDistribution[0, \[Sigma]]]; # + p {Cos[\[Theta]], Sin[\[Theta]]}) &, {x, y}, 500] ] }, \[Sigma] ], {\[Sigma], 0.1, 1.2, 0.1} ] ] 

enter image description here

+5
source

All Articles