Sampling and counting contingency tables with arbitrary margins
Number grids with fixed row and column sums can be sampled fairly and counted fast, whatever their size.
A contingency table is a grid of whole numbers whose rows and columns must add up to given totals. The first paper proves a fast way to pick such a table at random, with every table exactly equally likely. Fast here means fast in the number of digits, even when the totals are enormous. The second paper proves a fast way to estimate how many tables exist, even when single cells have caps. Earlier methods needed few rows, large totals or small totals.
The explanation
Fill a grid of two rows and three columns with whole numbers, none negative. Each row must add up to 3. Each column must add up to 2. Such a grid is called a contingency table. The required sums are its margins. Exactly 7 tables fit these margins. One has 1, 1, 1 in both rows. Another has 0, 1, 2 on top and 2, 1, 0 below.
Two jobs come with such tables. Counting asks how many tables fit: here, 7. Uniform sampling asks for one table drawn at random, with every table equally likely. Here each would get a chance of 1/7. A third twist adds a cap on single cells, called a cell bound. A cap of 0 forbids a cell outright. Forbid the top-left cell above, and only 2 tables remain. For grids this small, a computer can simply list every table.
Now take a 3-by-3 grid with every margin equal to one million. Each margin takes only 7 digits to write. Yet 125,000,750,001,875,002,250,001 tables fit, about 1.25 × 10^23. Listing them is hopeless. Computer scientists call a method fast when its time grows like a fixed power of the input's length. Here the length counts rows, columns and the digits of the margins, not their size. The papers work in binary digits, but the idea is the same. A bound that grows with the margin total itself would not count as fast.
Earlier work covered special shapes. In 2000, Dyer and Greenhill counted and sampled approximately for tables with two rows. In 2003, Kijima and Matsui gave exactly uniform sampling for two rows. Cryan and Dyer, and Dyer alone, handled any fixed number of rows in 2003. Their time bounds depend on that fixed row count. Dyer, Kannan and Mount in 1997, and Morris in 2002, worked with large margins. Arman, Gao and Wormald in 2021 handled sparse margins under a size condition. Their survey reported that no fast, nearly uniform sampler for arbitrary margins was known.
Cell bounds had their own history. Jerrum, Sinclair and Vigoda in 2004 covered the case where every cap is 0 or 1. In 2010, Cryan, Dyer and Randall handled large caps, or a fixed number of rows. They named the unrestricted case as an open problem.
The one obstacle was mixing. Real margins can be huge in some rows and tiny in others. Huge values cannot be stepped through one unit at a time. Tiny values break the smooth geometry that methods for large margins rely on. The papers split the table along that line. Cells that touch a small margin are handled one at a timeSource 1. The rest form a block of large numbers, treated with geometric tools. In the sampling paper, that block is padded first so every part of it counts as largeSource 1. A draw is kept only when the padding can be removed cleanlySource 2.
The first paper proves two samplers for any margins with matching totals. You choose an accuracy level k. One sampler always finishes within a fixed power of the input length and k. Its output can be slightly unfair, by at most 2^-k in total variationSource 3. Total variation is the largest amount any group of tables can gain or lose in probability. The other sampler is exactly uniform. It stops with probability 1, and its average running time is fastSource 4. A rare branch does slow, exhaustive work, so single runs can take much longerSource 5Source 6.
The second paper proves an approximate counting scheme with cell bounds, zero caps included. Ask for an error of ε and a failure chance of δ. The estimate then lands within a factor 1 ± ε of the true countSource 7. That happens with probability at least 1 − δ. One fixed power bound limits the work on every run. If no table fits, it returns zero every timeSource 7. Such a scheme is called an FPRAS.
Back in the 3-by-3 grid, each of those 10^23 tables can now get an exactly equal chance. The average cost grows with the number of digits, not with one million. Still, the papers call their power bounds deliberately largeSource 5. These are guarantees about growth, not practical running timesSource 8. Exact counting stays hard: Dyer, Kannan and Mount proved it is #P-complete even for two rows. The exact sampler also does not cover cell boundsSource 9.
How sure is this? A computer proof checker called Lean has checked formal versions of the main statements. They are the exact sampler, the bounded-time sampler and the counting scheme with cell bounds. All three sit in one Comparator challenge file, a fixed reference statement for checking. The counting paper's consequences for network flows are not in the Lean scope note. No specialist review has been published.
What it doesn't show. This does not count tables exactly, and it does not give a practical running time.
Where it came from
This result comes from an unreleased OpenAI model, published in the openai/math release on 6 October 2026.
Hype check
Major if the proofs hold. The Lean scope note lists formal versions of the three main statements. No specialist review is published. The power bounds are deliberately large, so this is not a practical tool.
Words used
- Contingency table
- A grid of nonnegative whole numbers whose row and column sums are fixed in advance.
- Margins
- The required row sums and column sums of a contingency table.
- Cell bound
- A cap on one entry of the table; a cap of 0 forbids that entry.
- Uniform sampling
- Drawing one object at random so that every allowed object has the same chance.
- Total variation
- The largest amount by which any group of outcomes gains or loses probability compared with a target.
- Polynomial time
- Running time that grows at most like a fixed power of the length of the input.
- FPRAS
- A randomized method that estimates a count within any requested relative error, with high probability, in polynomial time in the input and one over the error.
- Lean
- A computer proof checker that confirms each step of a formally written proof.
The original papers (2)
All quoted lines (9)
-
We pad this rectangle so every conditional completion problem has large margins.
Exact Uniform Sampling of Contingency Tables with Arbitrary Margins Back to text -
Accept only a transversal whose large completion entries are all at least the padding.
Exact Uniform Sampling of Contingency Tables with Arbitrary Margins Back to text -
Given an integer , a bounded-time algorithm outputs a table in whose law is within in total variation of the uniform law.
Exact Uniform Sampling of Contingency Tables with Arbitrary Margins Back to text -
An algorithm outputs an exactly uniform table in , terminates almost surely, and has expected running time polynomial in .
Exact Uniform Sampling of Contingency Tables with Arbitrary Margins Back to text -
The algorithms below are explicit, although their exponents are deliberately large. Part (ii) concerns expected time: an exceptionally rare branch performs exhaustive computation.
Exact Uniform Sampling of Contingency Tables with Arbitrary Margins Back to text -
Thus the exact algorithm has polynomial expected bit cost, not a polynomial bound on every execution.
Exact Uniform Sampling of Contingency Tables with Arbitrary Margins Back to text -
If is empty, the output is zero on every execution.
An FPRAS for Cell-Bounded Contingency Tables Back to text -
The polynomial bounds in the proof are deliberately generous; the result is a uniform complexity guarantee rather than a practical running-time estimate.
An FPRAS for Cell-Bounded Contingency Tables Back to text -
Those stronger sampling guarantees concern the uncapped setting; the flow consequence here has the total-variation and cost bounds just stated.
An FPRAS for Cell-Bounded Contingency Tables Back to text
Other sources
- openai/math release notes (2026): model provenance and review status
manuscripts and supporting proof artifacts produced by an internal OpenAI model
- M. Dyer and C. Greenhill, “Polynomial-time counting and sampling of two-rowed contingency tables”, Theoretical Computer Science 246 (2000), 265–278
- S. Kijima and T. Matsui, “Polynomial Time Perfect Sampling Algorithm for Two-rowed Contingency Tables”, METR 2003-15, University of Tokyo (2003)
- M. Cryan and M. Dyer, “A polynomial-time algorithm to approximately count contingency tables when the number of rows is constant”, Journal of Computer and System Sciences 67 (2003), 291–310
- M. Dyer, “Approximate counting by dynamic programming”, STOC (2003), 693–699
- M. Dyer, R. Kannan and J. Mount, “Sampling contingency tables”, Random Structures & Algorithms 10 (1997), 487–506
- B. Morris, “Improved bounds for sampling contingency tables”, Random Structures & Algorithms 21 (2002), 135–146
- A. Arman, P. Gao and N. Wormald, “Linear-time uniform generation of random sparse contingency tables with specified marginals”, arXiv:2104.09413 (2021)
- M. Jerrum, A. Sinclair and E. Vigoda, “A polynomial-time approximation algorithm for the permanent of a matrix with nonnegative entries”, Journal of the ACM 51 (2004), 671–697
- M. Cryan, M. Dyer and D. Randall, “Approximately Counting Integral Flows and Cell-Bounded Contingency Tables”, SIAM Journal on Computing 39 (2010), 2683–2703