How can I generate a random walk?

Bruce Bartlett shared this question 1 year ago
Answered

How can I define random walk in Geogebra? Say we choose -1 or 1 with equal probability N times, and then we want to define a rescaled function f(x) out of it.

In other words, let N be a fixed integer (given by a slider). I would like to define the following function of x, where x is a real parameter on the interval [0,1]:

Jumps = Sequence(RandomElement({-1, 1}), i, 1, N)

f(x) = Sum(Jumps, ceil(N*x))

This code does not work. I have not found a way to do this.

Best Answer
photo

If you combine IterationList() with this then I think that will be a bit faster (you also need an UpdateConstruction() in the slider's On Update script if you want it to be dynamic)

DataFunction(Sequence(N), l1)
g(x) = f(round(x))

Comments (5)

photo
2

N=100
Jumps = Sequence(RandomElement({-1, 1}), i, 1, N)
l1=Sequence(Sum(First(Jumps, i)), i, 1, N)
DataFunction(Sequence(N), l1)

photo
1

Thank you. Very helpful. It's not 100% what I would like, as there are two problems:

1. I would prefer the function to be a stepwise function, not triangular (that's why I used "ceil" in my formula).

2. Your code to compute l1 will be inefficient for large N. How can we compute l1 by running through the Jumps list only once?

photo
1

Here your points 1 and 2 will be met.

The performance is low (I suspect, on the basis of the requirement in point 1.). Possibly a BarChart() diagramm or a Polyline() is more performant.

photo
1

Ok, thank you to Michael and rami. IterationList!

photo
2

If you combine IterationList() with this then I think that will be a bit faster (you also need an UpdateConstruction() in the slider's On Update script if you want it to be dynamic)

DataFunction(Sequence(N), l1)
g(x) = f(round(x))

© 2020 International GeoGebra Institute