[Date Index]
[Thread Index]
[Author Index]
Re: Faster Random Walk Simulation ?!?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg66204] Re: Faster Random Walk Simulation ?!?
*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
*Date*: Thu, 4 May 2006 05:20:06 -0400 (EDT)
*Organization*: The Open University, Milton Keynes, UK
*References*: <e39k1n$cmn$1@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
mfific at gmail.com wrote:
> Dear All,
> I am running a simulation with Mathematica 5 that includes random walk
> described below. The argument boundaryA is upper and lower boundary for
> a random walk. Parameter "value" is just a constant arbitrarily set to
> 0.5. The output of the table function is the 10000 random walks, and
> their path to either +5 or -5 boundary value.
> While everything works as expected, it takes considerable duration of
> time. For example it takes 2.6 seconds to generate the table output, on
> a relatively fast computer.
>
> I would very appreciate if any significantly faster solution could be
> suggested.
>
>
> value = .5
>
>
> RandomWalk[boundaryA_] := Block[{l = {{0, 0}}, x = 0,
> i = 0 }, While[boundaryA > x > -boundaryA,
> x += If[Random[] > value, 1, -1];
> l = Append[l, {++i, x}]];
> l]
>
> Timing[Table[RandomWalk[5], {i, 1, 10000}];]
>
> Out[420]=
> {2.672 Second, Null}
>
>
> Thank you very much,
>
> Mario Fific
>
>
> Mario Fific
> Cognitive Psychology, Cognitive Science
> Indiana University
> 1101 E. 10th St.
> Bloomington, IN 47405-7007
>
Hi Mario,
Although I have got a speed improvement of about 10% (see RandomWalk2),
I doubt we can significantly improve on your code since most of the cpu
time must be spent on building the list of lists itself.
In[1]:=
value = 0.5;
RandomWalk[boundaryA_] := Block[{l = {{0, 0}}, x = 0,
i = 0}, While[boundaryA > x > -boundaryA,
x += If[Random[] > value, 1, -1];
l = Append[l, {++i, x}]]; l];
Timing[Table[RandomWalk[5], {i, 1, 10000}]; ][[1]]
Out[3]=
2.797*Second
In[4]:=
RandomWalk2[boundaryA_, value_:0.5] :=
NestWhileList[{First[#1] + 1, Last[#1] +
If[Random[] > value, 1, -1]} & , {0, 0},
-boundaryA < Last[#1] < boundaryA & ];
Timing[Table[RandomWalk2[5], {10000}]; ][[1]]
Out[5]=
2.421*Second
In[6]:=
RandomWalk3 = Compile[{boundaryA, value},
NestWhileList[{First[#1] + 1, Last[#1] +
If[Random[] > value, 1, -1]} & , {0, 0},
-boundaryA < Last[#1] < boundaryA & ]; ];
Timing[Table[RandomWalk3[5., 0.5], {10000}]; ][[1]]
Out[7]=
2.547*Second
Best regards,
Jean-Marc
Prev by Date:
**Re: Selecting Many Things Rather Than Selecting One Thing From Many**
Next by Date:
**Re: Faster Random Walk Simulation ?!?**
Previous by thread:
**Re: Faster Random Walk Simulation ?!?**
Next by thread:
**Re: Faster Random Walk Simulation ?!?**
| |