[Pkg-javascript-commits] [science.js] 73/87: Rewrite science.stats.mode.

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:12:04 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 027301059df79be8d90506ec800c39cee509093e
Author: Jason Davies <jason at jasondavies.com>
Date:   Mon Dec 17 11:03:41 2012 +0000

    Rewrite science.stats.mode.
    
    This prepares support for returning multiple modes in a future version.
    The API is backwards-compatible for now.
    
    Fixes #8.
---
 science.v1.js           | 29 ++++++++++++-----------------
 science.v1.min.js       |  2 +-
 src/stats/mode.js       | 29 ++++++++++++-----------------
 test/stats/mode-test.js |  2 ++
 4 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/science.v1.js b/science.v1.js
index 83d4226..8c072d2 100644
--- a/science.v1.js
+++ b/science.v1.js
@@ -1629,27 +1629,22 @@ science.stats.median = function(x) {
   return science.stats.quantiles(x, [.5])[0];
 };
 science.stats.mode = function(x) {
-  if ((n = x.length) === 1) return x[0];
-  x = x.slice().sort(science.ascending);
-  var mode,
-      n,
-      i = -1,
-      l = i,
-      last,
+  var counts = {},
+      mode = [],
       max = 0,
-      tmp,
-      v;
+      n = x.length,
+      i = -1,
+      d,
+      k;
   while (++i < n) {
-    if ((v = x[i]) !== last) {
-      if ((tmp = i - l) > max) {
-        max = tmp;
-        mode = last;
-      }
-      last = v;
-      l = i;
+    k = counts.hasOwnProperty(d = x[i]) ? ++counts[d] : counts[d] = 1;
+    if (k === max) mode.push(d);
+    else if (k > max) {
+      max = k;
+      mode = [d];
     }
   }
-  return mode;
+  return mode.length === 1 ? mode[0] : void 0;
 };
 // Uses R's quantile algorithm type=7.
 science.stats.quantiles = function(d, quantiles) {
diff --git a/science.v1.min.js b/science.v1.min.js
index 728ee6b..e7744fe 100644
--- a/science.v1.min.js
+++ b/science.v1.min.js
@@ -1 +1 @@
-(function(a){(function(a){science={version:"1.9.1"},science.ascending=function(a,b){return a-b},science.EULER=.5772156649015329,science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.quadratic=function(){function b(b,c,d){var e=c*c-4*b*d;return e>0?(e=Math.s [...]
\ No newline at end of file
+(function(a){(function(a){science={version:"1.9.1"},science.ascending=function(a,b){return a-b},science.EULER=.5772156649015329,science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.quadratic=function(){function b(b,c,d){var e=c*c-4*b*d;return e>0?(e=Math.s [...]
\ No newline at end of file
diff --git a/src/stats/mode.js b/src/stats/mode.js
index 97677f9..6a37711 100644
--- a/src/stats/mode.js
+++ b/src/stats/mode.js
@@ -1,23 +1,18 @@
 science.stats.mode = function(x) {
-  if ((n = x.length) === 1) return x[0];
-  x = x.slice().sort(science.ascending);
-  var mode,
-      n,
-      i = -1,
-      l = i,
-      last,
+  var counts = {},
+      mode = [],
       max = 0,
-      tmp,
-      v;
+      n = x.length,
+      i = -1,
+      d,
+      k;
   while (++i < n) {
-    if ((v = x[i]) !== last) {
-      if ((tmp = i - l) > max) {
-        max = tmp;
-        mode = last;
-      }
-      last = v;
-      l = i;
+    k = counts.hasOwnProperty(d = x[i]) ? ++counts[d] : counts[d] = 1;
+    if (k === max) mode.push(d);
+    else if (k > max) {
+      max = k;
+      mode = [d];
     }
   }
-  return mode;
+  return mode.length === 1 ? mode[0] : void 0;
 };
diff --git a/test/stats/mode-test.js b/test/stats/mode-test.js
index 366187d..ca0da17 100644
--- a/test/stats/mode-test.js
+++ b/test/stats/mode-test.js
@@ -10,7 +10,9 @@ suite.addBatch({
   "mode": {
     "returns correct modes": function() {
       assert.equal(science.stats.mode([1, 2, 2, 3, 4, 7, 9]), 2);
+      assert.equal(science.stats.mode([1, 2, 2, 3, 4, 7, 9].reverse()), 2);
       assert.equal(science.stats.mode([1]), 1);
+      assert.equal(science.stats.mode([1, 1, 1]), 1);
       assert.isUndefined(science.stats.mode([1, 2, 3, 4, 5]));
       assert.isUndefined(science.stats.mode([]));
     }

-- 
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