Gambler's Ruin

In statistics, gambler's ruin is the fact that a persistent gambler with finite wealth, playing a fair game (that is, each bet has expected value of zero to both sides) will eventually and inevitably go broke against an opponent with infinite wealth.
The concept was initially stated: A persistent gambler who raises his bet to a fixed fraction of the gambler's bankroll after a win, but does not reduce it after a loss, will eventually and inevitably go broke, even if each bet has a positive expected value.

Below simulates a situation where a game will randomly (50%-50%) increase of deduct a proportion ("Rate") of the money you have, which in the beginning is "Base".
Hit plot botton to see how your money changes during the period of you specify in "Period" field.

Final Value (rounded):

See how your money will eventually become in the long run, even though the mathematical expectation of change in money is 0, somewhat meaning you should at least get your base back in the end.

This setup mimics a random walk process for a gambling game. Before everything, 3 values are initialized:

**1.** "base" is initilaized to 10000, which represents gambler's starting money.

**2.** "rate" is initilaized to 0.5, which determines the change in percentage (positive or negative) that can occur in each step of the random walk. In this case it's increase by 50% or decrease by 50%.

**3.** "period" is initialized to 365, which represents the number of steps or days in the simulation, in this case, default is 1 year.

First the program enters a loop that iterates times of "period" length. In each iteration, the current value of "base" is appended to a list that records the historical money gambler has. then value is then updated by multiplying it with a random value chosen from either 1-"rate" (go down) or 1+"rate" (go up), which simulates the random walk process.

Then the will display the final money gambler has at the end of the period and plot out the how gambler's money changed using html canvas.

First the program enters a loop that iterates times of "period" length. In each iteration, the current value of "base" is appended to a list that records the historical money gambler has. then value is then updated by multiplying it with a random value chosen from either 1-"rate" (go down) or 1+"rate" (go up), which simulates the random walk process.

Then the will display the final money gambler has at the end of the period and plot out the how gambler's money changed using html canvas.

import numpy as np import matplotlib.pyplot as plt base=10000 rate=.5 period=365 seq=[] for i in range(period): seq.append(base) base*=np.random.choice([1-rate,1+rate]) print(seq[-1]) plt.plot(seq)