Skip to content

Commit

Permalink
Merge pull request #60 from vnmabus/fix_issue_59
Browse files Browse the repository at this point in the history
Fix issue 59.
  • Loading branch information
vnmabus authored Jan 26, 2024
2 parents 9fd24a0 + bacb6d0 commit 555dbc9
Show file tree
Hide file tree
Showing 2 changed files with 34 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
32 changes: 32 additions & 0 deletions dcor/tests/test_dcor.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,38 @@ 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, dtype=np.int64)
arr2 = np.array([10, 20, 5] * n_samples, dtype=np.int64)

int_int = dcor.distance_correlation(
arr1,
arr2,
compile_mode=dcor.CompileMode.NO_COMPILE,
)
float_int = dcor.distance_correlation(
arr1.astype(float),
arr2,
compile_mode=dcor.CompileMode.NO_COMPILE,
)
int_float = dcor.distance_correlation(
arr1,
arr2.astype(float),
compile_mode=dcor.CompileMode.NO_COMPILE,
)
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 555dbc9

Please sign in to comment.