I want to compare two contingency tables A and B with the same structure (same columns and rows). My first idea was to use chi squared distance $\sum_{i=1}^{p}{\frac{(O_i-E_i)^2}{E_i}}$. But, some cells $E_i$ are equal to zero.

Have you some ideas to deal with that problem or another distance metrics without this issue ?

More precisely, let's consider the contingency table A:

Atoms Carbone Hydrogen Nitrogen
Carbone 2339 2582 305
Hydrogen 2582 0 144
Nitrogen 305 144 29

Each cell $A_{ij}$ of table A shows how many times the two atoms (corresponding to the row i and the column j) are linked together to form a molecule in our data (in molecule table of the Biodegradability database).

I have another list of molecules who is represented by the contingency table B: Given another contingency table B:

Atoms Carbone Hydrogen Nitrogen
Carbone 1293 2664 107
Hydrogen 2664 1310 114
Nitrogen 107 114 0

My objective is to measure how close the table B is from table A by computing the chi-squared distance. As we can see, if we directly compute this distance, we get infinite value because of the null value in the cell B(Nitrogen, Nitrogen) -- Nitrogen atoms are never linked together in table B.

Do you have any idea on how to deal with this issue or have any other distance metric (without this issue) to propose?

  • 3
    $\begingroup$ Welcome to CV, Mohamed. Please explain what aspect(s) of these tables you want to compare, because there are many possible ways to do so, depending on your objectives. Providing a context for your question can help clarify it. $\endgroup$
    – whuber
    Aug 3, 2023 at 12:42
  • $\begingroup$ Also, $E_i$ is the expected value (under independence) so will be zero only if a complete row or column is zero. Did you mean $O_i$ is zero? If so, please edit. Also, maybe stats.stackexchange.com/questions/622187/… can help? depending on what you really are asking $\endgroup$ Aug 5, 2023 at 20:40
  • $\begingroup$ Hello, thank you for your help. we can assume that a contingency table is the sample of an unknown discrete discrete distribution. I want to evaluate the distance between the distributions behind these two contingency tables. $\endgroup$ Aug 14, 2023 at 13:21
  • $\begingroup$ Chi 2 can be no appropriate. I thought to something like jensen distance but it don't accept discrete values. $\endgroup$ Aug 14, 2023 at 13:24
  • $\begingroup$ These are not contingency tables! They are tabulations of counts of edges in graphs (the molecular bonds). In effect, you have one table with two rows (A and B) and six columns (the six possible unordered pairs of three elements). BTW, it's impossible for most stable molecules to contain any permanent H-H bonds (apart from hydrogen gas $H_2,$) so where does the 1310 count come from in the second panel? $\endgroup$
    – whuber
    Aug 22, 2023 at 16:45

1 Answer 1


You are tabulating edges. Your tables are, more or less, sums of adjacency matrices.

The data can be expressed (without redundancy) as a $2\times 6$ table explicitly indicating the possible edges:

Bond:    CC   CH   CN   HH   HN   NN   Total
-----  ---- ---- ---- ---- ---- ----   -----
    A  2339 2582  305    0  144   29 |  5399
    B  1293 2664  107 1310  114    0 |  5488
Total  3632 5246  412 1310  258   20 | 10887

(Never mind that this is not chemically possible.)

Any column with a total of 0 (which is what I would expect a realistic HH column to be) can be dropped. There is no problem with any remaining cells with zeros, because they will have non-zero expected values. (Some care is needed in computing a p-value when many expected values are small, but that's a separate issue and it does not apply in this example where the smallest expected value is 14.)

As usual, the "expected value" $E_{rc}$ in row $r$ and column $c$ is the product of the row total and column total divided by the grand total and the chi-squared statistic is the sum given in the question. Here, its value is $1739.6.$ You can use that, if you wish, as a measure of how "close" the datasets $A$ and $B$ are.

The usual chi-squared p-value is likely meaningless. It is computed by assuming the cell counts are independent. That's not the case with molecules. Consider, for instance, ethane, $C_2H_6.$ Because there is one CC bond, there must be six CH bonds: that's a lack of independence. (If you are counting double bonds and triple bonds as if they were a single link, then the presence of a single CC link in a hydrocarbon implies there are 2, 4, or 6 CH links -- but that still exhibits non-independence.)

It would be presumptuous to propose a metric or alternative method of comparing the data without more information about how the comparison is intended to be used.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.