# Fixed overflow bug in 0.2 chi\_squared\_test / contingency\_table\_test, present since June 25, 2018

**URL:** <https://discuss.hail.is/t/fixed-overflow-bug-in-0-2-chi-squared-test-contingency-table-test-present-since-june-25-2018/619>\
**Category:** Updates\
**Created:** [August 22, 2018, 7:51pm UTC](https://discuss.hail.is/t/fixed-overflow-bug-in-0-2-chi-squared-test-contingency-table-test-present-since-june-25-2018/619 "2018-08-22T19:51:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbloom](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/jbloom/32/109_2.png) [@jbloom](https://discuss.hail.is/u/jbloom)\
**Post date:** [August 22, 2018, 7:51pm UTC](https://discuss.hail.is/t/fixed-overflow-bug-in-0-2-chi-squared-test-contingency-table-test-present-since-june-25-2018/619/1 "2018-08-22T19:51:29Z")

</div>

We just [discovered and fixed](https://github.com/hail-is/hail/issues/4192) an overflow bug in [chi\_squared\_test](https://hail.is/docs/devel/functions/stats.html#hail.expr.functions.chi_squared_test) that has existed in the 0.2 development branch since commit `01875754aa921fab22e1c4e8f11949e21fdc884c` merged on June 25, 2018.

If you used a version since that commit to run `chi_squared_test` with counts `a`, `b`, `c`, and `d` then:

- if `a * d` or `b * c` exceeded 2,147,483,647, the odds ratio is wrong. E.g. a minimal table with incorrect odds ratio is (46341, 1, 1, 46341).
- if `(a + b) * (c + d) * (b + d) * (a + c)` exceeded 2,147,483,647, the p-value is wrong. E.g., two minimal tables with incorrect p-value are (108, 108, 108, 107) and (216, 0, 0, 215).

Tables with total count below 431 were unaffected.

The bug also applied to [contingency\_table\_test](https://hail.is/docs/devel/functions/stats.html#hail.expr.functions.contingency_table_test) when the total cell count was at least `min_cell_count`, since in this case the function calls `chi_squared_test`.

The `fisher_exact_test` is unrelated and unaffected.

Here is the problematic Scala implementation:

```auto
    val ad = a * d
    val bc = (b * c).toDouble
    val oddsRatio = ad / bc
    val det = ad - bc
    val chiSquare = (det * det * (a + b + c + d)) / ((a + b) * (c + d) * (b + d) * (a + c))
    val pValue = chiSquaredTail(chiSquare, 1)

```
