Skip to content
Advertisement

Random walk generate OverflowError: cannot convert float infinity to integer

I edited a chuck of random walk code from this link (ps: the orginal one works):

JavaScript

But my edited version throw out an error: OverflowError: cannot convert float infinity to integer, does someone could help to fix this issue? Thanks in advance.

Advertisement

Answer

Well, x[i] can be as large as 10, and so can y[1], so

JavaScript

can be as large as (1 + 0.8*10 + 1.5*10)*price[i-1] = 24.0 * price[i-1].

You only need a few hundred of those chained to overflow to infinity:

JavaScript

Of course they won’t all be 10, but it doesn’t matter all that much: exponential growth quickly explodes regardless. If, e.g., they were all 5 instead:

JavaScript

The question is more why you don’t expect it to overflow to infinity?

Note that the code you started from did not multiply on each step. It added a “random shock” instead. That’s far better behaved.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement