# Using different random values at each recursion step

I have the following code:

ϕ[0] := π/4
ϕ[n_] := Exp[-n] ϕ[n - 1] + Sqrt[1 - Exp[-2 n]] M
M = RandomVariate[NormalDistribution[0, 1]]


For this recursion relation, is the normal deviate taking a random variable during each recursion or is it just taking one for the whole recursion process?

Thanks,

-
M is a nailed thing in your code ... –  belisarius Jul 20 '12 at 19:46
As it is one for the whole recursion. You can use M:=... if you need a new one at each recursion. –  b.gatessucks Jul 20 '12 at 19:46
Thanks guys! Always helpful, I'll push you both up. –  kηives Jul 20 '12 at 19:51

Set (=) stores the value at the time of computation, while SetDelayed (:=) stores the definition, so that the value is computed again each time it is called. Therefore, writing

M = RandomVariate[NormalDistribution[0, 1]]


means using the same value over and over, while writing

M := RandomVariate[NormalDistribution[0, 1]]


means computing a new RandomVariate at each iteration.

Moreover, no matter what your choice is about M, while we're at it, you might want to do memoization for the whole process, i.e. write

ϕ[0] = π/4
ϕ[n_] := ϕ[n] = Exp[-n] ϕ[n - 1] + Sqrt[1 - Exp[-2 n]] M


This way when computing ϕ[n] you store the value in your memory, so that when computing ϕ[n + 1] you only have to recall it, instead of computing it from scratch. It is usually worth it to trade memory for speed this way.

-
Thank you. Here is some text. –  kηives Jul 20 '12 at 21:34