[Pkg-javascript-commits] [science.js] 24/87: Add some more predefined distance functions.

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:54 UTC 2016


This is an automated email from the git hooks/post-receive script.

bhuvan-guest pushed a commit to branch master
in repository science.js.

commit 8fd5a2c3a15c01e047965168797ea4841d3bb582
Author: Jason Davies <jason at jasondavies.com>
Date:   Thu Aug 25 22:15:03 2011 +0100

    Add some more predefined distance functions.
---
 science.stats.js            | 34 ++++++++++++++++++++++++++++++++++
 science.stats.min.js        |  2 +-
 src/stats/distance.js       | 34 ++++++++++++++++++++++++++++++++++
 test/stats/distance-test.js | 28 ++++++++++++++++++++++++++++
 4 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/science.stats.js b/science.stats.js
index 3b6e00e..a1a8b59 100644
--- a/science.stats.js
+++ b/science.stats.js
@@ -37,6 +37,40 @@ science.stats.distance = {
         s = 0;
     while (++i < n) s += Math.abs(a[i] - b[i]);
     return s;
+  },
+  minkowski: function(p) {
+    return function(a, b) {
+      var n = a.length,
+          i = -1,
+          s = 0;
+      while (++i < n) s += Math.pow(Math.abs(a[i] - b[i]), p);
+      return Math.pow(s, 1 / p);
+    };
+  },
+  chebyshev: function(a, b) {
+    var n = a.length,
+        i = -1,
+        max = 0,
+        x;
+    while (++i < n) {
+      x = Math.abs(a[i] - b[i]);
+      if (x > max) max = x;
+    }
+    return max;
+  },
+  hamming: function(a, b) {
+    var n = a.length,
+        i = -1,
+        d = 0;
+    while (++i < n) if (a[i] !== b[i]) d++;
+    return d;
+  },
+  jaccard: function(a, b) {
+    var n = a.length,
+        i = -1,
+        s = 0;
+    while (++i < n) if (a[i] === b[i]) s++;
+    return s / n;
   }
 };
 // See <http://en.wikipedia.org/wiki/Kernel_(statistics)>.
diff --git a/science.stats.min.js b/science.stats.min.js
index 5f9687f..4cf4ddc 100644
--- a/science.stats.min.js
+++ b/science.stats.min.js
@@ -1 +1 @@
-(function(){function h(a,b){var c=b+1;while(c<a.length&&a[c]===0)c++;return c}function g(a,b,c,d){var e=d[0],f=d[1],g=h(b,f);if(g<a.length&&a[g]-a[c]<a[c]-a[e]){var i=h(b,e);d[0]=i,d[1]=g}}function f(a){return(a=1-a*a*a)*a*a}function e(a){var b=a.length,c=0;while(++c<b)if(a[c-1]>=a[c])return!1;return!0}function d(a){var b=a.length,c=-1;while(++c<b)if(!isFinite(a[c]))return!1;return!0}function c(a,b,c,d){var e=[],f=a+c,g=b.length,h=-1;while(++h<g)e[h]=(a*b[h]+c*d[h])/f;return e}function b [...]
\ No newline at end of file
+(function(){function h(a,b){var c=b+1;while(c<a.length&&a[c]===0)c++;return c}function g(a,b,c,d){var e=d[0],f=d[1],g=h(b,f);if(g<a.length&&a[g]-a[c]<a[c]-a[e]){var i=h(b,e);d[0]=i,d[1]=g}}function f(a){return(a=1-a*a*a)*a*a}function e(a){var b=a.length,c=0;while(++c<b)if(a[c-1]>=a[c])return!1;return!0}function d(a){var b=a.length,c=-1;while(++c<b)if(!isFinite(a[c]))return!1;return!0}function c(a,b,c,d){var e=[],f=a+c,g=b.length,h=-1;while(++h<g)e[h]=(a*b[h]+c*d[h])/f;return e}function b [...]
\ No newline at end of file
diff --git a/src/stats/distance.js b/src/stats/distance.js
index 963ff05..3b86a86 100644
--- a/src/stats/distance.js
+++ b/src/stats/distance.js
@@ -16,5 +16,39 @@ science.stats.distance = {
         s = 0;
     while (++i < n) s += Math.abs(a[i] - b[i]);
     return s;
+  },
+  minkowski: function(p) {
+    return function(a, b) {
+      var n = a.length,
+          i = -1,
+          s = 0;
+      while (++i < n) s += Math.pow(Math.abs(a[i] - b[i]), p);
+      return Math.pow(s, 1 / p);
+    };
+  },
+  chebyshev: function(a, b) {
+    var n = a.length,
+        i = -1,
+        max = 0,
+        x;
+    while (++i < n) {
+      x = Math.abs(a[i] - b[i]);
+      if (x > max) max = x;
+    }
+    return max;
+  },
+  hamming: function(a, b) {
+    var n = a.length,
+        i = -1,
+        d = 0;
+    while (++i < n) if (a[i] !== b[i]) d++;
+    return d;
+  },
+  jaccard: function(a, b) {
+    var n = a.length,
+        i = -1,
+        s = 0;
+    while (++i < n) if (a[i] === b[i]) s++;
+    return s / n;
   }
 };
diff --git a/test/stats/distance-test.js b/test/stats/distance-test.js
index 2f5f435..63c9a91 100644
--- a/test/stats/distance-test.js
+++ b/test/stats/distance-test.js
@@ -21,6 +21,34 @@ suite.addBatch({
       assert.equal(manhattan([0], [1]), 1);
       assert.equal(manhattan([0, 0], [1, 1]), 2);
       assert.equal(manhattan([0, 0, 0], [1, 1, 1]), 3);
+    },
+    "hamming": function() {
+      var hamming = science.stats.distance.hamming;
+      assert.equal(hamming([], []), 0);
+      assert.equal(hamming([0], [1]), 1);
+      assert.equal(hamming([0, 1], [1, 1]), 1);
+      assert.equal(hamming([3, 2, 1], [1, 2, 3]), 2);
+    },
+    "minkowski": function() {
+      var minkowski = science.stats.distance.minkowski(.5);
+      assert.equal(minkowski([], []), 0);
+      assert.equal(minkowski([0], [1]), 1);
+      assert.equal(minkowski([0, 1], [1, 1]), 1);
+      assert.equal(minkowski([1, 2, 3], [1, 2, 3]), 0);
+    },
+    "chebyshev": function() {
+      var chebyshev = science.stats.distance.chebyshev;
+      assert.equal(chebyshev([], []), 0);
+      assert.equal(chebyshev([0], [1]), 1);
+      assert.equal(chebyshev([0, 1], [1, 1]), 1);
+      assert.equal(chebyshev([3, 2, 1], [1, 2, 3]), 2);
+    },
+    "jaccard": function() {
+      var jaccard = science.stats.distance.jaccard;
+      assert.isTrue(isNaN(jaccard([], [])));
+      assert.equal(jaccard([0], [1]), 0);
+      assert.equal(jaccard([0, 1], [1, 1]), .5);
+      assert.equal(jaccard([3, 2, 1, 0], [1, 2, 3, 4]), .25);
     }
   }
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/science.js.git



More information about the Pkg-javascript-commits mailing list