Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I program a random walk in Mathematica?

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

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

representing the ith step, is a vector with the length Pi, and a value of the change of direction between the vectors Pi and P(i+1) is measured algebraically (don't know how) by the turning angle a(i).

Like any angular distribution (of changes of direction) it is characterized by a mean vector which is taken to be symmetrical and to have angular mean Φ = o. The approach to this analysis involves numerical simulations, since the algebraic approach seems to be too complex and I have to use a pseudo-random Gaussian generator to obtain continuous values from a normal distribution with a mean of 0 and a standard deviation σ (0.1-1.2)radians to simulate a path.

So after each step with the length P (is constant i.e 125km), the value of the change of direction (turning angle a(i)) is determined by the pseudo-random generator for a given value of σ, which is constant along the path. And then makes a step in the next direction, and so on.

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 at random according to a uniform angular distribution by a pseudo-random uniform generator. Turning angles are recorded from -Pi to Pi

So my question is:

How can I take i.e 12 families of 500 step paths each characterized by a given value of standard variation σ ranging between 0.1 and 1.2 radians, of the distribution of changes of direction between successive steps and PLOT it in Mathematica? I don't know anything about Mathematica specially how to write the code for this problem.

like image 347
G.Xara Avatar asked May 26 '11 20:05

G.Xara


People also ask

What is random walk programming?

Introduction A random walk is a mathematical object, known as a stochastic or random process, that describes a path that consists of a succession of random steps on some mathematical space such as the integers.

How do I run a program in Wolfram Mathematica?

To Launch Mathematica:From the Start menu, choose Programs ▶ Wolfram Mathematica ▶ Mathematica 12. Alternatively, you can open an Explorer window, go to the Program Files\Wolfram Research\Mathematica\12.0 directory, and double-click the Mathematica icon.


1 Answers

Following your notations in the question, you use independent normal increments to build an angle, and then use it as a direction for next step of size size.

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. Xara's request to visualize random walk on Robinson's projection of a sphere.
RandomRobinsonWalk[coords_List] := 
 Show[CountryData["World", {"Shape", "Robinson"}], 
  Graphics[{Thick, Red, 
    Line[Map[ GeoGridPosition[ GeoPosition[#], "Robinson"][[1]] & , 
      coords]]}], Frame -> True]

Generation of random walk on a sphere is performed 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 next {\[Theta], \[Phi} pair 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, perform a fixed size rotation in [Theta] and then rotate around the previous vector by random angle \[Alpha].

Usage:

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

enter image description here

like image 189
Sasha Avatar answered Nov 04 '22 03:11

Sasha