Enumerating r x c contingency tables with fixed grand total

One way: recursion over the flattened length of the table, i.e., to produce sequences of length rc.

  • Base case: e(1,n), the contingency sequence with 1 element, contains just the grand total, n.
  • Step case: to compute e(l,n) for l > 1, you need
    • 0 conjoined with each sequence produced by e(l-1,n), i.e., all tables one size smaller with the originally desired grand total
    • 1 conjoined with each e(l-1,n-1)
    • 2 conjoined with each e(l-1,n-2)
    • n-1 conjoined with each e(l-1,1)
    • n conjoined with each e(l-1,0)

The number of 2×2 contingency tables seems to be given by the triangular pyramidal numbers, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969, … which is a sum of triangular numbers.

There’s a bunch of work on the geometry of contingency tables.Β  Fun stuff!Β  (Ended up playing with this to generate stimuli for an experiment.)