mljs / distance

Distance functions to compare vectors
https://mljs.github.io/distance/
MIT License
67 stars 4 forks source link

ml-distance

Zakodium logo

Maintained by Zakodium

[![NPM version][npm-image]][npm-url] [![Node.js CI](https://github.com/mljs/distance/actions/workflows/nodejs.yml/badge.svg)](https://github.com/mljs/distance/actions/workflows/nodejs.yml) [![Test coverage][codecov-image]][codecov-url] [![npm download][download-image]][download-url]

Distance functions to compare vectors.

Installation

$ npm i ml-distance

Methods

Distances

Returns the euclidean distance between vectors p and q

$d(p,q)=\sqrt{\sum\limits_{i=1}^{n}(p_i-q_i)^2}$

Returns the city block distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}$

Returns the Minkowski distance between vectors p and q for order d

Returns the Chebyshev distance between vectors p and q

$d(p,q)=\max\limits_i(|p_i-q_i|)$

Returns the Sørensen distance between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-qi\right|}}{\sum\limits{i=1}^{n}{p_i+q_i}}$

Returns the Gower distance between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{n}$

Returns the Soergel distance between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{max(p_i,q_i)}$

Returns the Kulczynski distance between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{min(p_i,q_i)}$

Returns the Canberra distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}\frac{\left|{p_i-q_i}\right|}{p_i+q_i}$

Returns the Lorentzian distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}\ln(\left|{p_i-q_i}\right|+1)$

Returns the Intersection distance between vectors p and q

$d(p,q)=1-\sum\limits_{i=1}^{n}min(p_i,q_i)$

Returns the Wave Hedges distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}\left(1-\frac{min(p_i,q_i)}{max(p_i,q_i)}\right)$

Returns the Czekanowski distance between vectors p and q

$d(p,q)=1-\frac{2\sum\limits_{i=1}^{n}{min(p_i,qi)}}{\sum\limits{i=1}^{n}{p_i+q_i}}$

Returns the Motyka distance between vectors p and q

$d(p,q)=1-\frac{\sum\limits_{i=1}^{n}{min(p_i,qi)}}{\sum\limits{i=1}^{n}{p_i+q_i}}$

Note: distance between 2 identical vectors is 0.5 !

Returns the Ruzicka similarity between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{max(p_i,qi)}}{\sum\limits{i=1}^{n}{min(p_i,q_i)}}$

Returns the Tanimoto distance between vectors p and q, and accepts the bitVector use, see the test case for an example

Returns the Inner Product similarity between vectors p and q

$s(p,q)=\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}$

Returns the Harmonic mean similarity between vectors p and q

$d(p,q)=2\sum\limits_{i=1}^{n}\frac{p_i\cdot{q_i}}{p_i+q_i}$

Returns the Cosine similarity between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{p_i\cdot{qi}}}{\sum\limits{i=1}^{n}{pi^2}\sum\limits{i=1}^{n}{q_i^2}}$

Returns the Kumar-Hassebrook similarity between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{p_i\cdot{qi}}}{\sum\limits{i=1}^{n}{pi^2}+\sum\limits{i=1}^{n}{qi^2}-\sum\limits{i=1}^{n}{p_i\cdot{q_i}}}$

Returns the Jaccard distance between vectors p and q

$d(p,q)=1-\frac{\sum\limits_{i=1}^{n}{p_i\cdot{qi}}}{\sum\limits{i=1}^{n}{pi^2}+\sum\limits{i=1}^{n}{qi^2}-\sum\limits{i=1}^{n}{p_i\cdot{q_i}}}$

Returns the Dice distance between vectors p and q

$d(p,q)=1-\frac{\sum\limits_{i=1}^{n}{(p_i-qi)^2}}{\sum\limits{i=1}^{n}{pi^2}+\sum\limits{i=1}^{n}{q_i^2}}$

Returns the Fidelity similarity between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\sqrt{p_i\cdot{q_i}}}$

Returns the Bhattacharyya distance between vectors p and q

$d(p,q)=-\ln\left(\sum\limits_{i=1}^{n}{\sqrt{p_i\cdot{q_i}}}\right)$

Returns the Hellinger distance between vectors p and q

$d(p,q)=2\cdot\sqrt{1-\sum\limits_{i=1}^{n}{\sqrt{p_i\cdot{q_i}}}}$

Returns the Matusita distance between vectors p and q

$d(p,q)=\sqrt{2-2\cdot\sum\limits_{i=1}^{n}{\sqrt{p_i\cdot{q_i}}}}$

Returns the Squared-chord distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{(\sqrt{p_i}-\sqrt{q_i})^2}$

Returns the squared euclidean distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{(p_i-q_i)^2}$

Returns the Pearson distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\frac{(p_i-q_i)^2}{q_i}}$

Returns the Neyman distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\frac{(p_i-q_i)^2}{p_i}}$

Returns the Squared distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\frac{(p_i-q_i)^2}{p_i+q_i}}$

Returns the Probabilistic Symmetric distance between vectors p and q

$d(p,q)=2\cdot\sum\limits_{i=1}^{n}{\frac{(p_i-q_i)^2}{p_i+q_i}}$

Returns the Divergence distance between vectors p and q

$d(p,q)=2\cdot\sum\limits_{i=1}^{n}{\frac{(p_i-q_i)^2}{(p_i+q_i)^2}}$

Returns the Clark distance between vectors p and q

$d(p,q)=\sqrt{\sum\limits_{i=1}^{n}{\left(\frac{\left|p_i-q_i\right|}{(p_i+q_i)}\right)^2}}$

Returns the Additive Symmetric distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\frac{(p_i-q_i)^2\cdot(p_i+q_i)}{p_i\cdot{q_i}}}$

Returns the Kullback-Leibler distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{p_i\cdot\ln\frac{p_i}{q_i}}$

Returns the Jeffreys distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\left((p_i-q_i)\ln\frac{p_i}{q_i}\right)}$

Returns the K divergence distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\left(p_i\cdot\ln\frac{2p_i}{p_i+q_i}\right)}$

Returns the Topsøe distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\left(p_i\cdot\ln\frac{2p_i}{p_i+q_i}+q_i\cdot\ln\frac{2q_i}{p_i+q_i}\right)}$

Returns the Jensen-Shannon distance between vectors p and q

$d(p,q)=\frac{1}{2}\left[\sum\limits_{i=1}^{n}{p_i\cdot\ln\frac{2p_i}{p_i+qi}}+\sum\limits{i=1}^{n}{q_i\cdot\ln\frac{2q_i}{p_i+q_i}}\right]$

Returns the Jensen difference distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\left[\frac{p_i\ln{p_i}+q_i\ln{q_i}}{2}-\left(\frac{p_i+q_i}{2}\right)\ln\left(\frac{p_i+q_i}{2}\right)\right]}$

Returns the Taneja distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\left[\frac{p_i+q_i}{2}\ln\left(\frac{p_i+q_i}{2\sqrt{p_i\cdot{q_i}}}\right)\right]}$

Returns the Kumar-Johnson distance between vectors p and q

$d(p,q)=\sum\limits_{i=1}^{n}{\frac{\left(p_i^2-q_i^2\right)^2}{2(p_i\cdot{q_i})^{3/2}}}$

Returns the average of city block and Chebyshev distances between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}+\max\limits_i(|p_i-q_i|)}{2}$

Similarities

Returns the Intersection similarity between vectors p and q

Returns the Czekanowski similarity between vectors p and q

Returns the Motyka similarity between vectors p and q

Returns the Kulczynski similarity between vectors p and q

Returns the Squared-chord similarity between vectors p and q

Returns the Jaccard similarity between vectors p and q

Returns the Dice similarity between vectors p and q

Returns the Tanimoto similarity between vectors p and q, and accepts the bitVector use, see the test case for an example

Refer to ml-tree-similarity

Contributing

A new metric should normally be in its own file in the src/dist directory. There should be a corresponding test file in test/dist.
The metric should be then added in the exports of src/index.js with a relatively small but understandable name (use camelCase).
It should also be added to this README with either a link to the formula or an inline description.

Authors

License

MIT