gammasoft/utils · mathUtils.js
javascript logo
function(sample) {
        var x = parseInt(sample[propertyX], 10),
            y = parseInt(sample[propertyY], 10);

        this.length++;

        if(lastX) {
            if(x < lastX) {
                nextIsAvailable = false;
            }

            xAverage.add(Math.abs(x - lastX));
        }

        lastX = x;

        sumOfX += x;
        sumOfY += y;
        sumOfXY += x * y;
        sumOfXX += x * x;
        sumOfYY += y * y;
        divisor = ((this.length * sumOfXX) - (sumOfX * sumOfX));
    }
Similar code snippets
1.
gammasoft/utils · mathUtils.js
Match rating: 85.81% · See similar code snippets
javascript logo
function(propertyX, propertyY) {
    if(typeof propertyX === 'undefined') {
        propertyX = 'x';
    }

    if(typeof propertyY === 'undefined') {
        propertyY = 'y';
    }

    var sumOfX = 0,
        sumOfY = 0,
        sumOfXY = 0,
        sumOfXX = 0,
        sumOfYY = 0,
        divisor = 1,
        lastX = null,
        nextIsAvailable = true,
        xAverage = new Average();

    this.length = 0;

    this.add = function(sample) {
        var x = parseInt(sample[propertyX], 10),
            y = parseInt(sample[propertyY], 10);

        this.length++;

        if(lastX) {
            if(x < lastX) {
                nextIsAvailable = false;
            }

            xAverage.add(Math.abs(x - lastX));
        }

        lastX = x;

        sumOfX += x;
        sumOfY += y;
        sumOfXY += x * y;
        sumOfXX += x * x;
        sumOfYY += y * y;
        divisor = ((this.length * sumOfXX) - (sumOfX * sumOfX));
    };

    this.a = function() {
        return ((sumOfXX * sumOfY) - (sumOfXY * sumOfX)) / divisor;
    };

    this.b = function() {
        return ((this.length * sumOfXY) - (sumOfX * sumOfY)) / divisor;
    };

    this.correlation = function() {
        return ((this.length * sumOfXY) - (sumOfX * sumOfY)) / (Math.sqrt((this.length * sumOfXX) - (sumOfX * sumOfX)) * Math.sqrt((this.length * sumOfYY) - (sumOfY * sumOfY)));
    };

    this.calculate = function(x) {
        return this.a() + (this.b() * x);
    };

    this.next = function() {
        if(!nextIsAvailable) {
            return new Error('To use the the "next" function your must add samples in ascending order of X');
        }

        var nextX = lastX + xAverage.result;

        lastX = nextX;

        return {
            x: nextX,
            y: this.calculate(nextX)
        };
    };
}
2.
gammasoft/utils · mathUtils.js
Match rating: 76.64% · See similar code snippets
javascript logo
function(samples) {
    var length = samples.length,
        sumOfX = 0,
        sumOfY = 0,
        sumOfXY = 0,
        sumOfXX = 0;

    samples.forEach(function(sample) {
        sumOfX += sample.x;
        sumOfY += sample.y;
        sumOfXY += sample.x * sample.y;
        sumOfXX += sample.x * sample.x;
    });

    var divisor = ((length * sumOfXX) - (sumOfX * sumOfX)),
        a = ((sumOfXX * sumOfY) - (sumOfXY * sumOfX)) / divisor,
        b = ((length * sumOfXY) - (sumOfX * sumOfY)) / divisor;

    return {
        a: a,
        b: b,
        fn: function(x) {
            return a + (b * x);
        }
    };
}
3.
gammasoft/utils · mathUtils.js
Match rating: 66.46% · See similar code snippets
javascript logo
function(sample) {
        sumOfX += sample.x;
        sumOfY += sample.y;
        sumOfXY += sample.x * sample.y;
        sumOfXX += sample.x * sample.x;
    }
4.
cmap/morpheus.js · bivariate_functions.js
Match rating: 65.62% · See similar code snippets
javascript logo
function (listOne, listTwo) {
  var sumx = 0;
  var sumxx = 0;
  var sumy = 0;
  var sumyy = 0;
  var sumxy = 0;
  var N = 0;
  for (var i = 0, size = listOne.size(); i < size; i++) {
    var x = listOne.getValue(i);
    var y = listTwo.getValue(i);
    if (isNaN(x) || isNaN(y)) {
      continue;
    }
    sumx += x;
    sumxx += x * x;
    sumy += y;
    sumyy += y * y;
    sumxy += x * y;
    N++;
  }
  var numr = sumxy - (sumx * sumy / N);
  var denr = Math.sqrt((sumxx - (sumx * sumx / N))
    * (sumyy - (sumy * sumy / N)));
  return denr == 0 ? 1 : numr / denr;
}
5.
cmap/morpheus.js · bivariate_functions.js
Match rating: 64.54% · See similar code snippets
javascript logo
function (xVector, yVector) {
  var sumX = 0;
  var sumY = 0;
  var sumXX = 0;
  var sumXY = 0;
  var count = 0;
  for (var i = 0, size = xVector.size(); i < size; i++) {
    var x = xVector.getValue(i);
    var y = yVector.getValue(i);
    if (!isNaN(x) && !isNaN(y)) {
      sumX += x;
      sumY += y;
      sumXX += x * x;
      sumXY += x * y;
      count++;
    }
  }

  var m = ((count * sumXY) - (sumX * sumY)) /
    ((count * sumXX) - (sumX * sumX));
  var b = (sumY / count) - ((m * sumX) / count);
  return {
    m: m,
    b: b
  };
}
6.
lfirek/regression-trend · linear.js
Match rating: 63.21% · See similar code snippets
javascript logo
function generate(data) {
    let equation, n, sumX, sumY, sumXY, sumPowX, aveY, aveX, pointsArray;

    pointsArray = [];
    
    equation = {
        a: 0,
        b: 0
    };

    n = sumX = sumY = sumXY = sumPowX = aveY = aveX = 0;
    let i;

    for (i = 0; i < data.length; i++) {

        if (data[i].y !== null) {
            n++;
            sumX += data[i].x;
            sumY += data[i].y;
            sumXY += data[i].x * data[i].y;
            sumPowX += Math.pow(data[i].x,2);
        }
    }
    aveY = sumY / n;
    aveX = sumX / n;
    equation.b = (n * sumXY - sumX * sumY) / (n * sumPowX - sumX * sumX);
    equation.a = aveY - equation.b * aveX;

    for (i = 0; i < data.length; i++) {
        pointsArray.push(
            {
                x: data[i].x,
                y: trend(equation, data[i].y)
            }
        );
    }

    return {
        equation: equation,
        points: pointsArray,
        pattern: "y = " + equation.a + " + " + equation.b + " * x"
    };
}
7.
skerit/protoblast · math.js
Match rating: 61.61% · See similar code snippets
javascript logo
function pearson(arrX, arrY) {

		var sumXS = 0,
		    sumYS = 0,
		    sumX  = 0,
		    sumY  = 0,
		    sumP  = 0,
		    num,
		    den,
		    min,
		    x,
		    y,
		    i;

		// Use the least number of available items
		min = Math.min(arrX.length, arrY.length);

		for (i = 0; i < min; i++) {
			x = arrX[i];
			y = arrY[i];

			sumX += x;
			sumY += y;
			sumXS += Math.pow(x, 2);
			sumYS += Math.pow(y, 2);
			sumP += x * y;
		}

		num = sumP - (sumX * sumY / min);
		den = Math.sqrt((sumXS - Math.pow(sumX, 2) / min) * (sumYS - Math.pow(sumY, 2) / min));

		if (den == 0) return 0;

		return num/den;
	}
8.
KoryNunn/gestures · linearRegresion.js
Match rating: 61.23% · See similar code snippets
javascript logo
function linearRegresion(points) {
    var sumX = 0,
        sumY = 0,
        sumXbyY = 0,
        sumXbyX = 0,
        x = 0,
        y = 0,
        numberOfPoints = points.length,
        results = [],
        m,
        b;

    if (numberOfPoints === 0) {
        return [];
    }

    for (var i = 0; i < numberOfPoints; i++) {
        x = points[i].x;
        y = points[i].y;
        sumX += x;
        sumY += y;
        sumXbyX += x*x;
        sumXbyY += x*y;
    }

    m = (numberOfPoints * sumXbyY - sumX * sumY) / (numberOfPoints * sumXbyX - sumX * sumX);
    b = (sumY / numberOfPoints) - (m * sumX) / numberOfPoints;


    for (var i = 0; i < numberOfPoints; i++) {
        results.push({
            x: points[i].x,
            y: points[i].x * m + b
        });
    }

    return results;
}
9.
filerjs/filer · simple_statistics.js
Match rating: 59.61% · See similar code snippets
javascript logo
function() {
            var m, b;

            // Store data length in a local variable to reduce
            // repeated object property lookups
            var data_length = data.length;

            //if there's only one point, arbitrarily choose a slope of 0
            //and a y-intercept of whatever the y of the initial point is
            if (data_length === 1) {
                m = 0;
                b = data[0][1];
            } else {
                // Initialize our sums and scope the `m` and `b`
                // variables that define the line.
                var sum_x = 0, sum_y = 0,
                    sum_xx = 0, sum_xy = 0;

                // Use local variables to grab point values
                // with minimal object property lookups
                var point, x, y;

                // Gather the sum of all x values, the sum of all
                // y values, and the sum of x^2 and (x*y) for each
                // value.
                //
                // In math notation, these would be SS_x, SS_y, SS_xx, and SS_xy
                for (var i = 0; i < data_length; i++) {
                    point = data[i];
                    x = point[0];
                    y = point[1];

                    sum_x += x;
                    sum_y += y;

                    sum_xx += x * x;
                    sum_xy += x * y;
                }

                // `m` is the slope of the regression line
                m = ((data_length * sum_xy) - (sum_x * sum_y)) /
                    ((data_length * sum_xx) - (sum_x * sum_x));

                // `b` is the y-intercept of the line.
                b = (sum_y / data_length) - ((m * sum_x) / data_length);
            }

            // Return both values as an object.
            return { m: m, b: b };
        }
10.
taataa/tapspace · Vector.js
Match rating: 55.53% · See similar code snippets
javascript logo
function (vlist) {
  // Average of the listed Vectors.
  //
  var i
  var len = vlist.length
  var sumx = 0
  var sumy = 0
  if (len > 0) {
    for (i = 0; i < len; i += 1) {
      sumx += vlist[i].x
      sumy += vlist[i].y
    }
    return new Vector(sumx / len, sumy / len)
  }
  throw new Error('Cannot compute mean for an empty list of vectors.')
}