Monkey Typing ABRACADABRA

2/2/2014

Solving a probability puzzle using martingale theory

My first post from my old blog martingalemeasure. It seems only fitting that the first post should be about probability; martingales in particular. In my favorite introductory book on measure theoretical probability, “Probablity with Martingales” by David Williams, we find an exercise in Chapter 10, which I paraphrase here:

Suppose a monkey is typing randomly at a typewriter whose only keys are the capital letters AA through ZZ of the english alphabet. What is the expected (average) time it will take for the monkey to type the word ABRACADABRAABRACADABRA?

This is not an easy problem. In fact it’s not entirely obvious that the average time is even finte! Williams expects the reader to solve it using the beautiful theory of martingales and in particular Doob’s optional-stopping theorem. We will calculate the result below, but stop short of a proof. (There are many proofs of this result already on the web. A quick search for “monkey abracadabra” will provide several links).

The purpose of this post is to try to provide the intuition behind the result. We do this by comparing the expected time it takes for the monkey to type ABRACADABRAABRACADABRA with the expected time it takes to type the first eleven letters of the alphabet ABCDEFGHIJKABCDEFGHIJK.

Simplification

It’s almost always a good idea when trying to solve a problem to first attack the simplest case you can think of that still embodies the essence of the problem. For our problem we can certainly benefit both from picking shorter strings, and by choosing a smaller alphabet. The important distinction between A ABRACADABRAABRACADABRA and ABCDEFGHIJKABCDEFGHIJK is that the former starts to repeat at charcter 88, i.e has a repeated sub-string. So if we look at ABAABA versus ABCABC we have a much shorter word that has the repeated sub-string AA. Since we lose nothing by restricting the possible letters to one of AA, BB, or CC; we restrict our alphabet to these three letters.

Solution

Ok, let’s use martingale theory to solve the simplified problem and then apply the result to the bigger problem. As is often the case in probability theory we will think in terms of gamblers making bets. Suppose that just before each keystroke made by the monkey, a new gambler shows up at a casino and employs the following betting strategy. She wagers one dollar that the monkey will type an AA, the first letter in the word. If she wins, the casino pays her fair odds and she receives 33 dollars. She then bets the entire 33 dollars that the next keystroke will be a BB, the second letter. She continues until either she has lost her initial dollar or the word is typed in full; in that case she wins 2727 dollars, for a profit of 2626.

Each individual bet is fair, that is, has an expected value of zero. Indeed, our gambler has a 1/31/3 chance of tripling her wager, say xx for profit of 2x2x and a 2/32/3 chance of losing it. Therefore, her expected value is

2x1/3x2/3=02x\cdot 1/3 - x\cdot 2/3 = 0

Hence, the entire strategy is fair, the gambler on average will not make or lose any money. If we now consider the total amount won or lost by the casino at any point in time, then that too must have expectation zero, since each gambler is playing a fair game, so is the casino as a whole. I should probably mention that the stochastic process describing the profit of the casino is a martingale, which is why we can apply the necessary mathematical machinery we need to solve this problem.

Here comes the big trick, when we say that the casino has expected gains of zero at any time, we mean random times as well. Including random times like TABCT_{ABC} the first time the word ABCABC is typed or TABAT_{ABA} the first time the word ABAABA is typed (technically time TT is called a stopping time and needs to satisfies one of the sufficient conditions of Doob’s theorem). We are almost there, all we need to do is calculate the expected gain by the casino at times TABCT_{ABC} and TABAT_{ABA} and we will be done. Let’s do ABCABC first. The first time the monkey types ABCABC, all of the gamblers except the third to last will have winnings of zero. The third to last will have winnings of 2727. The total amount wagered will be TABCT_{ABC}. So in order for the game to be fair we must have,

E[TABC27]=0\mathbb{E}[T_{ABC} - 27]=0

Hence, by the linearity of expectation,

E[TABC]=27\mathbb{E}[T_{ABC}]=27

But what happens when we analyze the string ABAABA instead? At time TABAT_{ABA}, not only has the third to last gambler 2727 in winnings, but the last gambler has 3.3. Since she wagered one dollar on the event the monkey typed an AA and the monkey did. Hence,

E[TABA273]=0\mathbb{E}[T_{ABA}-27-3]=0

and

E[TABA]=30\mathbb{E}[T_{ABA}]=30

Wow, the average time for the monkey to type ABAABA is actually longer than the average time to type ABCABC.

Back to ABRACADABRA

It should now be straight forward to calculate the expected time it takes for the monkey to type ABRACADABRAABRACADABRA and ABCDEFGHIJKABCDEFGHIJK. We simply compute how much money the casino owes the first time the monkey types the word. In the case of ABCDEFGHIJKABCDEFGHIJK the only winner is the gambler who started 1111 keystrokes ago. She grosses 261126^{11} so that turns out to be the expected time. But in the case of ABRACADABRAABRACADABRA the gambler who started playing 44 keystrokes ago has winnings of 26426^4 and the gambler who bet on the last keystroke won 2626, hence for ABRACADABRAABRACADABRA,

E[T]=2611+264+26\mathbb{E}[T]=26^{11} + 26^4 + 26

Intuition

I don’t know about you, but I find this result is counter-intuitive. I expected it would take the same time on average, or if anything shorter to type ABRACADABRAABRACADABRA, since at the 8th keystroke the monkey types an A starting the sequence again. Here is where the simplified case using ABCABC and ABAABA can help us get a better grip on what is really going on. In these cases we can easily caculate some probabilities. Let’s look at all of the winning strings after 5 keystrokes. We use * to mean any character in the alphabet, in this case one of AA, BB, or CC. Here are the three events that represent a case where the monkey has typed the string ABCABC in 55 keystrokes or less:


A B C * *
* A B C *
* * A B C

If we want to know the probability that the monkey typed ABCABC in the first 55 strokes, we need to take the probability of the union of the three events above. By looking at the third character in each string, we can see that the three cases are mutually exclusive. Therefore, we can just add their probabilities. The probability of the first case is clearly 32/353^2/3^5 so the probability of the union is 33/35=1/93^3/3^5=1/9. In other words the probability that the monkey types ABCABC in 55 keystrokes or less is 1/91/9. On the other hand for the ABAABAcase we have events:


A B A * *
* A B A *
* * A B A

And again the probability of each event is 1/271/27, but we can no longer add the three events since they are no longer mutually exclusive. We can see that the first and third cases overlap, they both contain the sequence:

A B A B A

And that is the only string that they have in common. So by the inclusion - exclusion principle we need to subtract that case out to get the probability as (271)/35(27-1)/3^5 Which is lower by 1/2431/243.

And now it should be clear why the average time for the monkey to type a word is longer if the word has repeated sub-strings. For any given time the probability of typing the string with repeated sub-strings is lower so it must take longer on average for the event to happen.