Maximum value of Chi squared while keeping marginal frequencies Let's say we have a RxC contingency table, with R=C. How could one best repopulate the table such that:


*

*all counts reside in the off-diagonal cells (zero's on the diagonal);

*marginal frequencies remain the same;

*the value of Chi square is maximal.


I can imagine a very 'exhaustive' approach by calculating the Chi square for each possible repopulation and taking the table that produces the highest Chi square value, but there has to be an elegant much better way to solve this! A formal proof may be difficult, but a smart algorithm should exist.
 A: This is a quadratic programming problem.  To see this, first rearrange the $R \times C$ matrix of expected values $E_{ij}$ and counts $X_{ij}$ into vectors, which we will label $\bf{E}$ and $\bf{X}$, and which have length $N = R \times C$ (although you could just remove the diagonal elements from both vectors before continuing, since they are constrained to equal zero.)  Then maximizing the $\chi^2$ statistic is the same as:
$\max_{\bf{X}} \sum_{i=1}^N \frac{(X_i-E_i)^2}{E_i} = \sum_{i=1}^NX_i^2/E_i -2 \sum_{i=1}^NX_i$
which pretty obviously can be rewritten in the canonical quadratic programming objective function form $\frac{1}{2}x^TQx +c^Tx$, $Q$ a diagonal matrix with $Q_{ii} = 2/E_i$ and $c_i = -2, i=1, \dots, N$.
The constraints on the marginal totals are just equality constraints on the sums of various of the elements of $\bf{X}$, with of course equality constraints for the constrained-to-equal-zero elements of $\bf{X}$, and fit into the standard QP formulation $Dx = d$ in the obvious manner.
