# How I solved a cool probability problem

A couple of weeks ago I came across a very interesting math puzzle when a friend told me to check out the Jane Street current puzzle of the month. From a first look, it might seem like it’s a simple problem, but it does get quite tricky. In the end, my solution turned out to be weird enough that I thought it could be cool to write about it.

The puzzle was the Robot Tug-of-War from August 2021. Here’s the problem statement:

The Robot Weightlifting World Championship was such a huge success that the organizers have hired you to help design its sequel: a Robot Tug-of-War Competition!

In each one-on-one matchup, two robots are tied together with a rope. The center of the rope has a marker that begins above position 0 on the ground. The robots then alternate pulling on the rope. The first robot pulls in the positive direction towards 1; the second robot pulls in the negative direction towards -1. Each pull moves the marker a uniformly random draw from [0,1] towards the pulling robot. If the marker first leaves the interval [‑½,½] past ½, the first robot wins. If instead it first leaves the interval past -½, the second robot wins.

However, the organizers quickly noticed that the robot going second is at a disadvantage. They want to handicap the first robot by changing the initial position of the marker on the rope to be at some negative real number. Your job is to compute the position of the marker that makes each matchup a 50–50 competition between the robots. Find this position to seven significant digits-the integrity of the Robot Tug-of-War Competition hangs in the balance!

If you like this kind of puzzle, maybe you should stop here if you want to try to solve it before I spoil it for you. 🙂

# First Steps

When I first read the problem, it screamed recursive probability function definition to me: When it’s player1’s turn they have a certain chance to win. If they don’t, player2 plays and if they also don’t win, it is player1’s turn again and we’re back to the same initial situation (or sort of, because the marker position player1 is playing from now has likely changed).

In order to proceed, let’s describe a couple of functions that I’ll start referring to in my explanations:

So basically we need to find a start marker position `s` such that `p1(s) = 0.5`.

I quickly found a simple (and wrong) solution for it. It sounded too easy, and it was. My mistake was to assume that the mean value of `p1(x)` (and similarly for`p2(x)` as well) in an interval `[a, b]` was `p1((a + b) / 2)`. Once I realized that was not the case, the problem became much harder.

I kept trying to find an analytical definition for `p1(x)`. And after thinking about it for some time, I was take to take the following steps:

The first part is pretty simple, it’s just the part of player1’s movement distribution that’s after 0.5, so `x + 1 - 0.5`.

The second part is much trickier. Since player1 didn’t win immediately, it will leave the marker at some position y and it will be player2’s turn. The chance of player1 winning now, is the chance that player2 loses, which is `1 - p2(y)`. The problem is that `y` varies according to player1's movement distribution, so we need to integrate `1 - p2(y)` over the possible values of `y`, so from `x to 0.5`. The integral is unweighted since player1's movement distribution is uniform in an interval of size 1, so

We note that there’s a symmetry between `p1(x)` and `p2(x)`: the chance of player1 winning when they start from a marker position `x` is the same chance of player2 winning when they start from position `-x`. So `p1(x) = p2(-x)`. Using that, we can replace `p2(y)` in the integral expression by `p1(-y)`.

Putting everything together:

So yeah, we found a recursive function definition after all! Although one with an unexpected integral getting in the way.

At this point, my differential equation solving skills failed me (it’s been a long time since I’ve sharpened them in college) and I got stuck. This is also where my solution started to get more weird (or interesting depending on your point of view haha 😅). As I couldn’t find a way to continue solving the problem analytically, I started looking for ways to approximate this integral.

# Solving p1(x) using discrete integral approximation

My idea came from numerical integral approximation, basically I would break the integration interval in blocks of size `step`, i.e. `[x, x+step]`, and approximate the area of each block by the value of the function at it's beginning times the `step` size, i.e. `f(x) * step`. As `step` gets smaller, the approximation gets better.

For illustration purposes, let’s approximate `p1(x)` using `step = 0.1`. For that we will evaluate `p1(x)` at the following set of points: `{0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4}`.

Now we have a system of 9 equations and 9 variables, we can solve it, and use as an approximate answer the value among those nine for which `p1(x)` gets evaluated closest to `0.5`! Then we can just keep reducing the step size to get a better and better approximation until we think we've got confidence enough on the seven significant digits that we need. 🙂

Initially that system of equations looked a little bit ugly and I was getting afraid it could be inefficient to solve (such that if I reduced the step a lot, I’d have too many variables and it would get so slow that it’s infeasible to compute), but then I got quite excited when I found a way to solve it in O(N)!

To see that linear way to solve it, from the second equation forward we subtract from each equation the one that is right above them (and we just keep the first equation as is):

Starting at the equation in the middle, and then looking in sequence at one bellow, then the next above the block, and then the next bellow the block, and so on, we can quickly express all variables in terms of `p1(0)` (i.e. let's represent `p1(x)` as `c(x) * p1(0)`, and find `c(x)` for every `x`, starting with `c(0) = 1)`. We do this for all equations except the first, which will be left out:

Now that we finally have the values for every `c(x)`, we can just use the first equation to find `p1(0)`:

Then we use `p1(0)` combined with each `c(x)` to find every other `p1(x)` and pick the one that's closest to `0.5`.

Looking back at how we solved each `c(x)`, we can see there's a rather simple pattern to determine them at pairs iteratively. Starting from `i = 0` and incrementing it by one every loop:

As I’ve been playing with Rust lately, of course I went ahead to implement this solution using it haha. Initially, I thought I had to store every `c(x)` in memory to solve the problem. That would limit how much I could decrease the step: for example, using a `step` of `1.0e-9`, I'd have to store `1.0e+9` 64-bit floating point variables (one for each `c(x)`), which would amount to `8GB` of RAM already.

It was then that I realized that my iteration method to find `c(x)` values only depended on the last two values computed! So the loop could actually run with constant memory (independent of the step size). I would just have to run it twice: once to find `p1(0)`, and a second time to inspect every `p1(x)` to find the one that is closest to `0.5`. This insight allowed me to use even smaller step sizes to build more confidence in my solution, like `1.0e-11` (which would require `800GB` of RAM if I had to store each `c(x)` in memory). I even tried using a smaller step once (it starts to take a lot of time to run, `1.0e-11` already takes a few minutes), but it looked like I started hitting some underflow issues with 64-bit floating point representation.

Here’s my Rust code that implements this:

Results:

Although I didn’t find a way to formally prove that my solution had at least seven correct significant digits, it looked like the first seven digits were getting stable enough (as I reduced the step size) to trust that they were the correct ones.

After a couple of hours struggling to make progress, I was quite excited and happy that I found such a simple algorithm to solve the problem! 🙂

# Final Thoughts

After I was satisfied with my solution, I still had one question at the back of my mind: “Does this problem have an analytical closed-form solution?”. It reminded me of the goat problem which got popular since it just got solved in closed-form recently.

Turns out, it does! When a solution was posted on the Jane Street website, I was quite delighted to see that they provided a closed-form solution:

Now look at that solution. It’s full of trigonometry expressions! Imagine how puzzled someone who studied probability, but has never seen trigonometry before would have been if they were presented this problem and that solution. That’s something that always keeps coming back to amaze me when I see a new example of it happening: how we can find so many connections between apparently unconnected areas of Mathematics.

Originally published at https://lucamoller.com.

I'm a Computer Engineer interested in all sorts of stuff. Check out my personal website at lucamoller.com

## More from Luca Mattos Moller

I'm a Computer Engineer interested in all sorts of stuff. Check out my personal website at lucamoller.com