Random Walker Continued

Follow the profit

The R function that fits well with this objective is cumsum(). For example, here is what it gives if you apply it to the vector (1, 4, 2, 3, 6):

cumsum(c(1, 4, 2, 3, 6))  # gives

#   1  5  7 10 16

It sums as it goes along: 1, 1+4=5, 5+2=7, 7+3=10, 10+6=16

Let’s use it on a set of 10,000 random outcomes of a coin. For that, we need to generate random numbers with the correct values for the profit and loss at 50:50 probabilities. Then plot the outcome.

n <- 10000
xx_f <- sample(c(-1, 1), n, TRUE, prob = c(1/2, 1/2))
x_val <- cumsum(xx_f)
plot(x_val, type = "l", col = "blue", ylim = c(-150,100), xlab = "", ylab = "")

Don’t assume that this is the only outcome you can expect. If I run the same program four more times, here is what you get:

So, anything is possible! But not all equally likely, as can be seen below:

Your intuition is correct; the maximum probability of occurrence for the H is 5000, which means the profit is zero. Again, not to forget: the absolute probability of getting 5000 may be the highest, about 80 out of 10000, yet that is not what you see in real life because not getting zero overwhelms (10000 – 80)! You can also see that chances of getting a gain of 200 (5100 Hs) or a loss of 200 (4900 Hs) are still possible, but with a lower likelihood. But, a gain (or a loss) of 400 is really rare.

Gambler’s ruin

While any of the above plots may be used to illustrate a gambler’s ruin, we will use our favourite roulette wheel; probability 18/38 for a win and 20/38 for a loss. The random walk is dramatic here for a 10000-long run.

You can try as many times as possible; getting a positive value at the end of this walk is unlikely!