Skip to main content

Python bindings for the dist_corr Rust library

Project description

dist-corr Python bindings

Python bindings for the Rust dist_corr library, providing fast computation of distance correlation, distance covariance, and distance variance between pairs of numeric vectors in R^n, with optimized implementations for binary (0/1) data.

Mathematical definition

For full mathematical definitions and derivations, see the main project README.

Installation

Install from PyPI:

pip install dist-corr

Input contract

  • Inputs must be numpy.ndarray.
  • Inputs must be one-dimensional, contiguous, and float64.
  • No list/tuple/pandas conversion is performed in the bindings.
  • NaN and inf values are passed directly to the Rust core.
  • If the binary flag is set to True, the input must contain only 0.0 and 1.0 values.

Quickstart

Basic usage examples (see API reference below for details).

Non-binary data

import numpy as np
import dist_corr

# Distance correlation
v1 = np.array([1.0, 2.0, 3.0], dtype=np.float64)
v2 = np.array([2.0, 4.0, 6.0], dtype=np.float64)

corr = dist_corr.distance_correlation(v1, v2)
print(f"Distance correlation: {corr}")

# Distance covariance
cov = dist_corr.distance_covariance(v1, v2)
print(f"Distance covariance: {cov}")

Binary data

import numpy as np
import dist_corr

v_bin_1 = np.array([0.0, 1.0, 0.0, 1.0], dtype=np.float64)
v_bin_2 = np.array([0.0, 0.0, 1.0, 1.0], dtype=np.float64)
v_real = np.array([0.5, 2.0, 1.0, -0.3], dtype=np.float64)

# v1 binary, v2 non-binary
corr = dist_corr.distance_correlation(v_bin_1, v_real, True, False)
print(f"Distance correlation (binary/non-binary): {corr}")

# v1 and v2 both binary
corr_both_bin = dist_corr.distance_correlation(v_bin_1, v_bin_2, True, True)
print(f"Distance correlation (both binary): {corr_both_bin}")

# v1 non-binary, v2 binary
cov_semi_bin = dist_corr.distance_covariance(v_real, v_bin_1, False, True)
print(f"Distance covariance (non-binary/binary): {cov_semi_bin}")

# v1 and v2 both binary
cov_both_bin = dist_corr.distance_covariance(v_bin_1, v_bin_2, True, True)
print(f"Distance covariance (both binary): {cov_both_bin}")

Distance variance

import numpy as np
import dist_corr

v = np.array([1.0, 0.0, 1.0], dtype=np.float64)
var = dist_corr.distance_variance(v)
print(f"Distance variance: {var}")

Performance and speed benchmarks

See the main project README for detailed benchmarks comparing this package to other Python and Rust implementations.

Error handling

All public compute functions return errors for common conditions:

  • Vectors have different lengths
  • One or both vectors are empty
  • A vector is declared binary (flag set) but contains other values

Check the returned error and propagate or handle as needed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dist_corr-0.1.1.dev1.tar.gz (7.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dist_corr-0.1.1.dev1-cp313-cp313-macosx_11_0_arm64.whl (3.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

File details

Details for the file dist_corr-0.1.1.dev1.tar.gz.

File metadata

  • Download URL: dist_corr-0.1.1.dev1.tar.gz
  • Upload date:
  • Size: 7.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for dist_corr-0.1.1.dev1.tar.gz
Algorithm Hash digest
SHA256 534ddb7b44deb350fc7a4ec574b3c13c59b60d6d4479de30a4c13214747136ea
MD5 4186259c5c5e05e943536d8724c48b9c
BLAKE2b-256 8b13689c5d308094838d5657ded1554f2094e0bbb346d0ae60653ad13124981c

See more details on using hashes here.

File details

Details for the file dist_corr-0.1.1.dev1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dist_corr-0.1.1.dev1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 363a12922ce445be83a8a9844351d52c093cad07c827b6a4df5bba1eb23a21c5
MD5 709ece2f74bcb0dee25c14410f627ed6
BLAKE2b-256 e971c0ea96aed6e5410617ad23b9f0cf5d1f0dbdd261c966e574920473174639

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page