{"id":9647,"library":"dcor","title":"dcor: Distance Correlation and Energy Statistics","description":"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.","status":"active","version":"0.7","language":"en","source_language":"en","source_url":"https://github.com/vnmabus/dcor","tags":["statistics","distance correlation","energy statistics","non-parametric","hypothesis testing","dependence measure"],"install":[{"cmd":"pip install dcor","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for numerical operations and array handling.","package":"numpy","optional":false},{"reason":"Required for statistical functions and scientific computing.","package":"scipy","optional":false}],"imports":[{"note":"The function was renamed in a previous major version for clarity.","wrong":"from dcor import get_distance_correlation","symbol":"distance_correlation","correct":"from dcor import distance_correlation"},{"symbol":"energy_distance","correct":"from dcor import energy_distance"},{"symbol":"independence_test","correct":"from dcor import independence_test"}],"quickstart":{"code":"import numpy as np\nimport dcor\n\n# Example data\nx = np.array([1, 2, 3, 4, 5])\ny = np.array([1, 2, 3, 4, 5])\nz = np.array([5, 4, 3, 2, 1])\nw = np.array([1, 2, 3, 4, 6]) # Slightly different for energy_distance\n\n# Calculate distance correlation\ndc_xy = dcor.distance_correlation(x, y)\ndc_xz = dcor.distance_correlation(x, z)\nprint(f\"Distance correlation (x, y): {dc_xy:.4f}\")\nprint(f\"Distance correlation (x, z): {dc_xz:.4f}\")\n\n# Calculate energy distance\ned_xw = dcor.energy_distance(x, w)\nprint(f\"Energy distance (x, w): {ed_xw:.4f}\")\n\n# Perform independence test (requires bootstrapping)\n# Note: n_bootstraps should be sufficiently large for real analysis\nindependence_p_value = dcor.independence_test(x, y, n_bootstraps=100).p_value\nprint(f\"P-value for independence test (x, y): {independence_p_value:.4f}\")","lang":"python","description":"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."},"warnings":[{"fix":"Update your code to use `dcor.distance_correlation`.","message":"The function `dcor.get_distance_correlation` was renamed to `dcor.distance_correlation` in version 0.4.0. Using the old name will result in an AttributeError.","severity":"breaking","affected_versions":"<0.4.0"},{"fix":"Ensure your input data is converted to `numpy.ndarray` before passing it to dcor functions (e.g., `np.array(my_list)`).","message":"Input data for all dcor functions (e.g., `distance_correlation`, `energy_distance`) must be NumPy arrays or objects convertible to them. Passing raw Python lists will lead to TypeError.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always provide a sufficiently large `n_bootstraps` argument (e.g., 1000 or more for production) to `independence_test`.","message":"When performing statistical tests like `dcor.independence_test`, the `n_bootstraps` parameter is mandatory and determines the number of bootstrap samples used for p-value calculation. A too small value can lead to unreliable results.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Reshape or filter your data to ensure `x.shape[0] == y.shape[0]` before computing statistics like distance correlation or energy distance.","message":"The input arrays `x` and `y` must have the same number of observations (rows). If they represent samples, they must be from the same number of experimental units.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure that `x.shape[0]` is equal to `y.shape[0]`. If `x` and `y` are 1D arrays, they must have the same length.","cause":"The number of samples (rows) in the input arrays `x` and `y` do not match.","error":"ValueError: x and y must have the same number of observations."},{"fix":"Convert your lists to NumPy arrays using `np.array(my_list)` before passing them to dcor functions.","cause":"dcor functions expect `numpy.ndarray` objects, but raw Python lists were passed directly.","error":"TypeError: unsupported operand type(s) for -: 'list' and 'list'"},{"fix":"Update your code to use the current function name `dcor.distance_correlation`.","cause":"Attempting to use an old function name `get_distance_correlation` which was deprecated and removed.","error":"AttributeError: module 'dcor' has no attribute 'get_distance_correlation'"},{"fix":"Add the `n_bootstraps` argument with an appropriate integer value, e.g., `dcor.independence_test(x, y, n_bootstraps=1000)`.","cause":"The `independence_test` function requires the `n_bootstraps` argument to specify the number of bootstrap iterations.","error":"TypeError: independence_test() missing 1 required positional argument: 'n_bootstraps'"}]}