Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 59. #60

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading