1
$\begingroup$

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,

$\endgroup$
3
  • 2
    $\begingroup$ M is a nailed thing in your code ... $\endgroup$ Jul 20, 2012 at 19:46
  • 3
    $\begingroup$ As it is one for the whole recursion. You can use M:=... if you need a new one at each recursion. $\endgroup$ Jul 20, 2012 at 19:46
  • $\begingroup$ Thanks guys! Always helpful, I'll push you both up. $\endgroup$
    – kηives
    Jul 20, 2012 at 19:51

1 Answer 1

7
$\begingroup$

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.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.