The Dating Problem

I first found out about this problem through a YouTube video by Viks recommended to me by the algorithm around two years ago.1 This post will be somewhat of a ramble expanding on the problem proposed by this video. They're not the first one to think of it, but it was my first introduction to the problem (I later found out that Numberphile made a video on it as well, in the context of toilets2).

For the people who haven't watched it, it's probably best to explain what I'm talking about first:

Suppose you want to find the very best person to settle down with. You meet people one by one, and you have to decide if you want to settle down with them or reject them and move on to the next person. Once you have rejected someone there's no going back to them anymore. How do you determine who you settle down with?

It's a very simple problem, with an elegant mathematical solution. I won't go into too much depth (watch the videos for more context), but the strategy is to reject a number of people first (regardless of how nice they are) and then to accept the first person better then all of those.

The problem can be solved mathematically as follows. Suppose you reject the first r people out of n total people. Then the probability that you end up with the very best person is

P(\text{Success}) = \frac{r}{n} \sum_{i=r+1}^{n} \frac{1}{i-1} \approx \frac{r}{n} \ln\left( \frac{n}{r} \right).

Differentiating with respect to r and setting the expression to zero gives

P^\prime(\text{Success}) = -1 - \ln \left( \frac{r}{n} \right) = 0,

so

r = \frac{n}{e} \approx 0.37n.

This was the conclusion reached by both videos: to find the very best partner (toilet) you have to reject 37% of your options first, before accepting the first one better than everything so far. Doing so you have a 37% chance of finding the very best partner.

Having gotten this introduction out of the way, there is something that irked me about this solution: no-one actually decides based on only finding the very best partner. What if you don't get your number one pick (which happens in 63% of cases)? Then we end up having to settle with someone we might not like too much. To me it makes much more sense to optimize for a best average. That is, to get the best partner we can without having to settle for someone worse if we fail.

The next part will be a bit more math-heavy. If you just want the results, you can skip to the conclusion below (somewhat hidden in the middle, sorry).

Let X be a random variable representing the "goodness" of the partner chosen (from 1 being the worst to n being the best). For context, the previous problem could be formulated as

P(\text{Success}) = \mathbb{P}(X = n).

We would like to maximize the expected value of X:

\mathbb{E}[X]

We can calculate this expected value by introducing another random variable M, which is the best partner we found in the first r people. Lets first consider the case where n (the very best partner) is in the first r people, so M = n. This happens r in every n times, so

\mathbb{P}(M = n) = \frac{r}{n}.

Next, if the second best partner is the best person in the first r people, then M = n-1. Meaning that n is not in the first r people, but n-1 is.

\mathbb{P}(M = n-1) = \left(1 - \frac{r}{n}\right) \frac{r}{n-1}

Notice that if n is not in the first r people, then the chance that n-1 is in there becomes slightly larger, since n is now "blocking a spot" in the last n-r places. We can do this again for n-2:

\mathbb{P}(M = n - 2) = \left(1 - \frac{r}{n}\right)\left(1 - \frac{r}{n-1}\right)\frac{r}{n - 2}.

Convince yourself that this pattern repeats:

\mathbb{P}(M = n - k) = \frac{r}{n - k} \prod_{i = 0}^{k - 1} \left( 1 - \frac{r}{n - i} \right).

There is one asterisk we have to take note of. What happens when n-k is less than r? There are r people, so the maximum has to be at least r as well. In this case we will define the probability to be zero.

Now finally, we will clean up our probability function a little bit

\begin{align*}
\mathbb{P}(M = n - k) &= \frac{r}{n - k} \prod_{i = 0}^{k - 1} \frac{n - i - r}{n - i} \\
&= \frac{r(n - k - 1)!(n-r)!}{(n-k-r)!n!}
\end{align*}

You can check for yourself that this is true. Then, we can use this for our expected value. If we have that M = n-k (for k > 0), then the partner we end up with will be any of

n - k + 1, n - k + 2, ..., n

and there is an equal chance to end up with any one of them. The average value will then be

\frac{(n - k + 1) + (n - k + 2) + ... + n}{k} = \frac{2n + 1 - k}{2}.

For the last case, where k=0 so M = n, there is no partner better than any one we have already encountered. This is kind of a worst case scenario and we choose the very last person we meet. This will be anyone up to n-1 (with equal chance for each one), so

\frac{1 + 2 + ... + (n - 1)}{(n - 1)} = \frac{n}{2}

is our expected value in this case.

Putting everything together, we can say that

\mathbb{E}[X] = \frac{n}{2} \mathbb{P}(M = n) + \sum_{k = 1}^{n-r} \frac{2n + 1 - k}{2} \cdot \mathbb{P}(M = n - k).

In the end, this simplifies quite nicely (exercise for the reader, bla bla bla) to

\mathbb{E}[X] = \frac{(n + 1)(2nr + n - r^2 - r)}{2n(r+1)}.

Then all that is left is taking the derivative with respect to r, setting it zero and solving for r:

\frac{\partial\mathbb{E}[X]}{\partial r} = \frac{(n + 1)((r + 1)^2 - n)}{2n(r + 1)^2}=0,

giving us the solutions

n = -1 \lor r = \pm \sqrt{n} - 1.

Obviously, n and r are greater than zero, so the only solution remaining is

r = \sqrt{n} - 1.

Nice.

After all this math we can finally say that the amount of people we need to reject is exactly one less than the square root of the total. Out of 100 people we reject the first 9. Out of 10000 we (only!) reject the first 99, then accept the first one better than those 99. Amazing right?

In the case that we don't end up on a whole number, we have to round either up or down. I can't be bothered to find out in which case you have to round down and when to round up.

There are a couple of things that amaze me about this result. First of all, that all these complex formulas collapse so nicely. But more specifically the edge case that we added. The idea that removing the k=0 term from the series and replacing it with our edge case is necessary to get a simple result is just crazy. If we had just defined our expected value as

\mathbb{E}[X] = \sum_{k = 0}^{n-r} \frac{2n + 1 - k}{2} \cdot \mathbb{P}(M = n - k)

we would have had no solution for r at all, and if we had left the first term out completely (meaning if we encounter the very best person in the first r people, we reject everyone and end up completely lonely) we would have gotten

r = \frac{\sqrt{n(n + 1)(2n + 1)}}{2n+1} - 1

as a result. Which is much less nice (though still somewhat nice).

A final thought (or almost) is about what the expected value would be. Plugging our solution into E gives

\mathbb{E}_{\text{max}}[X] = \frac{3}{2} + n + \frac{1}{2n} - \frac{1}{\sqrt{n}} - \sqrt{n}.

Though this is not exactly a clean formula for specific n, writing it in this form clearly shows that

\lim_{n \to \infty} \frac{\mathbb{E}_{\text{max}}[X]}{n} = 1

meaning that for large enough n, we can on average get the best (though not the very best) partner we want. This is in contrast to the original solution, where plugging in

r = \frac{n}{e}

gives

\mathbb{E}_{\text{original}}[X] = \frac{(n + 1)((2 - \frac{1}{e})n - 1 + e) }{2(n + e)}

so

\lim_{n \to \infty} \frac{\mathbb{E}_{\text{original}}[X]}{n} = 1 - \frac{1}{2e} \approx 0.82.

Meaning that on average, that strategy will never give you perfect results. On the other hand, plugging in our new solution to the old probability to get the very best partner,

\mathbb{P}(X = n) \approx \frac{\sqrt{n}}{n} \ln{\frac{n}{\sqrt{n}}} = \frac{\ln n}{2 \sqrt{n}} \to 0.

Of course, this is all theoretical as we all know the chance of actually finding more than one date is basically zero.

PS. Some equation were very hard to solve by hand, so I did it the lazy way. Here is the code I used to solve some of the more egregious equations:

from sympy import *

n, k, r = symbols('n k r')

P = ( r*factorial(n - r)*factorial(n - k) ) / ( (n - k)*factorial(n - k - r)*factorial(n) )
W = (2*n + 1 - k)/2

E = r/2 + Sum(P*W, (k, 1, n - r))

F = E.doit().simplify()
print(F)

D = diff(F, r).simplify()
print(D)

S = solve(D, r)
print(S)