[Pkg-javascript-commits] [science.js] 37/87: Add science.stats.distribution!

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:56 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 84b39aa13a663a552f3fa3bb6dab46ad838e1461
Author: Jason Davies <jason at jasondavies.com>
Date:   Sat Aug 27 23:01:08 2011 +0100

    Add science.stats.distribution!
---
 Makefile                                 |  2 ++
 science.stats.js                         | 53 ++++++++++++++++++++++++++++++++
 science.stats.min.js                     |  2 +-
 src/stats/distribution.js                |  2 ++
 src/stats/distribution/gaussian.js       | 51 ++++++++++++++++++++++++++++++
 test/stats/distribution/gaussian-test.js | 17 ++++++++++
 6 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 7678670..5670ecf 100644
--- a/Makefile
+++ b/Makefile
@@ -59,6 +59,8 @@ science.stats.js: \
 	src/stats/mode.js \
 	src/stats/quantiles.js \
 	src/stats/variance.js \
+	src/stats/distribution.js \
+	src/stats/distribution/gaussian.js \
 	src/end.js
 
 test: all
diff --git a/science.stats.js b/science.stats.js
index efc66af..e0f3c35 100644
--- a/science.stats.js
+++ b/science.stats.js
@@ -702,4 +702,57 @@ science.stats.variance = function(x) {
   }
   return s / (n - 1);
 };
+science.stats.distribution = {
+};
+// From http://www.colingodsey.com/javascript-gaussian-random-number-generator/
+// Uses the Box-Muller Transform.
+science.stats.distribution.gaussian = function() {
+  var random = Math.random,
+      mean = 0,
+      sigma = 1;
+
+  function gaussian() {
+    var x1, x2, rad, y1;
+
+    do {
+      x1 = 2 * random() - 1;
+      x2 = 2 * random() - 1;
+      rad = x1 * x1 + x2 * x2;
+    } while (rad >= 1 || rad === 0);
+
+    return x1 * Math.sqrt(-2 * Math.log(rad) / rad);
+  }
+
+  gaussian.pdf = function(x) {
+    x = (x - mu) / sigma;
+    return science_stats_distribution_gaussianConstant * Math.exp(-.5 * x * x) / sigma;
+  };
+
+  gaussian.cdf = function(x) {
+    x = (x - mu) / sigma;
+    return .5 * (1 + science.stats.erf(x / Math.SQRT2));
+  };
+
+  gaussian.mean = function(x) {
+    if (!arguments.length) return mean;
+    mean = +x;
+    return gaussian;
+  };
+
+  gaussian.sigma = function(x) {
+    if (!arguments.length) return sigma;
+    sigma = +x;
+    return gaussian;
+  };
+
+  gaussian.random = function(x) {
+    if (!arguments.length) return random;
+    random = x;
+    return gaussian;
+  };
+
+  return gaussian;
+};
+
+science_stats_distribution_gaussianConstant = 1 / Math.sqrt(2 * Math.PI);
 })()
\ No newline at end of file
diff --git a/science.stats.min.js b/science.stats.min.js
index 0412d8c..8b5434e 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/distribution.js b/src/stats/distribution.js
new file mode 100644
index 0000000..5afc424
--- /dev/null
+++ b/src/stats/distribution.js
@@ -0,0 +1,2 @@
+science.stats.distribution = {
+};
diff --git a/src/stats/distribution/gaussian.js b/src/stats/distribution/gaussian.js
new file mode 100644
index 0000000..2059025
--- /dev/null
+++ b/src/stats/distribution/gaussian.js
@@ -0,0 +1,51 @@
+// From http://www.colingodsey.com/javascript-gaussian-random-number-generator/
+// Uses the Box-Muller Transform.
+science.stats.distribution.gaussian = function() {
+  var random = Math.random,
+      mean = 0,
+      sigma = 1;
+
+  function gaussian() {
+    var x1, x2, rad, y1;
+
+    do {
+      x1 = 2 * random() - 1;
+      x2 = 2 * random() - 1;
+      rad = x1 * x1 + x2 * x2;
+    } while (rad >= 1 || rad === 0);
+
+    return x1 * Math.sqrt(-2 * Math.log(rad) / rad);
+  }
+
+  gaussian.pdf = function(x) {
+    x = (x - mu) / sigma;
+    return science_stats_distribution_gaussianConstant * Math.exp(-.5 * x * x) / sigma;
+  };
+
+  gaussian.cdf = function(x) {
+    x = (x - mu) / sigma;
+    return .5 * (1 + science.stats.erf(x / Math.SQRT2));
+  };
+
+  gaussian.mean = function(x) {
+    if (!arguments.length) return mean;
+    mean = +x;
+    return gaussian;
+  };
+
+  gaussian.sigma = function(x) {
+    if (!arguments.length) return sigma;
+    sigma = +x;
+    return gaussian;
+  };
+
+  gaussian.random = function(x) {
+    if (!arguments.length) return random;
+    random = x;
+    return gaussian;
+  };
+
+  return gaussian;
+};
+
+science_stats_distribution_gaussianConstant = 1 / Math.sqrt(2 * Math.PI);
diff --git a/test/stats/distribution/gaussian-test.js b/test/stats/distribution/gaussian-test.js
new file mode 100644
index 0000000..96ef965
--- /dev/null
+++ b/test/stats/distribution/gaussian-test.js
@@ -0,0 +1,17 @@
+require("../../../science");
+require("../../../science.stats");
+
+var vows = require("vows"),
+    assert = require("assert");
+
+var suite = vows.describe("science.stats.distribution.gaussian");
+
+suite.addBatch({
+  "gaussian": {
+    "simple": function() {
+      var gaussian = science.stats.distribution.gaussian;
+    }
+  }
+});
+
+suite.export(module);

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