Breaking pills
In a recent post, Mark Jason Dominus posed the following question:
Suppose you have a bottle that contains
whole pills. Each day you select a pill at random from the bottle. If it is a whole pill you eat half and put the other half back in the bottle. If it is a half pill, you eat it. How many half-pills can you expect to have in the bottle the day after you break the last whole pill?
This is a nice problem and stated in a natural setting. Superficially, I was reminded of (Feller’s) Banach’s matchbox problem.
Consider an “in-between state”, after some number of days have passed. The bottle will have some number of whole pills, say
-
with probability
, we pick a whole pill and put back a half-pill, thus going to state . -
with probability
, we pick a half-pill and eat it, thus going to state .
Here’s the same thing drawn as a picture; the numbers on the arrows are the probabilities:
(We used the letters “w” and “h” to stand for “whole pills” and “half-pills”, but conveniently, we can also remember “h” as “height”, maybe.)
So starting at state
[TODO: some sort of animation / interactive applet here, showing evolution and the first time the path hits the
Two useless(?) digressions
There exists a mathematical formalism that models such random processes, though it turns out to be useless for this problem. Namely, a Markov chain. This is when we have some set
There’s another formalism, a special case of Markov chains, that is more relevant, though it was still not useful to me. Namely, the theory of an urn process. The following is from page 529 of Analytic Combinatorics by Flajolet and Sedgewick:
an urn model with two colours is determined by a
matrix with integer entries: At any instant, if a ball of the first colour is drawn, then it is placed back into the urn together with balls of the first colour and balls of the second colour; similarly, when a ball of the second colour is drawn, with balls of the first colour and balls of the second colour.
So we can model the whole pills and half-pills as two colours of balls, and thus our random process as the urn process determined by the matrix
—every time we pick a whole pill we add a half-pill to the bottle, and every time we pick a half-pill we return nothing to the bottle. Unfortunately I don’t know anything about urn processes besides this definition, and I couldn’t find a way to use this.
End of digression
Fortunately, it is not too hard to just solve this problem from scratch. Recall the illustration from earlier:
[TODO: either reproduce earlier animation or a small variant.]
So for each starting point
in which the probability of a particular nonnegative integer
In our problem, for each point
Each
-
For
, we have as we’ve already hit so the probability distribution is simply the current with probability . -
For
, we have where on the RHS we take the second term to be if (that is, as the product is going to be , we won’t bother ourselves with the question of what is… or we could just define that arbitrarily, or whatever).
For example, in Sage, we can compute it as follows:
var('z')
from collections import defaultdict
p = defaultdict(dict) # So that we can refer to p[w][h] without error.
N = 31
for h in range(N): p[0][h] = z**h
for w in range(1, N):
w = Integer(w)
for h in range(N - w):
p[w][h] = w/(w+h)*p[w-1][h+1] + h/(w+h)*(p[w][h-1] if h else 0)
For example, we get
Hover over a point
[TODO: create this interactive applet]
We started with
But if we only care about the expected value, then it turns out to have a nice expression. For a probability generating function as before, namely
we can get the expected value as the derivative of
So we can simply compute, additionally to the previous code:
E = defaultdict(dict)
for w in range(1, N):
for h in range(N - w):
E[w][h] = derivative(p[w][h])(z = 1)
This works, and the numbers turn out to be something interesting: see if you can notice the pattern below:
[TODO: make an interactive thing to see the values as fractions, replacing this table below.]
It turns out that there’s a simple closed-form expression:
where
This is easy to prove by induction:
-
For
, there’s nothing to prove: , because all the probability is concentrated on that one value. -
For
and , we can from only move to , so as expected. -
For
and , suppose we’ve proved that , for all lexicographically (i.e. anything to the left or directly below). Then, which is what we wanted to prove. (I wrote out the manipulations explicitly because it took me a few tries to get it to work!)
Alternative proof
We proved that
Another way to picture this problem is in terms of balls-in-bins: consider the starting state
Consider the following argument. Suppose we start at state
For each bin
Update
See follow-up post by MJD which includes more clear explanation and also an intriguing note about isomorphism with a frog puzzle.