Skip to main content

Drunken Walk / Gambler's Ruin

·1456 words

I was recently asked a question which I forgot the intuition for, so I wanted to write a post that approaches it from a few different perspectives and share how I got my intuition back. In the interest of entertaining myself I have reworded the problem with a ridiculous scenario that still preserves the mathematical statement of the problem.

Imagine you get into a high speed chase with the cops. You catch a lucky break and veer out of sight but end up crashing Xinit=100X_\mathrm{init} = 100 meters from the last location where the cops saw you. You proceed to get out of the vehicle and continue running away on foot, but in a state of shock, each stride you either take one step away from the cops with probability pp or accidentally take a step backwith probability q=1pq = 1 - p.

Your safehouse is just 1,000 meters away from where the cops last saw you (X=1000X = 1000), but if you take too many steps back you’ll end up being captured by the cops, who are looking for you at your last known location (X=0)(X = 0).

Assume the distance you cover in a single step is Δx\Delta x meters and you’re physically capable of taking steps as small as 0.10.1 meters or as large as 100100 meters (you’re either superhuman or you have a collection of ender pearls in your inventory).

What step size should you take assuming you have infinite time, and will only get caught if you stumble to X=0X = 0?

Parameters:

Xinit=100 metersXtarget=1000 metersXcapture=0 metersP(step forward)=pP(step backward)=q=1pStep size=Δx[0.1,100] meters \begin{align*} X_\mathrm{init} &= 100 \text{ meters} \\ X_\mathrm{target} &= 1000 \text{ meters} \\ X_\mathrm{capture} &= 0 \text{ meters} \\ P(\text{step forward}) &= p \\ P(\text{step backward}) &= q = 1 - p \\ \text{Step size} &= \Delta x \in [0.1, 100] \text{ meters} \end{align*}

I’ll lead with the intuition and a relatively simple argument first.

First Solution (Quick)
#

The larger steps you take, the more variance you’ll experience per step. For example, if you bounce 100100 meters then you have a probability qq of getting arrested after just one step. On the other hand, if you take a small step like 0.10.1 meters, you only move a little bit, but it’ll take you 1,000 steps to cover the same total distance (and I do mean distance in the physics sense of the word, not displacement).

For a given Δx\Delta x we know that the variance of your position increases with the number of steps, but for a given number of steps it decreases when you take smaller steps; so what we’re really trying to get at is the tradeoff between having more small steps versus fewer large steps. From the first example we considered it is already apparent that smaller steps are better for the first step, but we can build our intuition more by thinking about what happens when you take more and more smaller steps. Imagine taking the limit Δx0\Delta x \rightarrow 0 for a fixed distance traveled:

nΔx=d=constant n \Delta x = d = \mathrm{constant}

As you take smaller and smaller steps you have to take proportionately more steps to move the same distance. Your net displacement is the sum of these steps:

i=1nΔXi=nΔX \sum_{i=1}^n \Delta X_i = n \overline{\Delta X}

In the limit of infinitely many steps ΔX\overline{\Delta X} converges to the expectation value of a single step:

ΔXΔx(pq) \overline{\Delta X} \rightarrow \Delta x (p - q)

Hence your net displacement converges to:

nΔx(pq)=d(pq) n \Delta x (p - q) = d(p - q)

which is just a number, not a random variable. In other words, in the limit of infinitely small steps you literally just walk in a straight line, never backtracking towards the police, which is ideal. The closest we can get to that based on the constraints is taking steps of 0.10.1 meters, so we choose that as our preferred answer.

Let’s explore what this means from a highly related, but slightly different perspective. To reiterate, a walk is described by your initial position plus the cumulative sum of a sequence of random steps:

{ΔXi:i=1,2,...} \{\Delta X_i : i = 1, 2, ...\}

Each step is IID and can be expressed as:

ΔXi=ΔxZi \Delta X_i = \Delta x \, Z_i

where ZiZ_i is sampled from a distribution that yields +1+1 with probability pp or 1-1 with probability qq. In other words each step has a variance proportional to (Δx)2(\Delta x)^2, and the sum of nn steps has a variance proportional to:

n(Δx)2 n (\Delta x)^2

Notice that nn shows up with a power of 11 and Δx\Delta x shows up with a power of 22. Hence, when we take the limit of small steps, but keep the amount of distance traveled constant we fix a factor of nΔxn \Delta x, but the remaining Δx\Delta x keeps shrinking, collapsing the variance to 0.

Simulations
#

Of course, when I needed it the most, I didn’t have the intuition above because I panicked. I was able to come up with the explanation above by doing my favourite past-time activity: writing up Monte Carlo simulations 😀

I generated 3 simulations of paths taken for each value of dxdx and plotted them in the figure below, along with the expected value Xn=xinit+n(pq)dxX_n = x_\mathrm{init} + n (p - q) dx. By adjusting the slider at the bottom of the figure you can switch between dx=0.1,10dx = 0.1, 10, and 100100 to see how the step size affects the ‘stability’ of escape attempts.

Interactive Monte Carlo Simulations: Effect of Step Size on Escape Probability

01000200030004000500002004006008001000
EV0.110100$\text{Total Distance Traveled (m)}$$\text{Position } X \text{ (m)}$$\mathrm{d}x = 0.1$

In-depth solution
#

In this section I’ll share a more complicated solution which is how I originally understood this problem, but it’s too long and impractical to use in an interview setting unless you happen to memorize the final answer (which I never do).

Let’s forget about the original problem statement for now, and just treat it like a Markov process with absorbing boundary conditions at xlow=0x_\mathrm{low} = 0 and xhigh=1000x_\mathrm{high} = 1000. Fix the step size Δx\Delta x and define the grid of valid states as xk=kΔx:k=1,2,N{x_k = k \Delta x : k = 1, 2, \dots N }, where Nxhigh/ΔxN \equiv x_\mathrm{high} / \Delta x. As in the original prompt we’ll denote the probability of moving towards xhighx_\mathrm{high} in a given step by pp, and we’ll let q=1pq = 1 - p. For convenience, denote the probability of escaping the police, starting at state xmx_m by qmq_m. That is,

qmPr(escapexinitial=xm) . q_m \equiv \mathrm{Pr}(\, \mathrm{escape} \, |\, x_\mathrm{initial} = x_m \,) \ .

Trivially, at m=0m = 0 and m=Nm = N we have,

q0=0qN=1 \begin{align*} q_0 &= 0 \\ q_N &= 1 \end{align*}

Doing a ’next-step’ analysis, we observe that if you’re at state xmx_m now, then after one step you will either be at xm1x_{m-1} or xm+1x_{m+1}, at which point if we knew qm1q_{m-1} and qm+1q_{m+1} then we could calculate qmq_m as,

qm=pqm+1+qqm1 . \begin{align*} q_m = p \, q_{m + 1} + q \, q_{m-1} \ . \end{align*}

Using the fact that p+q=1p + q = 1 we can write the left hand side as

qm=(p+q)=pqm+qqm . \begin{align*} q_m = (p + q) \, = p \, q_m + q \, q_m \ . \end{align*}

Subtracting the latter from the RHS yields

0=p(qm+1qm)+q(qm1qm) \begin{align} 0 = p \, (q_{m + 1} - q_m) + q \, (q_{m-1} - q_m) \end{align}

Now, if we define Fmqmqm1F_m \equiv q_{m} - q_{m - 1} and introduce the inverse odds rq/pr \equiv q / p we can rearrange eqn (1) as

Fm+1=qpFm=rFm . \begin{align*} F_{m+1} = \frac{q}{p} \, F_m = r F_m \ . \end{align*}

Induction then tells us

Fm+1=rmF1=rmq1 . \begin{align} F_{m+1} = r^m \, F_1 = r^m q_1 \ . \end{align}

On the other hand, using the definition of Fm F_m we observe that qm=k=1mFk q_m = \sum_{k=1}^m F_k . So, in combination with eqn (2) we have

qm=k=1mFk=k=1mrk1q1=q1rk=0m1rk1=q1r1rm1r . \begin{align*} q_m = \sum_{k=1}^m F_k = \sum_{k=1}^m r^{k-1} q_1 = \frac{q_1}{r} \sum_{k=0}^{m-1} r^{k-1} = \frac{q_1}{r}\frac{1 - r^m}{1 - r}\ . \end{align*}

To solve for q1q_1 we apply the boundary condition qN=1q_N = 1:

1=qN=q1r1rN1rq1r=1r1rN . \begin{align*} 1 = q_N = \frac{q_1}{r}\frac{1 - r^N}{1 - r} \Rightarrow \frac{q_1}{r} = \frac{1 - r}{1 - r^N} \ . \end{align*}

And so,

qm=1(q/p)m1(q/p)N . \begin{align} \boxed{q_m = \frac{1 - (q / p)^m}{1 - (q / p)^N}}\ . \end{align}

Eqn (3) can be used to solve our original problem. Since N=xend/ΔxN = x_\mathrm{end} / \Delta x and m=xinitial/Δxm = x_\mathrm{initial} / \Delta x we immediately see that taking smaller steps is equivalent to sending $m$ and $N$ to infinity at the same rate, so

limΔx0Pr(escape)=1. \begin{align*} \lim_{\Delta x \rightarrow 0} \mathrm{Pr}(\mathrm{escape}) = 1. \end{align*}

And clearly we should pick the smallest possible value of Δx \Delta x permitted.