Monday, 13 April 2020

Urns and Balls

This classical problem describes a puzzle where one has to divide black and white marbles over urns, in such a way that if we select an urn at random, and then select a random marble in this urn, the chance of us drawing a white marble is maximized. The solution to this problem can be found in many places on the internet, for example here.
This problem can be generalized by dividing $w$ white balls and $b$ black balls over $u$ urns, but the solution stays quite simple, one always has to put a single white ball in each urn, and put the remainder in the last urn.
To make the problem more interesting, we can add an extra condition to the problem, each urn must contain at least one marble of each colour. This version of the problem is has a less obvious solution, as for 50 white and black marbles, with 2 urns we have to put 1 black and 12 white marbles in the first urn and 38 white and 49 black marbles in the second.
To solve the harder version of the problem, let us first develop a framework to describe and deal with any general case. The technique described in this blog is a technique that is similar to a technique used in quantum mechanics, which is for example used to calculate the division of electrons between different atoms.

The framework

First, let us call the chance to draw a white marble times the number of urns "Energy", due to equivalence with quantum mechanics. As the number of urns is constant, maximizing the chance is equivalent to maximizing the energy. Note that in quantum mechanics, we often minimize the energy, but the concept is the same.
If we call the number of white marbles in urn $i$ $w_i$ and the number of black marbles $b_i$, then the energy can be written as
$E= \sum \frac{w_i}{t_i}$
Where $t_i=w_i+b_i$ is the total number of balls in each urn.
The workfunction is the energy needed to remove a marble from an urn, or in our case, the energy difference between the urn and a similar urn with one marble less.
For a white marble, this is
$W_w =  \frac{w_i-1}{t_i-1} - \frac{w_i}{t_i} = \frac{-b_i}{t_i(t_i-1)}$
and for a black marble this is:
$W_b =  \frac{w_i}{t_i-1} - \frac{w_i}{t_i} = \frac{w_i}{t_i(t_i-1)}$
Similarly, we can calculate the energy to add a marble
$T_w =  \frac{w_i+1}{t_i+1} - \frac{w_i}{t_i} = \frac{b_i}{t_i(t_i+1)}$
$T_b =  \frac{w_i}{t_i+1} - \frac{w_i}{t_i} = \frac{-w_i}{t_i(t_i+1)}$
As the total number of marbles is set by the problem statement, one cannot add or remove marbles, but one can move them from one urn to another.
The energy cost to move a marble from urn i to urn j is:
$W_{ij} = \frac{b_j}{t_j(t_j+1)} - \frac{b_i}{t_i(t_i-1)}$
$B_{ij} = -\frac{w_j}{t_j(t_j+1)} + \frac{w_i}{t_i(t_i-1)}$
Where W moves white marbles and B moves black marbles.
If we move a white marble from $i$ to $j$ and a black marble in the opposite direction, the energy cost is:
$\frac{1}{t_i}- \frac{1}{t_j}$
Not all of the actions are always valid. As there are sometimes restrictions on the number of marbles in each urn, we can only move the so-called "unpinned" marbles. For example, if one has to have a least one white marble in an urn, an urn with 3 white marbles is said to have 2 unpinned and one pinned marble.
Remember that we are looking for the configuration with the highest energy. This means that none of the three previously considered actions -moving white, moving black and moving both- are allowed to increase the energy. This means that the cost should always be positive, for every valid action that can be done in the configuration. While this is necessary, this is not sufficient, it could be that a configuration is locally optimal, but not globally.
Let us now only consider the action of moving a black marble in one direction and a white one in the other. Moving the black marble from the smaller to the bigger urn (and a white one opposite) will always increase our energy. This means that in the optimal configuration, at least one of the following statements is true for each pair of urns:
  1. Both urns have an equal number of marbles
  2. The bigger urn has no unpinned white marbles
  3. The smaller urn has no unpinned black marbles
This results intuitively makes sense, as if the number of marbles is already decided, we would want to move the black marbles to urns with a larger amount of marbles, to decrease the chance that they are drawn, and move white marbles to urns with a lower number of marbles. If two urns have the same about of marbles, the chance that each one of them is drawn is equal, so moving them around does nothing.
If two urns have the same number of marbles, the energy cost of moving a single black marble is:
$B_{ij} = -\frac{w_j}{t(t+1)} + \frac{w_i}{t(t-1)}$
what can always be made smaller than zero by swapping $i$ and $j$, so, in the first condition, one must add the extra condition that one of them must have no unpinned black marbles.
The same technique applies in the second case, where the bigger urn contains no white marbles that we are allowed to move. If the bigger urn is called j, then is $t_j > t_i$ and $w_j \leq w_i$ making
$B_{ij} = -\frac{w_j}{t_j(t_j+1)} + \frac{w_i}{t_i(t_i-1)}$
Always bigger than zero, so we can add the same condition as before to the second statement. This combines all statements in a single statement, being "For each pair of urns, one of them should have no unpinned black marbles" or equivalent:
All unpinned black marbles must be concentrated in a single jar
We will from now on refer to the jar with all the unpinned black marbles as the black jar, and to the other jars as the white jars.
We now know that the number of black marbles in the white jars are all equal (because they are all pinned), so the energy to move a marble from one jar to another is:
$W_{ij} = \frac{b}{t_j(t_j+1)} - \frac{b}{t_i(t_i-1)}$
This allows two jars to have at most a difference of one white marble between each pair. By consequence, there is only one single configuration possible for the white marbles in the white jars, namely distributing all of them equally, or giving some urns one extra if an equal distribution is not possible. This leaves only a single free parameter $\omega$ in our system, which is the number of white marbles in the white jars (the remainder is added to the black jar). For each system, the total energy can be calculated in the function of this variable, and then the maximal value can be selected.
Another approach is to check if moving a white marble from the white to the black jars increases the energy. The energy to move a white marble from the white to the heavy jars is:
$W_{ij} = \frac{b_P}{(b_P+b_W+\lceil\omega/(n-1)\rceil)(b_P+b_W+\lceil\omega/(n-1)\rceil+1)} - \frac{b_P+b}{(b_P+b+w-\omega)(b_P+b+w-\omega-1)}$
This will flip sign if the optimum is reached, so one can put it equal to zero to find the optimal omega.
Note that, unless in very specific edge cases, white marbles are always distributed, so the number of pinned white marbles rarely matters.

The original problem

In the original problem, there where no pinned marbles, all of our marbles are free. The number of jars also was two, leaving us with a single black and a single white jar. the total energy is thus
$\frac{\omega}{\omega} + \frac{w-\omega}{w-\omega +b} = 2- \frac{b}{w-\omega +b} $
This has no value if $\omega$ is zero, and is obviously larger is omega is smaller, so the optimal solution is $\omega=1$, which means that the optimal solution is equivalent to the solution found in the other sources.

The new problem

For the new problem, the energy can be optimized computationally.
Our starting problem had 2 urns, 50 marbles of each colour, and at least one black marble in each urn (also a white one, but this does not matter). Using this, one gets 12 white marbles and one black in the first urn, and 38 white with 49 blacks in the second.