# `energy_distance` — n4m.stats.energy_distance _Namespace_: **`n4m.stats`** · _Fully-qualified_: `n4m.stats.energy_distance` · _Catalog id_: `diagnostics.energy_distance` ## API surface **C ABI (ABI 2):** [`n4m_stats_energy_distance`](https://github.com/GBeurier/nirs4all-methods/blob/main/cpp/include/n4m/metrics.h#L746). Use the linked public header for the exact signature, configuration, and result handles. **R:** no current source-verified entry point was found for this catalog method. **MATLAB / Octave:** no current source-verified entry point was found for this catalog method. ## Explanations ### Bibliographic source Székely & Rizzo (2004), *Testing for equal distributions in high dimension*, InterStat 5; Rizzo & Székely (2016), *Energy distance*, WIREs Computational Statistics 8(1), 27–38, https://doi.org/10.1002/wics.1375; Székely & Rizzo (2013), *Energy statistics: A class of statistics based on distances*, Journal of Statistical Planning and Inference 143(8), 1249–1272, https://doi.org/10.1016/j.jspi.2013.03.018. ### Mathematical principle With $M_{ab}$ the mean Euclidean distance over all ordered row pairs of groups $a$ and $b$ (a V-statistic, so within a group the zero self-pairs count), the energy distance between the empirical distributions is $E_{ab}=2M_{ab}-M_{aa}-M_{bb}\ge 0$. The K-sample statistic is $T=\sum_{a= T - sqrt(eps)|T|, numerically undecidable draws counted separately with a conservative p-value). Supported domain: finite values, nonzero |x| in [2^-256, 2^256], spread <= 2^32 (refused otherwise). Reference gate: parity/generators/domain_shift_v1.py -> parity/fixtures/domain_shift_v1.json (scipy cdist, exact rational energies and permutation statistics), cpp/tests/test_domain_shift.cpp. _See also_: [methods index](index.md).