5

I defined a security boundary for a random walk:

p = 5000;(*steps*)
tc = 15;(*cube edge length*)
tp = 0.2;

random = Accumulate[
Join[{RandomReal[{-tc, tc}/2, 3]}, 
RandomVariate[NormalDistribution[0, tp], {p, 3}]]];

periodizedWalk = Mod[random, tc, -tc/2];
splitPeriodizedWalk = 
Split[periodizedWalk, EuclideanDistance[#1, #2] < tc/2 &];

With[{cube = First[PolyhedronData["Cube"]]}, 
Graphics3D[{{Opacity[0.1], Scale[cube, tc]}, Line[random]}, 
Boxed -> False]]

and now i want to obtain a sample of the random times at which the random walk crosses the boundary for the first time. Something like this:

W = {}; For[i = 1, i <= 5000, i++,
PosFinal = {0, 0}; EME = 1;
While[True,
PosFinal = PosFinal + step[Random[]];
If[Norm[PosFinal] > 15, Break[]];

EME = EME + 1;
];
W = Append[W, EME];
]

but i want these two codes to be related. Would like some help please :)

1 Answers1

10

Ad 1 When the walker is corssing the boundary region:

pos = Most@Accumulate[Length /@ SplitBy[tc/2 - Max /@ Abs@random, Sign]];

With[{cube = First[PolyhedronData["Cube"]]}, Graphics3D[{{Opacity[0.1], Scale[cube, tc]}, Line[random], PointSize@.03, Red, Point[random[[pos]]]}, Axes -> True]]

enter image description here

Ad 2 When the walker reaches the boundary first time. + statistics for many walks.

If you only need a moment that the boundary was reached there is no need to store the path or continue the calculation after that point. This can be a way to go:

walk[] := NestWhile[# + {RandomReal[{-1, 1}, 3], 1} &, 
                    {{0, 0, 0}, 0}, 
                    Norm[#[[1]]] < 10 &]

Here the boundary is a sphere of radius 10. The result of such walk is the last position and number of iterations:

walk[]
{{8.00459, -6.26412, 3.81035}, 43}

So we only need to store the last element for multiple (100 here) cases, like:

Reap[
  Do[Sow[walk[][[2]]],
     {100}] 
    ];
Histogram[ %[[2, 1]]]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • This is the way for given data. More natural approach is to use Reap/Sow while creating data. – Kuba Mar 23 '14 at 11:43
  • i want something like this:

    W = {}; For[i = 1, i <= 1000, i++, PosFinal = {0, 0}; EME = 1; While[True, PosFinal = PosFinal + step[Random[]]; If[Norm[PosFinal] > 15, Break[]];

    EME = EME + 1; ]; W = Append[W, EME]; ]

    but i want this code and the first to be related.

    – Mariana da Costa Mar 23 '14 at 12:11
  • 2
    @MarianadaCosta While adding new element to the list check if it is crossing border, if so use Sow[new] to Reap those posints at the end. Also, do not use Append to create lists. See point 3.2 – Kuba Mar 23 '14 at 12:38
  • what i should use intead of Append? search in the link but didn't find. And how do i check if they cross the border? – Mariana da Costa Mar 23 '14 at 13:05
  • @MarianadaCosta You can use Sow with two different tags one for each point and additional one for only crossings :) Or use NestList instead of For. Chow to check, well one point is inside and next one is outside or the opossite. You can use similar solution tu mine from the answer. – Kuba Mar 23 '14 at 13:08
  • 1
    @MarianadaCosta you want still to know the path or only the number of iterations to the boundary matter? If points are important, do you want to keep going after crossing the boudary? – Kuba Mar 23 '14 at 16:48
  • 1
    Do[ i = 0; NestWhile[(i++; # + RandomReal[{-1, 1}, 3]) &, {0, 0, 0}, Norm[#] < 15 &]; Sow[i], {20}] // Reap – Kuba Mar 23 '14 at 17:07
  • oh right, didnt understand, already worked (: thanks a lot! :D – Mariana da Costa Mar 23 '14 at 17:43
  • +1 Neat. This is actually quite general problem. I'd add simplest Reap/Sow version to the solution - just in case - to show to the folks minimal form. Very nice in general, I do enjoy when folks care to add a nice picture ;-) – Vitaliy Kaurov Mar 23 '14 at 19:52
  • @VitaliyKaurov Thank you :) The code from the comment is actually an answer to this question. But it is not stated clear enough. Should I add this here or there? p.s. pictures are always motivating for me too :) – Kuba Mar 23 '14 at 20:01