{"name":"ml-distance","version":"1.0.2","keywords":["distance","similarity","metric","vector","data","mining","datamining","machine","learning"],"author":"","license":"MIT","_id":"ml-distance@1.0.2","maintainers":[{"name":"targos","email":"targos@live.com"},{"name":"maasencioh","email":"maasencioh@gmail.com"}],"homepage":"https://github.com/mljs/distance","bugs":{"url":"https://github.com/mljs/distance/issues"},"dist":{"shasum":"8eb2dc9a5daf4e66c3213a1fac207de1c5e08b44","tarball":"http://registry-npm.ywwl.com/ml-distance/download/ml-distance-1.0.2.tgz","integrity":"sha512-roLmmragjaPj2ZPHLoA48CkhPAUXBXMk23HDd194Yaw/qxFwvfi0bK5oNxbXakkkgrB596Y8swM4K7urldBgfw==","signatures":[{"sig":"MEYCIQDZejMGIdG2Eyn5tYxXRVK35z3Rt1YiQ4vcF7teW53o2QIhAPi5Nl58G33wsRPuQkVPG5WVy3bCy++KFcGvtvYODK9c","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"size":8239,"noattachment":false,"key":"/ml-distance/-/ml-distance-1.0.2.tgz"},"main":"src/index.js","_from":".","_shasum":"8eb2dc9a5daf4e66c3213a1fac207de1c5e08b44","gitHead":"e440cb768fa87b0be23b0311f9cb176bac58ceab","scripts":{"test":"mocha --require should --reporter mocha-better-spec-reporter --recursive"},"_npmUser":{"name":"maasencioh","email":"maasencioh@gmail.com"},"repository":{"url":"git+https://github.com/mljs/distance.git","type":"git"},"_npmVersion":"2.11.0","description":"Distance and similarity functions to compare vectors","directories":{"lib":"src","test":"test"},"_nodeVersion":"2.2.1","devDependencies":{"del":"^1.1.1","gulp":"^3.8.11","mocha":"latest","yargs":"^1.3.2","should":"latest","gulp-git":"^0.5.3","gulp-bump":"^0.1.11","gulp-size":"^1.1.0","browserify":"^8.1.3","gulp-header":"^1.2.2","gulp-rename":"^1.2.0","gulp-uglify":"^1.1.0","vinyl-buffer":"^1.0.0","gulp-sourcemaps":"^1.3.0","vinyl-source-stream":"^1.0.0","mocha-better-spec-reporter":"latest"},"readme":"# ml-distance\n\n<h3 align=\"center\">\n\n  <a href=\"https://www.zakodium.com\">\n   <img src=\"https://www.zakodium.com/brand/zakodium-logo-white.svg\" width=\"50\" alt=\"Zakodium logo\" />\n  </a>\n\n  <p>\n    Maintained by <a href=\"https://www.zakodium.com\">Zakodium</a>\n  </p>\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/mljs/distance/actions/workflows/nodejs.yml/badge.svg)](https://github.com/mljs/distance/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n\n</h3>\n\nDistance functions to compare vectors.\n\n## Installation\n\n`$ npm i ml-distance`\n\nThis package is ESM-only. CommonJS consumers need Node.js >= 20.19, >= 22.12, or\nany 24.x or later to `require()` it, or should migrate to `import`.\n\n## Usage\n\n```js\nimport { distance, similarity } from 'ml-distance';\n\nconst p = [0, 1, 2, 3, 4];\nconst q = [4, 3, 2, 1, 0];\n\ndistance.euclidean(p, q); // 6.324555320336759\ndistance.manhattan(p, q); // 12\nsimilarity.cosine(p, q); // 0.3333333333333333\n```\n\n## Methods\n\n### Distances\n\n- `euclidean(p, q)`\n\nReturns the [euclidean distance](http://en.wikipedia.org/wiki/Euclidean_distance#n_dimensions) between vectors p and q\n\n$d(p,q)=\\sqrt{\\sum\\limits_{i=1}^{n}(p_i-q_i)^2}$\n\n- `manhattan(p, q)`\n\nReturns the [city block distance](http://en.wikipedia.org/wiki/Taxicab_geometry) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\left|p_i-q_i\\right|}$\n\n- `minkowski(p, q, d)`\n\nReturns the [Minkowski distance](http://en.wikipedia.org/wiki/Minkowski_distance) between vectors p and q for order d\n\n- `chebyshev(p, q)`\n\nReturns the [Chebyshev distance](http://en.wikipedia.org/wiki/Chebyshev_distance) between vectors p and q\n\n$d(p,q)=\\max\\limits_i(|p_i-q_i|)$\n\n- `sorensen(p, q)`\n\nReturns the [Sørensen distance](http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient) between vectors p and q\n\n$d(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{\\left|p_i-q_i\\right|}}{\\sum\\limits_{i=1}^{n}{p_i+q_i}}$\n\n- `gower(p, q)`\n\nReturns the [Gower distance](https://stat.ethz.ch/education/semesters/ss2012/ams/slides/v4.2.pdf) between vectors p and q\n\n$d(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{\\left|p_i-q_i\\right|}}{n}$\n\n- `soergel(p, q)`\n\nReturns the [Soergel distance](http://www.orgchm.bas.bg/~vmonev/SimSearch.pdf) between vectors p and q\n\n$d(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{\\left|p_i-q_i\\right|}}{\\sum\\limits_{i=1}^{n}{max(p_i,q_i)}}$\n\n- `kulczynski(p, q)`\n\nReturns the [Kulczynski distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{\\left|p_i-q_i\\right|}}{\\sum\\limits_{i=1}^{n}{min(p_i,q_i)}}$\n\n- `canberra(p, q)`\n\nReturns the [Canberra distance](http://en.wikipedia.org/wiki/Canberra_distance) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}\\frac{\\left|{p_i-q_i}\\right|}{p_i+q_i}$\n\n- `lorentzian(p, q)`\n\nReturns the [Lorentzian distance](https://stat.ethz.ch/education/semesters/ss2012/ams/slides/v4.2.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}\\ln(\\left|{p_i-q_i}\\right|+1)$\n\n- `intersection(p, q)`\n\nReturns the [Intersection distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=1-\\sum\\limits_{i=1}^{n}min(p_i,q_i)$\n\n- `waveHedges(p, q)`\n\nReturns the [Wave Hedges distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}\\left(1-\\frac{min(p_i,q_i)}{max(p_i,q_i)}\\right)$\n\n- `czekanowski(p, q)`\n\nReturns the [Czekanowski distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=1-\\frac{2\\sum\\limits_{i=1}^{n}{min(p_i,q_i)}}{\\sum\\limits_{i=1}^{n}{p_i+q_i}}$\n\n- `motyka(p, q)`\n\nReturns the [Motyka distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=1-\\frac{\\sum\\limits_{i=1}^{n}{min(p_i,q_i)}}{\\sum\\limits_{i=1}^{n}{p_i+q_i}}$\n\nNote: distance between 2 identical vectors is 0.5 !\n\n- `tanimoto(p, q, [bitVector])`\n\nReturns the [Tanimoto distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q, and accepts the bitVector use, see the test case for an example\n\n- `innerProduct(p, q)`\n\nReturns the [Inner Product similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$s(p,q)=\\sum\\limits_{i=1}^{n}{p_i\\cdot{q_i}}$\n\n- `harmonicMean(p, q)`\n\nReturns the [Harmonic mean similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=2\\sum\\limits_{i=1}^{n}\\frac{p_i\\cdot{q_i}}{p_i+q_i}$\n\n- `jaccard(p, q)`\n\nReturns the [Jaccard distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=1-\\frac{\\sum\\limits_{i=1}^{n}{p_i\\cdot{q_i}}}{\\sum\\limits_{i=1}^{n}{p_i^2}+\\sum\\limits_{i=1}^{n}{q_i^2}-\\sum\\limits_{i=1}^{n}{p_i\\cdot{q_i}}}$\n\n- `dice(p,q)`\n\nReturns the [Dice distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{(p_i-q_i)^2}}{\\sum\\limits_{i=1}^{n}{p_i^2}+\\sum\\limits_{i=1}^{n}{q_i^2}}$\n\n- `fidelity(p, q)`\n\nReturns the [Fidelity similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\sqrt{p_i\\cdot{q_i}}}$\n\n- `bhattacharyya(p, q)`\n\nReturns the [Bhattacharyya distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=-\\ln\\left(\\sum\\limits_{i=1}^{n}{\\sqrt{p_i\\cdot{q_i}}}\\right)$\n\n- `hellinger(p, q)`\n\nReturns the [Hellinger distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=2\\cdot\\sqrt{1-\\sum\\limits_{i=1}^{n}{\\sqrt{p_i\\cdot{q_i}}}}$\n\n- `matusita(p, q)`\n\nReturns the [Matusita distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sqrt{2-2\\cdot\\sum\\limits_{i=1}^{n}{\\sqrt{p_i\\cdot{q_i}}}}$\n\n- `squaredChord(p, q)`\n\nReturns the [Squared-chord distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{(\\sqrt{p_i}-\\sqrt{q_i})^2}$\n\n- `squaredEuclidean(p, q)`\n\nReturns the [squared euclidean distance](http://en.wikipedia.org/wiki/Euclidean_distance#Squared_Euclidean_distance) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{(p_i-q_i)^2}$\n\n- `pearson(p, q)`\n\nReturns the [Pearson distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\frac{(p_i-q_i)^2}{q_i}}$\n\n- `neyman(p, q)`\n\nReturns the [Neyman distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\frac{(p_i-q_i)^2}{p_i}}$\n\n- `squared(p, q)`\n\nReturns the [Squared distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\frac{(p_i-q_i)^2}{p_i+q_i}}$\n\n- `probabilisticSymmetric(p, q)`\n\nReturns the [Probabilistic Symmetric distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=2\\cdot\\sum\\limits_{i=1}^{n}{\\frac{(p_i-q_i)^2}{p_i+q_i}}$\n\n- `divergence(p, q)`\n\nReturns the [Divergence distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=2\\cdot\\sum\\limits_{i=1}^{n}{\\frac{(p_i-q_i)^2}{(p_i+q_i)^2}}$\n\n- `clark(p, q)`\n\nReturns the [Clark distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sqrt{\\sum\\limits_{i=1}^{n}{\\left(\\frac{\\left|p_i-q_i\\right|}{(p_i+q_i)}\\right)^2}}$\n\n- `additiveSymmetric(p, q)`\n\nReturns the [Additive Symmetric distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\frac{(p_i-q_i)^2\\cdot(p_i+q_i)}{p_i\\cdot{q_i}}}$\n\n- `kullbackLeibler(p, q)`\n\nReturns the [Kullback-Leibler distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{p_i\\cdot\\ln\\frac{p_i}{q_i}}$\n\n- `jeffreys(p, q)`\n\nReturns the [Jeffreys distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\left((p_i-q_i)\\ln\\frac{p_i}{q_i}\\right)}$\n\n- `kdivergence(p, q)`\n\nReturns the [K divergence distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\sum\\limits_{i=1}^{n}{\\left(p_i\\cdot\\ln\\frac{2p_i}{p_i+q_i}\\right)}$\n\n- `topsoe(p, q)`\n\nReturns the [Topsøe distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$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)}$\n\n- `jensenShannon(p, q)`\n\nReturns the [Jensen-Shannon distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$d(p,q)=\\frac{1}{2}\\left[\\sum\\limits_{i=1}^{n}{p_i\\cdot\\ln\\frac{2p_i}{p_i+q_i}}+\\sum\\limits_{i=1}^{n}{q_i\\cdot\\ln\\frac{2q_i}{p_i+q_i}}\\right]$\n\n- `jensenDifference(p, q)`\n\nReturns the [Jensen difference distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$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]}$\n\n- `taneja(p, q)`\n\nReturns the [Taneja distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$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]}$\n\n- `kumarJohnson(p, q)`\n\nReturns the [Kumar-Johnson distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$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}}}$\n\n- `avg(p, q)`\n\nReturns the average of city block and Chebyshev distances between vectors p and q\n\n$d(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{\\left|p_i-q_i\\right|}+\\max\\limits_i(|p_i-q_i|)}{2}$\n\n### Similarities\n\n- `intersection(p, q)`\n\nReturns the [Intersection similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n- `czekanowski(p, q)`\n\nReturns the [Czekanowski similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n- `motyka(p, q)`\n\nReturns the [Motyka similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n- `kulczynski(p, q)`\n\nReturns the [Kulczynski similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n- `squaredChord(p, q)`\n\nReturns the [Squared-chord similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n- `cosine(p, q)`\n\nReturns the [Cosine similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$s(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{p_i\\cdot{q_i}}}{\\sqrt{\\sum\\limits_{i=1}^{n}{p_i^2}}\\sqrt{\\sum\\limits_{i=1}^{n}{q_i^2}}}$\n\n- `kumarHassebrook(p, q)`\n\nReturns the [Kumar-Hassebrook similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n$s(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{p_i\\cdot{q_i}}}{\\sum\\limits_{i=1}^{n}{p_i^2}+\\sum\\limits_{i=1}^{n}{q_i^2}-\\sum\\limits_{i=1}^{n}{p_i\\cdot{q_i}}}$\n\n- `pearson(p, q)`\n\nReturns the [Pearson correlation](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient) between vectors p and q, i.e. the cosine similarity of the mean-centred vectors\n\n- `ruzicka(p, q)`\n\nReturns the [Ruzicka similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf), also known as the [weighted Jaccard similarity](https://en.wikipedia.org/wiki/Jaccard_index#Weighted_Jaccard_similarity_and_distance), between vectors p and q. The matching distance is `soergel`.\n\n$s(p,q)=\\frac{\\sum\\limits_{i=1}^{n}{min(p_i,q_i)}}{\\sum\\limits_{i=1}^{n}{max(p_i,q_i)}}$\n\n- `dice(p, q)`\n\nReturns the [Dice similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q\n\n- `tanimoto(p, q, [bitVector])`\n\nReturns the [Tanimoto similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q, and accepts the bitVector use, see the test case for an example\n\n- `tree(a,b, from, to, [options])`\n\nRefer to [ml-tree-similarity](https://github.com/mljs/tree-similarity)\n\n## Contributing\n\nA new metric should normally be in its own file in the `src/distances` (or `src/similarities`) directory. There should be a corresponding test file in the neighbouring `__tests__` directory.  \nThe metric should be then added in the exports of `src/distances.ts` (or `src/similarities.ts`) with a relatively small but understandable name (use camelCase).  \nIt should also be added to this README with either a link to the formula or an inline description.\n\n## Authors\n\n- [Michaël Zasso](https://github.com/targos)\n- [Miguel Angel Asencio Hurtado](https://github.com/maasencioh)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/ml-distance.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/ml-distance\n[codecov-image]: https://img.shields.io/codecov/c/github/mljs/distance.svg\n[codecov-url]: https://codecov.io/gh/mljs/distance\n[download-image]: https://img.shields.io/npm/dm/ml-distance.svg?style=flat-square\n[download-url]: https://npmjs.org/package/ml-distance\n","publish_time":1435060354300,"_cnpm_publish_time":1435060354300,"dist-tags":{"latest":"5.0.0"}}