The nested means problem.
This month I have been introduced to the following curious problem:
Take the numbers from 1 to 12. Put them into groups. E.g. (1, 2), (8, 10), (3, 4, 5, 6, 7, 9, 11, 12). Average each group e.g. 1.5, 9, 7.125. Then average those averages: 5.875. How do you group the numbers to get the minimum?
In this blog post I will try my best to answer the following questions:
- What is the solution to the original puzzle?
- What about the range from 1 to ?
And elaborate on the following open question:
- What if groups can be nested times, instead of just two times?
Diving in⌗
To solve this problem and gain some intuition about it, I will use Dyalog APL. I start by defining the average function and verifying the numbers given in the problem statement:
To reason about this problem, recall the Simpson Paradox, which states that an average of averages does not necessarily yield an average overall. The average of numbers from 1 to 12 is 6.5. Notice that:
The average has decreased below the expected 6.5, but is it the best that we can do? Notice that it is trivially more efficient to group small numbers into separate mean groups:
Knowing this, we can attempt at discovering the mean that taking first numbers to separate groups yields, for up to 11:
Hence, the optimal grouping that solves problem 1 is as follows: (1) (2) (3 ... 12)
.
elements.⌗
Let’s start by modifying the model to actually compute the minimum element:
Mapping this function over ranges from 3 to 33 yields a curious pattern:
The pattern can be explored further if we pick the largest possible partitioning and group the result:
This sequence is clearly derived from the integer square root function - a(n) = floor((-1 + sqrt(1+4*n))/2)
- as evidenced below:
Hence, the optimal grouping that solves problem 2 is the one that puts the first numbers in separate groups, and then the remainder in the last group.
But why?⌗
The intuition is clearly sufficient to derive a solution with plenty of evidence to back it, but let’s try to prove it now.
We will consider the following function which models our averages ( is the amount of single groups, is the amount of numbers in total):
The function can be trivially simplified as follows. Start by writing the sums in a closed form:
Simply fractions:
We want to find the value of for which the function has the smallest value. Obviously:
It is sufficient that the partial derivative is zero for a fixed at some point . The formula for critical points is rewritten as:
Which trivially yields . This result is admittedly different from the result obtained previously: a(n) = floor((-1 + sqrt(1+4*n))/2)
, but the discrepancy is easily explained by rounding - after all, does not have to be a whole number and our choice of a(n)
was skewed towards the last index of the smallest value; notice that the following holds for .
Elementary operations assuming yield:
Notice that since, by squaring both sides, , hence , which holds since . Substituting yields , which is trivially true by natural induction.
Nested groups.⌗
Combining the observations from the previous sections, we can now tackle the problem of having nested groups. Following the previous schema, the optimal result is in the form . The helper function used in this scenario is as follows:
If were not fixed, the most optimal layout would trivially be 1 1 1 1...
.
The question is as follows:
Given (top of the range) and (amount of groups), determine the formula for the terms such that the average is minimised. For example, given and ,
c=2 4 25
A special case: 3-deep nesting.⌗
Trivially, the following function models c
for 3-deep nesting (the proof can be formulated following the reasoning from previous sections):
Since to get a solution for the range up to , notice the following partial derivatives of :
Since the denominators are irrelevant in finding the critical points (poles only for improbable values of ), the second equation relates to and :
Substituting gives an equation that relates and :
This equation will always have a few solution but it is remarkably easy to pick the correct one: is at least , so must be smaller than by at least 4. Neither nor may be negative. The equation is impossible to solve for given through analytic methods and numerical methods are required.
Using Newton-Raphson, I arrive at , which trivially yields and :
Which is provably the optimal way to group the range from 1 to 32 using 3-deep ranges.