Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-distance

Package Overview
Dependencies
Maintainers
8
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-distance - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

lib/index.js

19

History.md

@@ -0,1 +1,20 @@

# [3.0.0](https://github.com/mljs/distance/compare/v2.1.1...v3.0.0) (2019-06-29)
### Bug Fixes
* use approximates values for testing ([10d563e](https://github.com/mljs/distance/commit/10d563e))
### chore
* update dependencies and remove support for Node.js 6 ([760e28c](https://github.com/mljs/distance/commit/760e28c))
### BREAKING CHANGES
* Node.js 6 is no longer supported.
<a name="2.1.1"></a>

@@ -2,0 +21,0 @@ ## [2.1.1](https://github.com/mljs/distance/compare/v2.1.0...v2.1.1) (2016-08-03)

37

package.json
{
"name": "ml-distance",
"version": "2.1.1",
"version": "3.0.0",
"description": "Distance and similarity functions to compare vectors",
"main": "src/index.js",
"directories": {
"lib": "src",
"test": "test"
},
"main": "lib/index.js",
"module": "src/index.js",
"files": [
"src",
"lib"
],
"scripts": {
"test": "mocha --require should --reporter mocha-better-spec-reporter --recursive"
"compile": "rollup -c",
"eslint": "eslint src",
"eslint-fix": "npm run eslint -- --fix",
"prepublishOnly": "npm run compile",
"test": "npm run test-coverage && npm run eslint",
"test-only": "jest",
"test-coverage": "jest --coverage"
},

@@ -38,11 +45,15 @@ "repository": {

"devDependencies": {
"mocha": "^3.0.0",
"mocha-better-spec-reporter": "^3.0.2",
"should": "^10.0.0"
"@babel/plugin-transform-modules-commonjs": "^7.4.4",
"eslint": "^6.0.1",
"eslint-config-cheminfo": "^1.20.1",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jest": "^22.7.1",
"jest": "^24.8.0",
"rollup": "^1.16.3"
},
"dependencies": {
"ml-distance-euclidean": "^1.0.0",
"ml-stat": "^1.0.1",
"ml-tree-similarity": "^0.0.0"
"ml-array-mean": "^1.1.1",
"ml-distance-euclidean": "^2.0.0",
"ml-tree-similarity": "^1.0.0"
}
}
# ml-distance
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![npm download][download-image]][download-url]
Distance functions to compare vectors
Distance functions to compare vectors.
## Installation
`$ npm install ml-distance`
`$ npm i ml-distance`

@@ -18,214 +17,213 @@ ## Methods

* `euclidean(p, q)`
- `euclidean(p, q)`
Returns the [euclidean distance](http://en.wikipedia.org/wiki/Euclidean_distance#n_dimensions) between vectors p and q
* `manhattan(p, q)`
- `manhattan(p, q)`
Returns the [city block distance](http://en.wikipedia.org/wiki/Taxicab_geometry) between vectors p and q
* `minkowski(p, q, d)`
- `minkowski(p, q, d)`
Returns the [Minkowski distance](http://en.wikipedia.org/wiki/Minkowski_distance) between vectors p and q for order d
* `chebyshev(p, q)`
- `chebyshev(p, q)`
Returns the [Chebyshev distance](http://en.wikipedia.org/wiki/Chebyshev_distance) between vectors p and q
* `sorensen(p, q)`
- `sorensen(p, q)`
Returns the [Sørensen distance](http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient) between vectors p and q
* `gower(p, q)`
- `gower(p, q)`
Returns the [Gower distance](https://stat.ethz.ch/education/semesters/ss2012/ams/slides/v4.2.pdf) between vectors p and q
* `soergel(p, q)`
- `soergel(p, q)`
Returns the [Soergel distance](http://www.orgchm.bas.bg/~vmonev/SimSearch.pdf) between vectors p and q
* `kulczynski(p, q)`
- `kulczynski(p, q)`
Returns the [Kulczynski distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `canberra(p, q)`
- `canberra(p, q)`
Returns the [Canberra distance](http://en.wikipedia.org/wiki/Canberra_distance) between vectors p and q
* `lorentzian(p, q)`
- `lorentzian(p, q)`
Returns the [Lorentzian distance](https://stat.ethz.ch/education/semesters/ss2012/ams/slides/v4.2.pdf) between vectors p and q
* `intersection(p, q)`
- `intersection(p, q)`
Returns the [Intersection distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `waveHedges(p, q)`
- `waveHedges(p, q)`
Returns the [Wave Hedges distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `czekanowski(p, q)`
- `czekanowski(p, q)`
Returns the [Czekanowski distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `motyka(p, q)`
- `motyka(p, q)`
Returns the [Motyka distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `ruzicka(p, q)`
- `ruzicka(p, q)`
Returns the [Ruzicka similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `tanimoto(p, q, [bitVector])`
- `tanimoto(p, q, [bitVector])`
Returns 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
* `innerProduct(p, q)`
- `innerProduct(p, q)`
Returns the [Inner Product similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `harmonicMean(p, q)`
- `harmonicMean(p, q)`
Returns the [Harmonic mean similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `cosine(p, q)`
- `cosine(p, q)`
Returns the [Cosine similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `kumarHassebrook(p, q)`
- `kumarHassebrook(p, q)`
Returns the [Kumar-Hassebrook similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `jaccard(p, q)`
- `jaccard(p, q)`
Returns the [Jaccard distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `dice(p, q)`
- `dice(p, q)`
Returns the [Dice distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `fidelity(p, q)`
- `fidelity(p, q)`
Returns the [Fidelity similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `bhattacharyya(p, q)`
- `bhattacharyya(p, q)`
Returns the [Bhattacharyya distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `hellinger(p, q)`
- `hellinger(p, q)`
Returns the [Hellinger distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `matusita(p, q)`
- `matusita(p, q)`
Returns the [Matusita distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `squaredChord(p, q)`
- `squaredChord(p, q)`
Returns the [Squared-chord distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `squaredEuclidean(p, q)`
- `squaredEuclidean(p, q)`
Returns the [squared euclidean distance](http://en.wikipedia.org/wiki/Euclidean_distance#Squared_Euclidean_distance) between vectors p and q
* `pearson(p, q)`
- `pearson(p, q)`
Returns the [Pearson distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `neyman(p, q)`
- `neyman(p, q)`
Returns the [Neyman distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `squared(p, q)`
- `squared(p, q)`
Returns the [Squared distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `probabilisticSymmetric(p, q)`
- `probabilisticSymmetric(p, q)`
Returns the [Probabilistic Symmetric distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `divergence(p, q)`
- `divergence(p, q)`
Returns the [Divergence distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `clark(p, q)`
- `clark(p, q)`
Returns the [Clark distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `additiveSymmetric(p, q)`
- `additiveSymmetric(p, q)`
Returns the [Additive Symmetric distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `kullbackLeibler(p, q)`
- `kullbackLeibler(p, q)`
Returns the [Kullback-Leibler distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `jeffreys(p, q)`
- `jeffreys(p, q)`
Returns the [Jeffreys distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `kdivergence(p, q)`
- `kdivergence(p, q)`
Returns the [K divergence distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `topsoe(p, q)`
- `topsoe(p, q)`
Returns the [Topsøe distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `jensenShannon(p, q)`
- `jensenShannon(p, q)`
Returns the [Jensen-Shannon distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `jensenDifference(p, q)`
- `jensenDifference(p, q)`
Returns the [Jensen difference distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `taneja(p, q)`
- `taneja(p, q)`
Returns the [Taneja distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `kumarJohnson(p, q)`
- `kumarJohnson(p, q)`
Returns the [Kumar-Johnson distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `avg(p, q)`
- `avg(p, q)`
Returns the average of city block and Chebyshev distances between vectors p and q
### Similarities
* `intersection(p, q)`
- `intersection(p, q)`
Returns the [Intersection similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `czekanowski(p, q)`
- `czekanowski(p, q)`
Returns the [Czekanowski similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `motyka(p, q)`
- `motyka(p, q)`
Returns the [Motyka similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `kulczynski(p, q)`
- `kulczynski(p, q)`
Returns the [Kulczynski similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `squaredChord(p, q)`
- `squaredChord(p, q)`
Returns the [Squared-chord similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `jaccard(p, q)`
- `jaccard(p, q)`
Returns the [Jaccard similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `dice(p, q)`
- `dice(p, q)`
Returns the [Dice similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
* `tanimoto(p, q, [bitVector])`
- `tanimoto(p, q, [bitVector])`
Returns 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
* `tree(a,b, from, to, [options])`
- `tree(a,b, from, to, [options])`

@@ -240,17 +238,10 @@ Refer to [ml-tree-similarity](https://github.com/mljs/tree-similarity)

## Test
```js
$ npm install
$ npm test
```
## Authors
- [Michaël Zasso](https://github.com/targos)
- [Miguel Angel Asencio Hurtado](https://github.com/maasencioh)
- [Michaël Zasso](https://github.com/targos)
- [Miguel Angel Asencio Hurtado](https://github.com/maasencioh)
## License
[MIT](./LICENSE)
[MIT](./LICENSE)

@@ -261,5 +252,3 @@ [npm-image]: https://img.shields.io/npm/v/ml-distance.svg?style=flat-square

[travis-url]: https://travis-ci.org/mljs/distance
[david-image]: https://img.shields.io/david/mljs/distance.svg?style=flat-square
[david-url]: https://david-dm.org/mljs/distance
[download-image]: https://img.shields.io/npm/dm/ml-distance.svg?style=flat-square
[download-url]: https://npmjs.org/package/ml-distance

@@ -1,46 +0,43 @@

"use strict";
export { euclidean, squaredEuclidean } from 'ml-distance-euclidean';
exports.additiveSymmetric = require('./distances/additiveSymmetric');
exports.avg = require('./distances/avg');
exports.bhattacharyya = require('./distances/bhattacharyya');
exports.canberra = require('./distances/canberra');
exports.chebyshev = require('./distances/chebyshev');
exports.clark = require('./distances/clark');
exports.czekanowski = require('./distances/czekanowski');
exports.dice = require('./distances/dice');
exports.divergence = require('./distances/divergence');
exports.euclidean = require('ml-distance-euclidean');
exports.fidelity = require('./distances/fidelity');
exports.gower = require('./distances/gower');
exports.harmonicMean = require('./distances/harmonicMean');
exports.hellinger = require('./distances/hellinger');
exports.innerProduct = require('./distances/innerProduct');
exports.intersection = require('./distances/intersection');
exports.jaccard = require('./distances/jaccard');
exports.jeffreys = require('./distances/jeffreys');
exports.jensenDifference = require('./distances/jensenDifference');
exports.jensenShannon = require('./distances/jensenShannon');
exports.kdivergence = require('./distances/kdivergence');
exports.kulczynski = require('./distances/kulczynski');
exports.kullbackLeibler = require('./distances/kullbackLeibler');
exports.kumarHassebrook = require('./distances/kumarHassebrook');
exports.kumarJohnson = require('./distances/kumarJohnson');
exports.lorentzian = require('./distances/lorentzian');
exports.manhattan = require('./distances/manhattan');
exports.matusita = require('./distances/matusita');
exports.minkowski = require('./distances/minkowski');
exports.motyka = require('./distances/motyka');
exports.neyman = require('./distances/neyman');
exports.pearson = require('./distances/pearson');
exports.probabilisticSymmetric = require('./distances/probabilisticSymmetric');
exports.ruzicka = require('./distances/ruzicka');
exports.soergel = require('./distances/soergel');
exports.sorensen = require('./distances/sorensen');
exports.squared = require('./distances/squared');
exports.squaredChord = require('./distances/squaredChord');
exports.squaredEuclidean = require('ml-distance-euclidean').squared;
exports.taneja = require('./distances/taneja');
exports.tanimoto = require('./distances/tanimoto');
exports.topsoe = require('./distances/topsoe');
exports.tree = require('ml-tree-similarity');
exports.waveHedges = require('./distances/waveHedges');
export { default as additiveSymmetric } from './distances/additiveSymmetric';
export { default as avg } from './distances/avg';
export { default as bhattacharyya } from './distances/bhattacharyya';
export { default as canberra } from './distances/canberra';
export { default as chebyshev } from './distances/chebyshev';
export { default as clark } from './distances/clark';
export { default as czekanowski } from './distances/czekanowski';
export { default as dice } from './distances/dice';
export { default as divergence } from './distances/divergence';
export { default as fidelity } from './distances/fidelity';
export { default as gower } from './distances/gower';
export { default as harmonicMean } from './distances/harmonicMean';
export { default as hellinger } from './distances/hellinger';
export { default as innerProduct } from './distances/innerProduct';
export { default as intersection } from './distances/intersection';
export { default as jaccard } from './distances/jaccard';
export { default as jeffreys } from './distances/jeffreys';
export { default as jensenDifference } from './distances/jensenDifference';
export { default as jensenShannon } from './distances/jensenShannon';
export { default as kdivergence } from './distances/kdivergence';
export { default as kulczynski } from './distances/kulczynski';
export { default as kullbackLeibler } from './distances/kullbackLeibler';
export { default as kumarHassebrook } from './distances/kumarHassebrook';
export { default as kumarJohnson } from './distances/kumarJohnson';
export { default as lorentzian } from './distances/lorentzian';
export { default as manhattan } from './distances/manhattan';
export { default as matusita } from './distances/matusita';
export { default as minkowski } from './distances/minkowski';
export { default as motyka } from './distances/motyka';
export { default as neyman } from './distances/neyman';
export { default as pearson } from './distances/pearson';
export { default as probabilisticSymmetric } from './distances/probabilisticSymmetric';
export { default as ruzicka } from './distances/ruzicka';
export { default as soergel } from './distances/soergel';
export { default as sorensen } from './distances/sorensen';
export { default as squared } from './distances/squared';
export { default as squaredChord } from './distances/squaredChord';
export { default as taneja } from './distances/taneja';
export { default as tanimoto } from './distances/tanimoto';
export { default as topsoe } from './distances/topsoe';
export { default as waveHedges } from './distances/waveHedges';

@@ -1,9 +0,9 @@

module.exports = function additiveSymmetric(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i]) * (a[i] + b[i])) / (a[i] * b[i]);
}
return 2 * d;
};
export default function additiveSymmetric(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i]) * (a[i] + b[i])) / (a[i] * b[i]);
}
return 2 * d;
}

@@ -1,14 +0,14 @@

module.exports = function avg(a, b) {
var ii = a.length,
max = 0,
ans = 0,
aux = 0;
for (var i = 0; i < ii ; i++) {
aux = Math.abs(a[i] - b[i]);
ans += aux;
if (max < aux) {
max = aux;
}
export default function avg(a, b) {
var ii = a.length;
var max = 0;
var ans = 0;
var aux = 0;
for (var i = 0; i < ii; i++) {
aux = Math.abs(a[i] - b[i]);
ans += aux;
if (max < aux) {
max = aux;
}
return (max + ans) / 2;
};
}
return (max + ans) / 2;
}

@@ -1,8 +0,8 @@

module.exports = function bhattacharyya(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return - Math.log(ans);
};
export default function bhattacharyya(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return -Math.log(ans);
}

@@ -1,8 +0,8 @@

module.exports = function canberra(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.abs(a[i] - b[i]) / (a[i] + b[i]);
}
return ans;
};
export default function canberra(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.abs(a[i] - b[i]) / (a[i] + b[i]);
}
return ans;
}

@@ -1,12 +0,12 @@

module.exports = function chebyshev(a, b) {
var ii = a.length,
max = 0,
aux = 0;
for (var i = 0; i < ii ; i++) {
aux = Math.abs(a[i] - b[i]);
if (max < aux) {
max = aux;
}
export default function chebyshev(a, b) {
var ii = a.length;
var max = 0;
var aux = 0;
for (var i = 0; i < ii; i++) {
aux = Math.abs(a[i] - b[i]);
if (max < aux) {
max = aux;
}
return max;
};
}
return max;
}

@@ -1,9 +0,11 @@

module.exports = function clark(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += Math.sqrt(((a[i] - b[i]) * (a[i] - b[i])) / ((a[i] + b[i]) * (a[i] + b[i])));
}
return 2 * d;
};
export default function clark(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += Math.sqrt(
((a[i] - b[i]) * (a[i] - b[i])) / ((a[i] + b[i]) * (a[i] + b[i]))
);
}
return 2 * d;
}

@@ -1,7 +0,5 @@

'use strict';
import czekanowskiSimilarity from '../similarities/czekanowski';
const czekanowskiSimilarity = require('../similarities/czekanowski');
module.exports = function czekanowskiDistance(a, b) {
return 1 - czekanowskiSimilarity(a, b);
};
export default function czekanowskiDistance(a, b) {
return 1 - czekanowskiSimilarity(a, b);
}

@@ -1,12 +0,12 @@

module.exports = function dice(a, b) {
var ii = a.length,
p = 0,
q1 = 0,
q2 = 0;
for (var i = 0; i < ii ; i++) {
p += a[i] * a[i];
q1 += b[i] * b[i];
q2 += (a[i] - b[i]) * (a[i] - b[i]);
}
return q2 / (p + q1);
};
export default function dice(a, b) {
var ii = a.length;
var p = 0;
var q1 = 0;
var q2 = 0;
for (var i = 0; i < ii; i++) {
p += a[i] * a[i];
q1 += b[i] * b[i];
q2 += (a[i] - b[i]) * (a[i] - b[i]);
}
return q2 / (p + q1);
}

@@ -1,9 +0,9 @@

module.exports = function divergence(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / ((a[i] + b[i]) * (a[i] + b[i]));
}
return 2 * d;
};
export default function divergence(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / ((a[i] + b[i]) * (a[i] + b[i]));
}
return 2 * d;
}

@@ -1,8 +0,8 @@

module.exports = function fidelity(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return ans;
};
export default function fidelity(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return ans;
}

@@ -1,8 +0,8 @@

module.exports = function gower(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.abs(a[i] - b[i]);
}
return ans / ii;
};
export default function gower(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.abs(a[i] - b[i]);
}
return ans / ii;
}

@@ -1,8 +0,8 @@

module.exports = function harmonicMean(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += (a[i] * b[i]) / (a[i] + b[i]);
}
return 2 * ans;
};
export default function harmonicMean(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += (a[i] * b[i]) / (a[i] + b[i]);
}
return 2 * ans;
}

@@ -1,8 +0,8 @@

module.exports = function hellinger(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return 2 * Math.sqrt(1 - ans);
};
export default function hellinger(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return 2 * Math.sqrt(1 - ans);
}

@@ -1,8 +0,8 @@

module.exports = function innerProduct(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += a[i] * b[i];
}
return ans;
};
export default function innerProduct(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += a[i] * b[i];
}
return ans;
}

@@ -1,8 +0,8 @@

module.exports = function intersection(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.min(a[i], b[i]);
}
return 1 - ans;
};
export default function intersection(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.min(a[i], b[i]);
}
return 1 - ans;
}

@@ -1,14 +0,14 @@

module.exports = function jaccard(a, b) {
var ii = a.length,
p1 = 0,
p2 = 0,
q1 = 0,
q2 = 0;
for (var i = 0; i < ii ; i++) {
p1 += a[i] * b[i];
p2 += a[i] * a[i];
q1 += b[i] * b[i];
q2 += (a[i] - b[i]) * (a[i] - b[i]);
}
return q2 / (p2 + q1 - p1);
};
export default function jaccard(a, b) {
var ii = a.length;
var p1 = 0;
var p2 = 0;
var q1 = 0;
var q2 = 0;
for (var i = 0; i < ii; i++) {
p1 += a[i] * b[i];
p2 += a[i] * a[i];
q1 += b[i] * b[i];
q2 += (a[i] - b[i]) * (a[i] - b[i]);
}
return q2 / (p2 + q1 - p1);
}

@@ -1,8 +0,8 @@

module.exports = function jeffreys(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += (a[i] - b[i]) * Math.log(a[i] / b[i]);
}
return ans;
};
export default function jeffreys(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += (a[i] - b[i]) * Math.log(a[i] / b[i]);
}
return ans;
}

@@ -1,8 +0,10 @@

module.exports = function jensenDifference(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += ((a[i] * Math.log(a[i]) + b[i] * Math.log(b[i])) / 2) - ((a[i] + b[i]) / 2) * Math.log((a[i] + b[i]) / 2);
}
return ans;
};
export default function jensenDifference(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans +=
(a[i] * Math.log(a[i]) + b[i] * Math.log(b[i])) / 2 -
((a[i] + b[i]) / 2) * Math.log((a[i] + b[i]) / 2);
}
return ans;
}

@@ -1,10 +0,10 @@

module.exports = function jensenShannon(a, b) {
var ii = a.length,
p = 0,
q = 0;
for (var i = 0; i < ii ; i++) {
p += a[i] * Math.log(2 * a[i] / (a[i] + b[i]));
q += b[i] * Math.log(2 * b[i] / (a[i] + b[i]));
}
return (p + q) / 2;
};
export default function jensenShannon(a, b) {
var ii = a.length;
var p = 0;
var q = 0;
for (var i = 0; i < ii; i++) {
p += a[i] * Math.log((2 * a[i]) / (a[i] + b[i]));
q += b[i] * Math.log((2 * b[i]) / (a[i] + b[i]));
}
return (p + q) / 2;
}

@@ -1,8 +0,8 @@

module.exports = function kdivergence(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += a[i] * Math.log(2 * a[i] / (a[i] + b[i]));
}
return ans;
};
export default function kdivergence(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += a[i] * Math.log((2 * a[i]) / (a[i] + b[i]));
}
return ans;
}

@@ -1,10 +0,10 @@

module.exports = function kulczynski(a, b) {
var ii = a.length,
up = 0,
down = 0;
for (var i = 0; i < ii ; i++) {
up += Math.abs(a[i] - b[i]);
down += Math.min(a[i],b[i]);
}
return up / down;
};
export default function kulczynski(a, b) {
var ii = a.length;
var up = 0;
var down = 0;
for (var i = 0; i < ii; i++) {
up += Math.abs(a[i] - b[i]);
down += Math.min(a[i], b[i]);
}
return up / down;
}

@@ -1,8 +0,8 @@

module.exports = function kullbackLeibler(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += a[i] * Math.log(a[i] / b[i]);
}
return ans;
};
export default function kullbackLeibler(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += a[i] * Math.log(a[i] / b[i]);
}
return ans;
}

@@ -1,12 +0,12 @@

module.exports = function kumarHassebrook(a, b) {
var ii = a.length,
p = 0,
p2 = 0,
q2 = 0;
for (var i = 0; i < ii ; i++) {
p += a[i] * b[i];
p2 += a[i] * a[i];
q2 += b[i] * b[i];
}
return p / (p2 + q2 - p);
};
export default function kumarHassebrook(a, b) {
var ii = a.length;
var p = 0;
var p2 = 0;
var q2 = 0;
for (var i = 0; i < ii; i++) {
p += a[i] * b[i];
p2 += a[i] * a[i];
q2 += b[i] * b[i];
}
return p / (p2 + q2 - p);
}

@@ -1,8 +0,9 @@

module.exports = function kumarJohnson(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.pow(a[i] * a[i] - b[i] * b[i],2) / (2 * Math.pow(a[i] * b[i],1.5));
}
return ans;
};
export default function kumarJohnson(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans +=
Math.pow(a[i] * a[i] - b[i] * b[i], 2) / (2 * Math.pow(a[i] * b[i], 1.5));
}
return ans;
}

@@ -1,8 +0,8 @@

module.exports = function lorentzian(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.log(Math.abs(a[i] - b[i]) + 1);
}
return ans;
};
export default function lorentzian(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.log(Math.abs(a[i] - b[i]) + 1);
}
return ans;
}

@@ -1,9 +0,9 @@

module.exports = function manhattan(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += Math.abs(a[i] - b[i]);
}
return d;
};
export default function manhattan(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += Math.abs(a[i] - b[i]);
}
return d;
}

@@ -1,8 +0,8 @@

module.exports = function matusita(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return Math.sqrt(2 - 2 * ans);
};
export default function matusita(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += Math.sqrt(a[i] * b[i]);
}
return Math.sqrt(2 - 2 * ans);
}

@@ -1,9 +0,9 @@

module.exports = function minkowski(a, b, p) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += Math.pow(Math.abs(a[i] - b[i]),p);
}
return Math.pow(d,(1/p));
};
export default function minkowski(a, b, p) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += Math.pow(Math.abs(a[i] - b[i]), p);
}
return Math.pow(d, 1 / p);
}

@@ -1,10 +0,10 @@

module.exports = function motyka(a, b) {
var ii = a.length,
up = 0,
down = 0;
for (var i = 0; i < ii ; i++) {
up += Math.min(a[i], b[i]);
down += a[i] + b[i];
}
return 1 - (up / down);
};
export default function motyka(a, b) {
var ii = a.length;
var up = 0;
var down = 0;
for (var i = 0; i < ii; i++) {
up += Math.min(a[i], b[i]);
down += a[i] + b[i];
}
return 1 - up / down;
}

@@ -1,9 +0,9 @@

module.exports = function neyman(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / a[i];
}
return d;
};
export default function neyman(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / a[i];
}
return d;
}

@@ -1,9 +0,9 @@

module.exports = function pearson(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / b[i];
}
return d;
};
export default function pearson(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / b[i];
}
return d;
}

@@ -1,9 +0,9 @@

module.exports = function probabilisticSymmetric(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / (a[i] + b[i]);
}
return 2 * d;
};
export default function probabilisticSymmetric(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / (a[i] + b[i]);
}
return 2 * d;
}

@@ -1,10 +0,10 @@

module.exports = function ruzicka(a, b) {
var ii = a.length,
up = 0,
down = 0;
for (var i = 0; i < ii ; i++) {
up += Math.min(a[i],b[i]);
down += Math.max(a[i],b[i]);
}
return up / down;
};
export default function ruzicka(a, b) {
var ii = a.length;
var up = 0;
var down = 0;
for (var i = 0; i < ii; i++) {
up += Math.min(a[i], b[i]);
down += Math.max(a[i], b[i]);
}
return up / down;
}

@@ -1,10 +0,10 @@

module.exports = function soergel(a, b) {
var ii = a.length,
up = 0,
down = 0;
for (var i = 0; i < ii ; i++) {
up += Math.abs(a[i] - b[i]);
down += Math.max(a[i],b[i]);
}
return up / down;
};
export default function soergel(a, b) {
var ii = a.length;
var up = 0;
var down = 0;
for (var i = 0; i < ii; i++) {
up += Math.abs(a[i] - b[i]);
down += Math.max(a[i], b[i]);
}
return up / down;
}

@@ -1,10 +0,10 @@

module.exports = function sorensen(a, b) {
var ii = a.length,
up = 0,
down = 0;
for (var i = 0; i < ii ; i++) {
up += Math.abs(a[i] - b[i]);
down += a[i] + b[i];
}
return up / down;
};
export default function sorensen(a, b) {
var ii = a.length;
var up = 0;
var down = 0;
for (var i = 0; i < ii; i++) {
up += Math.abs(a[i] - b[i]);
down += a[i] + b[i];
}
return up / down;
}

@@ -1,9 +0,9 @@

module.exports = function squared(a, b) {
var i = 0,
ii = a.length,
d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / (a[i] + b[i]);
}
return d;
};
export default function squared(a, b) {
var i = 0;
var ii = a.length;
var d = 0;
for (; i < ii; i++) {
d += ((a[i] - b[i]) * (a[i] - b[i])) / (a[i] + b[i]);
}
return d;
}

@@ -1,8 +0,9 @@

module.exports = function squaredChord(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += (Math.sqrt(a[i]) - Math.sqrt(b[i])) * (Math.sqrt(a[i]) - Math.sqrt(b[i]));
}
return ans;
};
export default function squaredChord(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans +=
(Math.sqrt(a[i]) - Math.sqrt(b[i])) * (Math.sqrt(a[i]) - Math.sqrt(b[i]));
}
return ans;
}

@@ -1,8 +0,10 @@

module.exports = function taneja(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += (a[i] + b[i]) / 2 * Math.log((a[i] + b[i]) / (2 * Math.sqrt(a[i] * b[i])));
}
return ans;
};
export default function taneja(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans +=
((a[i] + b[i]) / 2) *
Math.log((a[i] + b[i]) / (2 * Math.sqrt(a[i] * b[i])));
}
return ans;
}

@@ -1,18 +0,18 @@

var tanimotoS = require('./../similarities/tanimoto');
import tanimotoS from '../similarities/tanimoto';
module.exports = function tanimoto(a, b, bitvector) {
if (bitvector)
return 1 - tanimotoS(a, b, bitvector);
else {
var ii = a.length,
p = 0,
q = 0,
m = 0;
for (var i = 0; i < ii ; i++) {
p += a[i];
q += b[i];
m += Math.min(a[i],b[i]);
}
return (p + q - 2 * m) / (p + q - m);
export default function tanimoto(a, b, bitvector) {
if (bitvector) {
return 1 - tanimotoS(a, b, bitvector);
} else {
var ii = a.length;
var p = 0;
var q = 0;
var m = 0;
for (var i = 0; i < ii; i++) {
p += a[i];
q += b[i];
m += Math.min(a[i], b[i]);
}
};
return (p + q - 2 * m) / (p + q - m);
}
}

@@ -1,8 +0,10 @@

module.exports = function topsoe(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += a[i] * Math.log(2 * a[i] / (a[i] + b[i])) + b[i] * Math.log(2 * b[i] / (a[i] + b[i]));
}
return ans;
};
export default function topsoe(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans +=
a[i] * Math.log((2 * a[i]) / (a[i] + b[i])) +
b[i] * Math.log((2 * b[i]) / (a[i] + b[i]));
}
return ans;
}

@@ -1,8 +0,8 @@

module.exports = function waveHedges(a, b) {
var ii = a.length,
ans = 0;
for (var i = 0; i < ii ; i++) {
ans += 1 - (Math.min(a[i], b[i]) / Math.max(a[i], b[i]));
}
return ans;
};
export default function waveHedges(a, b) {
var ii = a.length;
var ans = 0;
for (var i = 0; i < ii; i++) {
ans += 1 - Math.min(a[i], b[i]) / Math.max(a[i], b[i]);
}
return ans;
}

@@ -1,4 +0,4 @@

'use strict';
import * as distance from './distances';
import * as similarity from './similarities';
exports.distance = require('./distances');
exports.similarity = require('./similarities');
export { distance, similarity };

@@ -1,12 +0,14 @@

"use strict";
import * as tree from 'ml-tree-similarity';
exports.cosine = require('./similarities/cosine');
exports.czekanowski = require('./similarities/czekanowski');
exports.dice = require('./similarities/dice');
exports.intersection = require('./similarities/intersection');
exports.jaccard = require('./similarities/jaccard');
exports.kulczynski = require('./similarities/kulczynski');
exports.motyka = require('./similarities/motyka');
exports.pearson = require('./similarities/pearson');
exports.squaredChord = require('./similarities/squaredChord');
exports.tanimoto = require('./similarities/tanimoto');
export { tree };
export { default as cosine } from './similarities/cosine';
export { default as czekanowski } from './similarities/czekanowski';
export { default as dice } from './similarities/dice';
export { default as intersection } from './similarities/intersection';
export { default as jaccard } from './similarities/jaccard';
export { default as kulczynski } from './similarities/kulczynski';
export { default as motyka } from './similarities/motyka';
export { default as pearson } from './similarities/pearson';
export { default as squaredChord } from './similarities/squaredChord';
export { default as tanimoto } from './similarities/tanimoto';

@@ -1,12 +0,12 @@

module.exports = function cosine(a, b) {
var ii = a.length,
p = 0,
p2 = 0,
q2 = 0;
for (var i = 0; i < ii ; i++) {
p += a[i] * b[i];
p2 += a[i] * a[i];
q2 += b[i] * b[i];
}
return p / (Math.sqrt(p2) * Math.sqrt(q2));
};
export default function cosine(a, b) {
var ii = a.length;
var p = 0;
var p2 = 0;
var q2 = 0;
for (var i = 0; i < ii; i++) {
p += a[i] * b[i];
p2 += a[i] * a[i];
q2 += b[i] * b[i];
}
return p / (Math.sqrt(p2) * Math.sqrt(q2));
}

@@ -1,11 +0,9 @@

'use strict';
module.exports = function czekanowskiSimilarity(a, b) {
var up = 0;
var down = 0;
for (var i = 0; i < a.length; i++) {
up += Math.min(a[i], b[i]);
down += a[i] + b[i];
}
return 2 * up / down;
};
export default function czekanowskiSimilarity(a, b) {
var up = 0;
var down = 0;
for (var i = 0; i < a.length; i++) {
up += Math.min(a[i], b[i]);
down += a[i] + b[i];
}
return (2 * up) / down;
}

@@ -1,5 +0,5 @@

var diceD = require('./../distances/dice');
import diceD from '../distances/dice';
module.exports = function dice(a, b) {
return 1 - diceD(a,b);
};
export default function dice(a, b) {
return 1 - diceD(a, b);
}

@@ -1,5 +0,5 @@

var intersectionD = require('./../distances/intersection');
import intersectionD from '../distances/intersection';
module.exports = function intersection(a, b) {
return 1 - intersectionD(a,b);
};
export default function intersection(a, b) {
return 1 - intersectionD(a, b);
}

@@ -1,5 +0,5 @@

var jaccardD = require('./../distances/jaccard');
import jaccardD from '../distances/jaccard';
module.exports = function jaccard(a, b) {
return 1 - jaccardD(a, b);
};
export default function jaccard(a, b) {
return 1 - jaccardD(a, b);
}

@@ -1,5 +0,5 @@

var kulczynskiD = require('./../distances/kulczynski');
import kulczynskiD from '../distances/kulczynski';
module.exports = function kulczynski(a, b) {
return 1 / kulczynskiD(a, b);
};
export default function kulczynski(a, b) {
return 1 / kulczynskiD(a, b);
}

@@ -1,5 +0,5 @@

var motykaD = require('./../distances/motyka');
import motykaD from '../distances/motyka';
module.exports = function motyka(a, b) {
return 1 - motykaD(a,b);
};
export default function motyka(a, b) {
return 1 - motykaD(a, b);
}

@@ -1,18 +0,17 @@

'use strict';
import mean from 'ml-array-mean';
var stat=require('ml-stat').array;
var cosine=require('./cosine');
import cosine from './cosine';
module.exports = function pearson(a, b) {
var avgA=stat.mean(a);
var avgB=stat.mean(b);
export default function pearson(a, b) {
var avgA = mean(a);
var avgB = mean(b);
var newA=new Array(a.length);
var newB=new Array(b.length);
for (var i=0; i<newA.length; i++) {
newA[i]=a[i]-avgA;
newB[i]=b[i]-avgB;
}
var newA = new Array(a.length);
var newB = new Array(b.length);
for (var i = 0; i < newA.length; i++) {
newA[i] = a[i] - avgA;
newB[i] = b[i] - avgB;
}
return cosine(newA, newB);
};
return cosine(newA, newB);
}

@@ -1,5 +0,5 @@

var squaredChordD = require('./../distances/squaredChord');
import squaredChordD from '../distances/squaredChord';
module.exports = function squaredChord(a, b) {
return 1 - squaredChordD(a, b);
};
export default function squaredChord(a, b) {
return 1 - squaredChordD(a, b);
}

@@ -1,25 +0,25 @@

module.exports = function tanimoto(a, b, bitvector) {
if (bitvector) {
var inter = 0,
union = 0;
for (var j = 0; j < a.length; j++) {
inter += a[j] && b[j];
union += a[j] || b[j];
}
if (union === 0)
return 1;
return inter / union;
export default function tanimoto(a, b, bitvector) {
if (bitvector) {
var inter = 0;
var union = 0;
for (var j = 0; j < a.length; j++) {
inter += a[j] && b[j];
union += a[j] || b[j];
}
else {
var ii = a.length,
p = 0,
q = 0,
m = 0;
for (var i = 0; i < ii ; i++) {
p += a[i];
q += b[i];
m += Math.min(a[i],b[i]);
}
return 1 - (p + q - 2 * m) / (p + q - m);
if (union === 0) {
return 1;
}
};
return inter / union;
} else {
var ii = a.length;
var p = 0;
var q = 0;
var m = 0;
for (var i = 0; i < ii; i++) {
p += a[i];
q += b[i];
m += Math.min(a[i], b[i]);
}
return 1 - (p + q - 2 * m) / (p + q - m);
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc