# The story of two lovers and the beauty of statistics

[The original title for this post was “The story of two lovers, joint densities and the beauty of statistics”. I dropped the “joint densities” from the title]

Recently a friend of mine gave me a puzzle. The puzzle is as follows:

Many years ago there were two people, secretly in love, and were traveling with a group of friends who did not know they are in love. The large group of friends were going around Europe by bus. Stopping in major cities and staying in hotels. In the long hallway of hotels, every person would get a separate room. Every night one of these lovers would sneak out of their room and go to the other’s room for an innocent cuddling. The puzzle is: if the long hallway is L meters long and rooms are uniformly spread along the hallway. What would be the average length that one of them needs to tiptoe to reach the other one.

Depending on how you want to solve this problem this can be an easy or a relatively hard problem. We will solve it two ways. First we can solve it with simulation and next we can validate the simulation with an analytical solution.

Let’s first simplify the problem. Assume the location of one of them is $X \sim U(0,L)$ and the other is located at $Y \sim U(0,L)$. We want to find the average value for \$latex Z= |X-Y|

And the histogram of $Z$ values is shown below. $Z$ is following a triangular distribution.

If R is your favorite language then you might find the rgl and akima libraries pretty useful here. You can produce visualizations of Z values (vs X and Y) using R.

This problem can be solved in 6 lines of python or R (perhaps two line if you don’t mind a compressed notation). But can how can we find a closed form solution for this?

Analytical solution
We have $X \sim U(0,L)$ and $Y \sim U(0,L)$. We want to find the average value for $E[Z]= E[|X-Y|]$. Additionally we know that $Y$ and $X$ are independent $X \perp Y$. The probability density function for $X$ and $Y$ are $f_X(x) = 1/L$ and $f_Y(y) = 1/L$. Since the two variables are independent then the expectation of the joint probability is in the form of:

$E[Z] = \int_0^L \int_0^L |x-y|f_X(x)f_Y(y)dy dx$

By replacing $f_X(x) = 1/L$ and $f_Y(y) = 1/L$ we have:

$E[Z] = \int_0^L \int_0^L |x-y| \frac{1}{L^2} dy dx$

We can now remove the absolute value by conditioning on the value of $X-Y$. Meaning, if $X-Y>0$ then $|X-Y| = X-Y$ otherwise $|X-Y| = Y-X$.

$E[Z] = \frac{1}{L^2} \int_0^L \int_0^x (x-y) dy dx + \int_0^L \int_x^L (y-x) dy dx$

Therefore:

$E[Z] = \frac{1}{L^2} \int_0^L (xy-1/2y^2)_{y=0}^{y=x} dx + \int_0^L (1/2y^2-xy)_{y=x}^{y=L} dx$

This is equal to $\frac{1}{3} L$. So the two lovers would, on average, walk one third of the hallway to see each other.