Remembrance of Things Just A Moment Ago
Subtitle: In Search Of Time Lost Playing This Game
# The Memory Game
Jan created an online version of the Memory Puzzle for his daughter Sofie and son Senne, which he calls Shinkei-Suijaku, Japanese for “nervous breakdown”. The game consists of pairs of cards placed randomly face down on a table. The player selects two cards to flip over, and if they match the player scores a point. The game may be played in solitaire mode, or with two players alternating turns.
Jan built a timer into the game, so you’ve got 60 seconds to find all eight pairs. When you win, the crowd cheers, confetti falls down your screen and your reward is:
Not satisfied with simply playing the game, Jan and I had to analyze it.
# What’s the worst that can happen?
The first question was, if you play totally randomly as if you couldn’t remember from one turn to the next where any card is, how many turns would it take to find all of the pairs?
Suppose you’ve found seven of the eight pairs, so two cards remain on the table. Whichever card you choose first, the second will match it, and the probability of finding a match on the last turn is one.
Just before the last turn you’ll have four cards face down on the table. You choose any one first, and one of the remaining three second. Since only one of those three matches the card you picked first, the probability of matching the seventh pair is .
Continuing backward from the end of the game, there will be six cards on the table for the sixth match. One of five will match the first card you flip, so . At the start of a sixteen card game, you’ll have fifteen choices for your second pick with only one possible match, so .
For a game with pairs, the probability of randomly matching the pair is
is the probability of making a match on each turn, but it doesn’t say how many tries you’d need to get each match.
# The Negative Binomial Distribution
A Bernoulli trial is a random event with two possible outcomes, such as the flip of a coin - either heads or tails, or the roll of a die in which it comes up 4 or it doesn’t. Often, one outcome is called “success” and the other “failure”, but the name can be misleading. If we ask how many tosses of the die will I likely make before I get a 4, then the rolls that aren’t 4 are “successes”, and the 4 becomes a “failure”. Sometimes you’ll find the definition reversed, so you should carefully understand the context for each problem.
A somewhat more general question might be, what is the probability of getting three 4’s in ten rolls of a fair die? Or, you might ask, what is the probability of successes out of events?
The first thing to do is figure out how many ways you can have successes out of events. The first events could all be successes, followed by failures, or it could be one of many other possible combinations. The formula for the number of ways to get out of is
where . To calculate the number of combinations of events out of trials,
Notice that cancels the last 7 multiplications in making the calculation much simpler. Often, you’ll find other cancellations such as into and into , which leaves .
In the case of rolling a die, the probability of getting a 4 is , and the probability of getting something else (not 4) is . In our 10 trials, we want to get exactly 3 successes and 7 failures, or . Multiply by to get the probability of exactly 3 successes out of 10, about .
If you want to know the probability of getting at least 3 out of 10, then you’d take 4 out of 10, 5 out of 10, and so on as successes. Do the same calculation for each of those outcomes, and add up the total. Even easier, calculate 0, 1, and 2 successes out of ten and subtract the answer from 1.
# Just One Win
In Shinkei-Suijaku, once a pair is matched, the two cards are removed from the table, so we’re looking for one success or . We don’t need to calculate the number of ways we can get successes out of trials because we’ll stop as soon as we get the first success.
Suppose the probability of success is and we take tries before getting a match. Then the probability of this happening is
where is the probability of success on the event. Looking at the first few possible outcomes,
How many trials do you expect to need to get the one match? The expectation is the sum of the probabilities of each possible outcome, times the number of trials,
Factoring out of the right side,
Now, multiply both sides by
and subtract the second from the first,
Factoring out from both sides leaves
Since , then the quantity is also between and . Then is a geometric series and can be simplified to
In other words, the expected number of trials to get a match is the inverse of the probability of obtaining the match on any one trial. At the beginning of the game, we found that , so we can expect, on average, we’ll need attempts to find the first match if we randomly flip pairs of cards over.
for a game with pairs during the match, then the expected number of trials is
and the expected total number of trials is
For the game with pairs, we should expect to take attempts if we pick pairs randomly. Since Jan gives us just 60 seconds to finish the game, we’ll need to select pairs slightly faster than one per second.
# What’s the best that can happen?
Now, suppose we have perfect memory and can instantly match any card we’ve seen previously. The best case would be if all pairs were in order, so the first two cards we pick are a match, the second two are a match, and so on. Then the game would be over in eight turns. But, we have to assume that the placement of the cards is random as well.
Imagine the cards are laid out in a line. We flip the first two, then the next two, and on to the end. It will take eight turns, and now we know exactly where all of the pairs are, so at most we’d need another eight turns to complete the game.
But, two things might happen that will shorten the game. First, we might get lucky and two successive cards would match. More likely, when we flip the first of the pair, we might recognize a match from a card we saw earlier. This is a screen capture from a game I played where I flipped pairs along each row beginning in the top left corner.
Amazingly, everything matched and I even got a matching pair on the second try. In the lower right, the first card flipped was the orangutan which I was able to match with a card seen earlier, and then the last card matched the first. So, this took six total trials to find four matches.
# Eidetic Memory
Eidetic, or photographic memory seems to be a thing that isn’t a thing. Harvard scientist Charles Stromeyer III published An Adult Eidetiker in 1970 about an artist named Elizabeth who apparently could remember images perfectly from one day to the next. The paper appears to be a hoax, though. Elizabeth was never retested, nor could anyone else demonstrate the same capability. But, let’s assume we do have perfect memory.
How often should we expect to see a match to a card seen earlier? If there are cards, with perfect memory we need no more than matches. But, let’s say we’ve looked at pairs, and flip over the first card of the pair. In the example above, maybe I would have checked the first three pairs, and on the fourth pair, the first card is the orangutan I saw in the previous pair. Instead of flipping the next card (the kangaroo), I score a point by flipping the other orangutan.
The probability the first card of the pair has a match among the previous cards is
since I’ve seen other cards already, and there are a total of cards that might match the one I’ve just flipped over.
If there isn’t a match among the cards I’ve already seen, there’s still a small chance the first card of the pair matches the second. Since only one card matches the first one, and there are other cards,
Letting be the required number of turns in a game, we know at most it will take turns, but we can save a few turns along the way. Since we’re going to look at pairs of cards on each turn, only about half of the turns will result in a match, so I’ll divide and by .
The trick for calculating the sum of the first numbers is in The Sum of the Sum of Some Numbers.
# Every Expectation
Trying to map out the possible ways a game could go gets out of hand very quickly. To analyze the play, start with a game of just two cards, and label them as . The number label doesn’t indicate anything about the picture on the front of the card, it’s just a way to order the cards. Obviously, with only two cards in the game, they have to match and in one turn you’re done.
Next, consider a game with two pairs where we’ll label the cards from left to right as . On the first turn, you flip over cards and . Since there are three other cards besides the first one (), the probability of getting a match on the first turn is . If you do get a match, then you’ll also get a match on your second turn when you flip cards and .
The notation is when card is a match for card , and when they don’t match. The numbers in parentheses are the probabilities of a match.
The expected number of turns is the probability of taking a path times the number of turns, so .
The more likely path, with probability , is the two cards won’t match. But then you know exactly where the cards are, so you can match both in the next two turns. The expectation for this path is . The total expectation for both paths is .
For a game with three pairs labeled , there’s a probability of a match on the first turn, and then we can copy the paths we took previously. Otherwise, we need to check every possible outcome when the first two cards aren’t a match. As you can see, the number of possible paths grows rapidly, and my calculations are probably wrong somewhere!
# A Monte Carlo Experiment
Imagine trying to expand the analysis to pairs, or cards! An easier way to get close to the answer is to use a Monte Carlo simulation (See The Boonie Conundrum for another example of the Monte Carlo method and how the casinos at Monte Carlo played a role in the development of the first atomic bomb.) A Monte Carlo simulation runs a random experiment many times and looks at the statistics of the outcome to get insight into a problem.
A nice feature of simulations is you can abstract away unnecessary features. By this I mean that if some part of the problem isn’t needed to get your answer, you shouldn’t include it in the calculation.
We’d like to know, on average, how many turns it will take to solve the Memory Game if you have Eidetic memory. As we simulate the game, we need to count turns, but we don’t need to know which cards matched. If you turn over a card, there’s only one other like it on the table, so the question you need to answer is, “Have I seen this card before?” Other than keeping track of where you are in the list of cards, nothing else matters.
This is an example of a game with matches, with numbers representing the icons found on the front of the cards:
The first turn will be used to look at the first two cards, so we can ignore it. If we have a counter called
turns we can set it to , and move on. We need to keep track of the next card we’ll turn over, so initialize the card counter
k = 3, meaning we’ll begin our game at card .
Have we seen card before? Is there a somewhere in the list before this card? If there is, we’ve found the one and only match for that card. The way to do this is to ask the computer,
cards[k] ∈ cards[1:k-1]
The symbol means “in”, so the code asks, “Is the card somewhere in the first to cards?” If it is, we’ve found the match. If not, the match is farther down the list.
If the and cards match, the turn is over and we skip forward to the card. If not, does the card match one we’ve seen earlier? If so, take another turn and then move forward in the list.
Continue this process until you’ve looked at everything but the last two cards. If the last two cards are a match, you’ll need one turn to look at them. If they don’t you’ll use one turn each to match them with cards you’ve seen earlier.
Here’s the flowchart of the process made with the free Flowchart Maker & Online Diagram Software:
Put this into a loop, run it a bunch of times and you’ve got a Monte Carlo experiment. How many times is a “bunch”? One way to think about this is to ask how many ways can you shuffle cards? The answer is you can select the first card different ways, the second card ways, and so on.
This is ( factorial), but each card has an identical match so there are only possible shuffles. For cards, there are ways to shuffle them (that’s 10 quadrillion!). You need to repeat the experiment until you’ve sufficiently sampled the possible permutations, but I won’t get into the details here.
# The Results
I wrote the code for the Monte Carlo experiments in Julia and ran experiments using cards. For each experiment, I calculated how many possible shuffles there were, chose the number of Monte Carlo iterations, recorded the expected number of turns, and the expected number divided by the number of card pairs.
|# cards||# shuffles||# MC||E[X]||E[X] / # matches|
Using Veusz, I plotted per match for each of the experiments:
which shows the ratio is a bit over . For the case with four cards, , a bit lower than the analytic estimate of , and for six cards the Monte Carlo estimate was , while the analytic solution was . So, the analytic method seems to be a bit off.
But, enough Shinkei-Suijaku. This is your