0
$\begingroup$

I'm trying to generate a random walk using RecurrenceTable:

RecurrenceTable[
  {a[n + 1] == RandomVariate[  NormalDistribution[a[n], 0.5`]] , a[1] == 10},
  a, {n, 1, 10}]

The above (simplified example) code works (in version 10.3), but I get two errors:

RandomVariate::realprm: Parameter a[n] at position 1 in NormalDistribution[a[n],0.5] is expected to be real. 
RandomVariate::realprm: "Parameter #2 at position 1 in NormalDistribution[#2,0.5] is expected to be real." 

I suspect that this is because mathematica is attempting to evaluate NormalDistribution immediately, but should only be doing so once a[n] is evaluatable to a number.

Whats the right way to correct this error?

$\endgroup$
7
$\begingroup$

This works..

f[x_?NumericQ] := RandomVariate[NormalDistribution[x, 0.5`]]
RecurrenceTable[{a[n + 1] == f[a[n]], a[1] == 10}, a, {n, 1, 10}]

{10, 9.01261, 8.63581, 8.92847, 9.097, 9.48324, 9.44516, 8.19759, 8.01132, 8.85788}

I don't see why you don't just do this though:

NestList[RandomVariate[NormalDistribution[#, 0.5`]] &, 10, 10]
$\endgroup$
  • $\begingroup$ Excellent! As to why i'm not using NestList, it's because I didn't know it existed. I will now. $\endgroup$ – user1816847 Oct 4 '16 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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