MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Faster Random Walk Simulation ?!?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66275] Re: Faster Random Walk Simulation ?!?
  • From: Mark Fisher <mark at markfisher.net>
  • Date: Sat, 6 May 2006 23:50:37 -0400 (EDT)
  • References: <e39k1n$cmn$1@smc.vnet.net><e3cism$a5i$1@smc.vnet.net> <e3f5gl$s84$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

mfific at gmail.com wrote:

> Hi Mark,
> 
> This is the winner!!! Timing is now .45 sec. I tried to use Compile
> function before but without sucess. Could you help me (and potential
> readers) with short explanation of your coude. 
> 
> thank you 
> 
> Mario
> 

Sure. To repeat,

cf = Compile[
    {{value, _Real}, {bound, _Integer}},
   Transpose[{Range[0, Length[#] - 1], #}]& @
    NestWhileList[
     # + If[Random[] > value, 1, -1]&,
     0,
    -bound < # < bound&]
   ]

The main part is the NestWhileList, which takes 3 arguments, the 
function to be nested (an anonymous function which assumes value has be 
given)

(# + If[Random[] > value, 1, -1])&

the starting value

0

and the "while" condition (an anonymous function that returns True or 
False as long as bound is a number)

(-bound < # < bound)&

The list of integers returned by NestWhileList is passed to the function 
that creates the index numbers

Transpose[{#, Range[0, Length[#] - 1]}]&

Each of these parts works by itself outside of Compile. Try them out and 
see what they do. For example try

Transpose[{#, Range[0, Length[#] - 1]}]& @ {a, b, c}

and

With[{bound = 5}, (-bound < # < bound)& @ 4]

and

With[{value = .5}, (# + If[Random[] > value, 1, -1])& @ 0]

Have I helped or just added more mystery?

--Mark


  • Prev by Date: Save to Quicktime during calculations?
  • Next by Date: Re: Re: Determining Linear dependent vectors
  • Previous by thread: Re: Faster Random Walk Simulation ?!?
  • Next by thread: Re: Faster Random Walk Simulation ?!?