Skip to content

Commit

Permalink
Fix issue 59.
Browse files Browse the repository at this point in the history
Fixes an integer overflow by reordering the operands, so that the data
is first converted to a floating type.
  • Loading branch information
vnmabus committed Dec 7, 2023
1 parent 9fd24a0 commit c3fabd3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dcor/_dcor_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def _dcov_from_terms(
) -> Array:
"""Compute distance covariance WITHOUT centering first."""
first_term = mean_prod / n_samples
second_term = a_axis_sum @ b_axis_sum / n_samples
third_term = a_total_sum * b_total_sum / n_samples
second_term = a_axis_sum / n_samples @ b_axis_sum
third_term = a_total_sum / n_samples * b_total_sum

if bias_corrected:
first_term /= (n_samples - 3)
Expand Down
29 changes: 29 additions & 0 deletions dcor/tests/test_dcor.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,35 @@ def test_dcor_constant(self) -> None:
corr_af_inv = dcor.distance_correlation_af_inv(a, a)
self.assertAlmostEqual(corr_af_inv, 0)

def test_integer_overflow(self) -> None:
"""Tests int overflow behavior detected in issue #59."""
n_samples = 10000

# some simple data
arr1 = np.array([1, 2, 3] * n_samples)
arr2 = np.array([10, 20, 5] * n_samples)

int_int = dcor.distance_correlation(
arr1,
arr2,
)
float_int = dcor.distance_correlation(
arr1.astype(float),
arr2,
)
int_float = dcor.distance_correlation(
arr1,
arr2.astype(float),
)
float_float = dcor.distance_correlation(
arr1.astype(float),
arr2.astype(float),
)

self.assertAlmostEqual(int_int, float_float)
self.assertAlmostEqual(float_int, float_float)
self.assertAlmostEqual(int_float, float_float)


class TestDcorArrayAPI(unittest.TestCase):
"""Check that the energy distance works with the Array API standard."""
Expand Down

0 comments on commit c3fabd3

Please sign in to comment.