dcor: Distance Correlation and Energy Statistics

0.7 · active · verified Fri Apr 17

dcor is a Python library that provides efficient implementations of distance correlation and energy statistics, powerful tools for measuring dependence and performing two-sample tests. It supports various statistical tests including independence testing and two-sample testing. Currently at version 0.7, it is actively maintained with regular updates and a focus on numerical stability and performance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to calculate distance correlation, energy distance, and perform an independence test using dcor. It initializes sample NumPy arrays and applies the main functions, printing their results.

import numpy as np
import dcor

# Example data
x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 2, 3, 4, 5])
z = np.array([5, 4, 3, 2, 1])
w = np.array([1, 2, 3, 4, 6]) # Slightly different for energy_distance

# Calculate distance correlation
dc_xy = dcor.distance_correlation(x, y)
dc_xz = dcor.distance_correlation(x, z)
print(f"Distance correlation (x, y): {dc_xy:.4f}")
print(f"Distance correlation (x, z): {dc_xz:.4f}")

# Calculate energy distance
ed_xw = dcor.energy_distance(x, w)
print(f"Energy distance (x, w): {ed_xw:.4f}")

# Perform independence test (requires bootstrapping)
# Note: n_bootstraps should be sufficiently large for real analysis
independence_p_value = dcor.independence_test(x, y, n_bootstraps=100).p_value
print(f"P-value for independence test (x, y): {independence_p_value:.4f}")

view raw JSON →