dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #16021
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5958: Impl a partial js port of commons math SimpleRegression class.
------------------------------------------------------------
revno: 5958
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-02-14 19:00:17 +0100
message:
Impl a partial js port of commons math SimpleRegression class.
added:
dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/simpleRegression.js
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/simpleRegression.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/simpleRegression.js 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/simpleRegression.js 2012-02-14 18:00:17 +0000
@@ -0,0 +1,61 @@
+
+/**
+ * Partial js port of commons math SimpleRegression class.
+ */
+
+var simpleRegression = new SimpleRegression();
+
+function SimpleRegression()
+{
+ var sumX = 0; // Sum of x values
+ var sumY = 0; // Sum of y values
+ var sumXX = 0; // Total variation in x
+ var sumXY = 0; // Sum of products
+ var n = 0; // Number of observations
+ var xbar = 0; // Mean of accumulated x values, used in updating formulas
+ var ybar = 0; // Mean of accumulated y values, used in updating formulas
+
+ this.addData = function( x, y )
+ {
+ if ( n == 0 )
+ {
+ xbar = x;
+ ybar = y;
+ }
+ else
+ {
+ var dx = x - xbar;
+ var dy = y - ybar;
+ sumXX += dx * dx * n / ( n + 1 );
+ sumXY += dx * dy * n / ( n + 1 );
+ xbar += dx / ( n + 1 );
+ ybar += dy / ( n + 1 );
+ }
+
+ sumX += x;
+ sumY += y;
+ n++;
+ };
+
+ this.predict = function( x )
+ {
+ var b1 = simpleRegression.getSlope();
+
+ return simpleRegression.getIntercept( b1 ) + b1 * x;
+ };
+
+ this.getSlope = function()
+ {
+ if ( n < 2 )
+ {
+ return Number.NaN;
+ }
+
+ return sumXY / sumXX;
+ };
+
+ this.getIntercept = function( slope )
+ {
+ return ( sumY - slope * sumX ) / n;
+ };
+}