# A ranked list can balance the 24 orders of four entries to within 25% of the best possible error

One way to test how uniform a ranked list looks is to pick four entries, read them from left to right and record their relative order. In a random list, each of the 24 possible orders is equally likely. How close can a single list come to equal counts for all 24? For a list of length $n$, even the smallest possible error in the worst count grows as $n^3$. The question is how large the coefficient of $n^3$ has to be.

We construct lists of every sufficiently large length whose worst count differs from its target by $(1/576+o(1))n^3$, where $o(1)$ tends to zero as the length grows. This cuts the leading error of a published grid construction sixteenfold. Counting increasing and decreasing pairs also gives a lower bound: every design has error at least $(1/720-o(1))n^3$. The two coefficients differ by a factor of $5/4$.

## Measuring the error

A permutation is a list of the ranks $1$ through $n$, each used once. Four entries need not be consecutive: wherever $8,2,9,5$ appear in that order, they contribute one occurrence of the pattern $3142$, because $8$ is third smallest, $2$ is smallest, $9$ is largest and $5$ is second smallest. Write $C_p$ for the count of pattern $p$. There are $\binom n4$ ways to choose four entries, so the target for each pattern is $M_n=\binom n4/24$, its expected count in a uniform random permutation. The largest absolute error in one permutation is $D=\max_p|C_p-M_n|$. Its minimum over all permutations of length $n$ is $D_n$.

[Beniamini, Lavee and Linial, *How Balanced Can Permutations Be?*](https://doi.org/10.1007/s00493-024-00127-x) proved in Theorem 7 that $D_n=\Omega(n^3)$: no sequence of permutations can have $D_n/n^3\to0$. Their proof left the lower coefficient unspecified. We obtain $1/720$ by subtracting the mean in their pair-count identity and keeping its coefficients. For an upper bound, they superposed two oppositely rotated square grids. Their error is $2m^6/9+O(m^5)$ at length $n=2m^2$, giving coefficient $1/36$. [Schein, *Microscopic Probabilities for U-Statistics and Almost-Balanced Permutations*](https://arxiv.org/abs/2611.25820) estimates how many permutations meet a cubic error bound when its coefficient is sufficiently large. For the smallest attainable error, our bounds are
\[
 \frac1{720}\le \liminf_{n\to\infty}\frac{D_n}{n^3}
 \le \limsup_{n\to\infty}\frac{D_n}{n^3}\le\frac1{576}.
\]
These bounds apply whether or not the ratio $D_n/n^3$ converges.

## A constraint shared by all permutations

A list that nearly balances its four-point orders must also nearly balance its increasing and decreasing pairs. Beniamini, Lavee and Linial squared the number of increasing pairs. We subtract the number of decreasing pairs before squaring. This difference is twice the deviation of the increasing-pair count from its random-list mean. Its square ties the errors of all 24 patterns together.

For the lower bound it suffices to consider lists whose error is at most a constant times $n^3$, written $D=O(n^3)$. For a pattern $t$ of length $k=2$ or $3$, we can recover its count $C_t$ from the four-entry counts. Weight each $C_p$ by the number of occurrences of $t$ in $p$ and sum. Each copy of $t$ in the list is counted $\binom{n-k}{4-k}$ times. Subtract the corresponding identity in expectation for a uniform permutation and divide by this multiplicity: $C_t=\binom nk/k!+O(n^{k-1})$.

Give each position pair $i<j$ sign $e_{ij}=+1$ if it is increasing and $-1$ otherwise. Their sum $S$ is the excess of increasing pairs over decreasing pairs. The estimate for $k=2$ gives $S=O(n)$. In the expansion of $S^2$, terms use two, three or four distinct positions. A pair multiplied by itself contributes one. For two different pairs sharing a position, group their products by the triple of positions. The three products on a triple sum to $3$ if its entries rise throughout or fall throughout (a monotone triple) and to $-1$ otherwise, averaging $1/3$ over the six triple orders. For pairs with no common position, write $h(p)$ for the sum of the products on the three ways to split a four-point order $p$ into pairs. Accounting for both orders of multiplication gives
\[
 S^2=\binom n2+2\left(\tfrac13\binom n3+O(n^2)\right)+2\sum_p h(p)C_p .
\]
The left side is only $O(n^2)$, so the disjoint pairs must cancel the cubic term from the triples. Disjoint signs have zero mean product in a random order. Thus $\sum_p h(p)(C_p-M_n)=-n^3/18+O(n^2)$. Of the 24 values of $h$, eight have magnitude three and sixteen have magnitude one, by enumerating the three pairings. The weighted sum has magnitude at most $40D$, giving the lower coefficient $1/720$.

## Correcting the bias of a finite grid

To bring all 24 counts close to their targets, we first identify which orders a grid favors. Plot a permutation as points in a square: the horizontal coordinate gives list order and the vertical one gives rank. Conversely, any points without coordinate ties give a permutation this way. Four independent points uniform in the square give every order probability $1/24$. Placing points in separate cells changes the chance that two points will be neighbors in both coordinates. This favors some orders over others.

We tile the square with rectangles of side lengths comparable to $n^{-1/2}$. In each rectangle put a uniform point and its reflection through the center; at odd lengths one rectangle of half the usual area gets just one uniform point. We call the two reflected points twins. Draws in different rectangles are independent. Finally, shift points vertically by a smooth transformation that carries uniform area to a chosen probability density $\rho$ close to the uniform density of one. Reflections keep random changes in a cell from moving its two points in the same direction. The resulting count fluctuations are small compared with $n^3$, as the variance calculation below shows.

The systematic error has a simple cause: two of four independent draws can fall in the same cell more often than two distinct points chosen from the list do. Among the four sampled points, the two in a common cell usually sit next to each other in both coordinate orders. Call consecutive entries of $p$ whose ranks rise or fall by one a *bond*; let $b(p)$ count these bonds. For instance, $1234$ has three, while $3142$ has none. Let $P_p(\rho)$ denote the pattern probability for four independent density draws. There are six possible pairs among four points. The sampling calculation below gives the resulting correction for each pattern:
\[
 \frac{C_p}{\binom n4}
 =P_p(\rho)+\frac6n\left(\frac1{24}-\frac{b(p)}{36}\right)+o_{\mathbb P}(1/n). \tag{1}
\]
Here $o_{\mathbb P}(1/n)$ means that, for any fixed $\eta>0$, the probability that the remainder exceeds $\eta/n$ in absolute value tends to zero. This holds simultaneously for all 24 patterns. To compare errors across lengths, take $n^3/576$ counts as one unit. At uniform density the signed error $(C_p-M_n)/(n^3/576)$ then approaches $6-4b(p)$. The increasing order has a deficit of six in these units; the bond-free orders have a surplus of six.

![Signed limiting errors for all 24 orders, before and after adjusting the density. Both monotone orders move from minus six to minus one; all adjusted errors lie between minus one and one.](results/profile.png)

*Figure 1. Adjusting the density reduces the largest absolute limiting error from six units to one. Each column follows one pattern in the reflected-pair construction. Open circles give the bias at uniform density; diamonds give the exact limiting errors after the density change. The shaded interval is one unit on either side of equal counts. These are coefficients for large lists, not bounds on individual finite draws.*

## A density that corrects all 24 orders

We adjust the density so that its effect on each pattern offsets the bias from sharing a cell. Put $d=n^{-1/2}$ and take $\rho=1+df+d^2g$. Here $f$ and $g$ are functions of position in the square. Choose $f$ so that the term proportional to $d$ in every pattern probability vanishes. Its quadratic effect then has size $d^2=1/n$, just right to offset the bias in (1). The smaller adjustment $g$ enters linearly at that same scale.

To find such an $f$, let $X_1<\cdots<X_4$ and $Y_1<\cdots<Y_4$ be two independent sorted uniform samples from $[0,1]$, each of size four. Pairing horizontal rank $i$ with vertical rank $p(i)$ and averaging over the sorted coordinates gives
\[
 24P_p(\rho)=\mathbb E\prod_{i=1}^4\rho(X_i,Y_{p(i)}).
\]
Each individual rank has a cubic density, proportional at rank $i$ to $x^{i-1}(1-x)^{4-i}$. We build the density from shifted Legendre polynomials. The polynomial $P_j$ has degree $j$ and satisfies $P_j(1)=1$; its product with any polynomial of lower degree integrates to zero on $[0,1]$. Thus a product $P_a(x)P_b(y)$ with either degree at least four makes no linear contribution at any rank. Two such products at different ranks can have a nonzero joint contribution.

A search over mixtures of Legendre products gave the following density. Writing $a\otimes b$ for $a(x)b(y)$, use
\[
 f=\sqrt{38115/956}\,[3(P_1\otimes P_4+P_4\otimes P_1)+14P_4\otimes P_4],\qquad
 g=\sum_{a,b=1}^3 G_{ab}P_a\otimes P_b ,
\]
\[
 G=\begin{pmatrix}
 8737/66920&-125/478&-27357/19120\\
 -521/1912&2775/956&532/239\\
 38443/19120&2275/956&37352/1195
 \end{pmatrix}.
\]
Every term integrates to zero along either axis, so each coordinate on its own stays uniform.

The limiting scaled error $R_p$ plotted in Figure 1 comes directly from multiplying out the four factors. With $f_i=f(X_i,Y_{p(i)})$ and similarly for $g_i$, equation (1) gives
\[
 R_p=6-4b(p)+\mathbb E\left[\sum_i g_i+\sum_{i<j} f_i f_j\right].
\]
Higher terms in the probability are $O(d^3)$. For each product of polynomial terms, independence of the two sorted samples separates the expectation into one integral per axis. For four degrees $j_1,\ldots,j_4$, put $j_i=0$ at unused ranks ($P_0=1$). Starting at $V_0=1$, integrate $V_i(x)=\int_0^x V_{i-1}(t)P_{j_i}(t)\,dt$ in increasing rank order. The ordered-sample expectation is $24V_4(1)$. Multiplying the two axis results and summing with the displayed coefficients gives rational answers: for example $R_{1234}=R_{4321}=-1$, $R_{1243}=4159/33460$, and $R_{3142}=-1$. Over all 24 the maximum absolute value is exactly one. Equation (1) now gives $D/n^3\to1/576$ in probability and hence the stated existence bound at every large length.

Since $|P_j|\le1$, the condition $20\sqrt{38115/(956n)}+\sum_{a,b}|G_{ab}|/n<1$ guarantees a positive density. It holds from $n=16{,}034$ onward.

## Counts in finite lists

Even when the density is positive, counts vary from draw to draw. In units of $n^3/576$, the finite-length corrections to the mean are $O(n^{-1/2})$ and the standard deviation bound below is $O(n^{-1/4})$. We counted rising and falling quadruples in finite draws by extending monotone triples one entry at a time. In 40 draws at $n=32{,}768$, the rising and falling orders had mean errors of $-0.78$ and $-1.10$, with standard deviations $0.52$ and $0.51$. In 16 draws at $n=524{,}288$, the means were $-0.92$ and $-1.06$, with standard deviations $0.25$ and $0.22$. Both orders have limiting error $-1$, yet even at half a million entries an individual draw can miss that value by a substantial fraction of a unit.

## Why this density adjustment stops at one unit

No choice of fixed smooth functions $f,g$ that integrate to zero along either axis can lower the limiting maximum error below one unit when reflected pairs are sampled with density $1+df+d^2g$. The extra constraint comes from sorting the patterns into two groups by parity. Let $s(p)$ be $+1$ for an even number of decreasing pairs and $-1$ for an odd number. The signed sum of $24P_p(\rho)$ over orders, weighted by $s(p)$, is the expected determinant of the matrix with entries $\rho(X_i,Y_j)$. Each column is the all-ones column plus a correction of order $d$. Two all-ones columns make a determinant zero, so every surviving term uses at least three corrections. The parity sum is therefore $O(d^3)$. The finite-grid bias also has zero parity sum, by summing $s(p)(6-4b(p))$ over the 24 orders.

Consequently any such construction with bounded scaled errors has $\sum_p s(p)R_p=0$ in the limit. The universal pair constraint gives $\sum_p h(p)R_p=-32$ in the same units. On the sixteen orders with $|h|=1$, $h+s=0$; on the other eight, $|h+s|=4$. Adding the two constraints therefore gives $32\max_p|R_p|\ge32$, so at least one pattern must reach the edge of the band in Figure 1.

This one-unit lower bound applies to the stated sampling family. Larger density changes could behave differently. If $\varepsilon=\|\rho-1\|_\infty$ is the largest change in density anywhere in the square, the determinant bound is $O(\varepsilon^3)$; once $\varepsilon$ reaches order $n^{-1/3}$ it no longer forces parity to vanish on the $1/n$ scale. For unrestricted permutations, closing the gap between $1/720$ and $1/576$ remains open.

## Proof of the sampling formula

### Placing the points

A cell’s capacity is the number of points it holds. Assign capacity two to each cell except for the possible singleton. Distribute the cells by number as equally as possible among $\lfloor\sqrt{n/2}\rfloor$ horizontal rows. Give each row height equal to its total capacity divided by $n$, then divide its width in proportion to cell capacities. Each cell of capacity $s$ has area $s/n$ and both dimensions comparable to $d$. Transport the reflected draws by $T(x,v)=(x,y)$, where $\int_0^y\rho(x,t)\,dt=v$. This produces the desired density from a uniform point in the square. By implicit differentiation, $T$ differs from the identity by $O(d)$ in position and in its first two derivatives. Coordinate ties have probability zero; in particular, for fixed horizontal position the two transported twins move in opposite vertical directions as $v$ varies.

### Expected counts

Give each point a distinct label, so a cell of capacity two contains two labels. Compare four uniformly chosen distinct site labels in the construction with four independent density draws. For the latter, choose labels with replacement and draw a fresh independent position in each selected cell before transport. Conditional on four different cells the laws agree exactly: in both cases the weight of the cell sequence is proportional to the product of capacities. Call it a collision when two selected labels belong to the same cell. Because a cell holds at most two labels, the probability that two or more pairs collide is $O(n^{-2})$ in either model. For each specified pair the chance of a single coincidence is $2/n+O(n^{-2})$ with replacement and $1/n+O(n^{-2})$ for distinct labels. These follow by summing $s^2$ or $s(s-1)$ over capacities $s$ and dividing by $n^2$ or $n(n-1)$, respectively.

Given exactly one pair in a shared cell, the common cell center and the two other points approach three independent uniform square points. Before further coincidences are excluded, the common cell has weights proportional to $s^2$ or $s(s-1)$, so the probability of any set of cells differs from its area by at most $O(1/n)$ because at most one cell has capacity other than two. Given the collision, the chance of any further collision is $O(1/n)$.

To see how accurately the three points approximate a uniform triple, use area weights before excluding further collisions. Take a fresh uniform point in the common cell as its reference position, independent of whether the pair rises or falls before transport. For the two outside points use their positions before transport. These three reference points are independent and uniform in the square. Each coordinate moves by at most $O(d)$ under transport or replacement by a site in the common cell. The probability that any two of the three uniform points are this close on either axis is $O(d)$. Outside this event the triple order is unchanged and neither outside point splits the doubled entry. Before transport the doubled pair has fair orientation in every cell, for independent positions and for twins alike. Transport can change their vertical order only if their original vertical difference is $O(d^2)$, since the derivative of the displacement from the original position is $O(d)$ across a cell of diameter $O(d)$. This difference has density bounded by $O(1/d)$ in either model, so the chance of a flip is $O(d)$.

Thus when just one pair shares a cell, the limiting pattern can be generated from a uniform triple: choose one of its three entries uniformly and replace it with an adjacent pair, equally likely to rise or fall. There are $6\cdot3\cdot2=36$ equally likely outcomes before reading off the pattern. Exactly $b(p)$ give $p$, one per bond that could be contracted. With four distinct cells the pattern probability is $1/24+O(d)$, since conditioning away collisions costs $O(1/n)$ and $\rho=1+O(d)$ uniformly. Passing to distinct site labels transfers mass $6/n+O(n^{-2})$ from single collisions to four different cells. This gives (1) in expectation with remainder $O(d/n)$, hence an $O(d)$ correction in scaled count units.

As an independent check at uniform density, we grouped quadruples on an $m$-by-$m$ twin grid by their $i$ occupied rows and $j$ occupied columns. Each arrangement occurs $\binom mi\binom mj$ times. Within each row or column we enumerated which side of their centers the points fell on and the relative ranks of their distances from those centers. Either sign is equally likely, and twins have opposite signed distances. After subtracting the target count, the expected counts are exact polynomials in $m$ of degree at most six. For every order the coefficient of $m^6$ is $8(6-4b(p))/576$, as predicted at $n=2m^2$.

### Variation between draws

We bound the variance in two parts: variation explained by individual cells and variation due to interactions between cells. Reflection controls the first part; separation between cells controls the second. For a fixed pattern put $F=C_p$. First condition on the random draw $U_A$ in a single cell $A$ of capacity $s_A$. Terms not using $A$ have constant conditional mean, and quadruples using both its points contribute $O(n^2)$. For each specified site in $A$ at position $u$ before transport, the conditional sum over triples outside $A$ is
\[
 \binom{n-s_A}{3}H(Tu)+O(n^2),
\]
where $H(z)$ is the probability that $z$ and three independent density draws form $p$. The estimate holds uniformly: independent labels touch $A$, repeat, or share a cell with probability $O(1/n)$, and away from these events give the same conditional law as distinct sites outside $A$ avoiding shared cells.

The first two derivatives of $H\circ T$, the pattern probability as a function of position before transport, are bounded uniformly in $n$. For each possible rank of $z$ on each axis, split the ordered coordinate integrals at $z_x,z_y$. Rescale each resulting interval to $[0,1]$. This leaves a fixed integration domain; the Jacobian is a product of powers of interval lengths. Two derivatives under the integral are uniformly bounded because the density and its first two derivatives are uniformly bounded. This also proves the estimate for the smooth adjustments in the parity argument. Adding evaluations at $u$ and its reflection cancels the linear terms in the Taylor expansion at the cell center. Thus the centered conditional mean $\mathbb E[F\mid U_A]-\mathbb EF$ is $O(n^3d^2+n^2)=O(n^2)$ for twins and $O(n^3d+n^2)$ for the possible singleton. Their variances sum to $O(n^5)$.

To bound the remaining variance, replace two cell draws $A,B$ by independent copies. Take the mixed difference: the original count minus each singly replaced count plus the doubly replaced count. The sum of the expected squared mixed differences over unordered cell pairs bounds the remaining variance. To see why, take the mean of $F$ conditional on each subset of cell draws. Subtract the contributions of all its proper subsets, starting with the overall mean. This inclusion-exclusion decomposes $F$ into components, one for each subset. Independence makes distinct components orthogonal (their expected product is zero), and averaging a component over any variable it uses gives zero. The mixed difference cancels components missing either cell and multiplies the expected square of every remaining component by four, preserving orthogonality. Every component using at least two cells is covered.

Call two cells near if their ranges of possible positions after transport overlap horizontally or vertically; otherwise call them far. Each cell is near $O(\sqrt n)$ others: their preimages fit in two strips of width $O(d)$ and have disjoint interiors of area at least $1/n$ each. Only quadruples using both resampled cells can contribute to the mixed difference, so its size is always $O(n^2)$. For a far pair, quadruples with a repeated cell contribute only $O(n)$, since at most one more cell is free. In a quadruple with four distinct cells, changing the draw in $A$ cannot change any comparison unless one of the other two cells is near $A$. This leaves $O(\sqrt n\,n)$ possible quadruples. Each contributes at most a constant. Summing over near and far pairs gives a variance bound of $O(n^{3/2}n^4+n^2n^3)=O(n^{11/2})$ beyond the one-cell terms. The total variance is therefore $o(n^6)$. By Chebyshev's inequality, the chance of a deviation from the mean larger than any fixed fraction of $n^3$ tends to zero. With only 24 orders, this holds for all of them simultaneously: their centered fluctuations are $o_{\mathbb P}(n^3)$, as required for (1).
