[Pkg-javascript-commits] [science.js] 34/87: Add science.stats.erf.
bhuvan krishna
bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:55 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 cc1fcdd8ad6a8a60392bddbb7b45f8740b386c2b
Author: Jason Davies <jason at jasondavies.com>
Date: Sat Aug 27 21:55:21 2011 +0100
Add science.stats.erf.
---
Makefile | 1 +
science.stats.js | 22 ++++++++++++++++++++++
science.stats.min.js | 2 +-
src/stats/erf.js | 22 ++++++++++++++++++++++
test/stats/erf-test.js | 22 ++++++++++++++++++++++
5 files changed, 68 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 7ac88e3..c24678e 100644
--- a/Makefile
+++ b/Makefile
@@ -45,6 +45,7 @@ science.stats.js: \
src/stats/stats.js \
src/stats/bandwidth.js \
src/stats/distance.js \
+ src/stats/erf.js \
src/stats/kernel.js \
src/stats/kde.js \
src/stats/kmeans.js \
diff --git a/science.stats.js b/science.stats.js
index e6eccf3..c3213cf 100644
--- a/science.stats.js
+++ b/science.stats.js
@@ -73,6 +73,28 @@ science.stats.distance = {
return s / n;
}
};
+// Based on implementation in http://picomath.org/.
+science.stats.erf = function(x) {
+ var a1 = 0.254829592,
+ a2 = -0.284496736,
+ a3 = 1.421413741,
+ a4 = -1.453152027,
+ a5 = 1.061405429,
+ p = 0.3275911;
+
+ // Save the sign of x
+ var sign = x < 0 ? -1 : 1;
+ if (x < 0) {
+ sign = -1;
+ x = -x;
+ }
+
+ // A&S formula 7.1.26
+ var t = 1 / (1 + p * x);
+ return sign * (
+ 1 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1)
+ * t * Math.exp(-x * x));
+};
// See <http://en.wikipedia.org/wiki/Kernel_(statistics)>.
science.stats.kernel = {
uniform: function(u) {
diff --git a/science.stats.min.js b/science.stats.min.js
index d086072..c1aa426 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/erf.js b/src/stats/erf.js
new file mode 100644
index 0000000..9fd09e9
--- /dev/null
+++ b/src/stats/erf.js
@@ -0,0 +1,22 @@
+// Based on implementation in http://picomath.org/.
+science.stats.erf = function(x) {
+ var a1 = 0.254829592,
+ a2 = -0.284496736,
+ a3 = 1.421413741,
+ a4 = -1.453152027,
+ a5 = 1.061405429,
+ p = 0.3275911;
+
+ // Save the sign of x
+ var sign = x < 0 ? -1 : 1;
+ if (x < 0) {
+ sign = -1;
+ x = -x;
+ }
+
+ // A&S formula 7.1.26
+ var t = 1 / (1 + p * x);
+ return sign * (
+ 1 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1)
+ * t * Math.exp(-x * x));
+};
diff --git a/test/stats/erf-test.js b/test/stats/erf-test.js
new file mode 100644
index 0000000..a1fd583
--- /dev/null
+++ b/test/stats/erf-test.js
@@ -0,0 +1,22 @@
+require("../../science");
+require("../../science.stats");
+
+var vows = require("vows"),
+ assert = require("assert");
+
+var suite = vows.describe("science.stats.erf");
+
+suite.addBatch({
+ "erf": {
+ "simple": function() {
+ var erf = science.stats.erf;
+ assert.inDelta(erf(-3 ), -0.999977909503, 1e-6);
+ assert.inDelta(erf(-1 ), -0.842700792950, 1e-6);
+ assert.inDelta(erf( 0 ), 0, 1e-6);
+ assert.inDelta(erf( 0.5), 0.520499877813, 1e-6);
+ assert.inDelta(erf( 2.1), 0.997020533344, 1e-6);
+ }
+ }
+});
+
+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