cmap/morpheus.js · bivariate_functions.js
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
  };
}
Similar code snippets
1.
cmap/morpheus.js · bivariate_functions.js
Match rating: 76.72% · 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;
}
2.
iceonepiece/jonfon · cosine.js
Match rating: 70.6% · See similar code snippets
javascript logo
function dot(x, y){
  var sum = 0;
  for( var i = 0; i < x.length; i++ ){
    sum += x[i] * y[i];
  }
  return sum;
}
3.
cmap/morpheus.js · bivariate_functions.js
Match rating: 69.58% · See similar code snippets
javascript logo
function (listOne, listTwo) {
  var sumX2 = 0;
  var sumY2 = 0;
  var sumXY = 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;
    }
    sumX2 += x * x;
    sumY2 += y * y;
    sumXY += x * y;
  }
  return (sumXY / Math.sqrt(sumX2 * sumY2));
}
4.
lfirek/regression-trend · linear.js
Match rating: 69.1% · 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"
    };
}
5.
skerit/protoblast · math.js
Match rating: 67.12% · 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;
	}
6.
filerjs/filer · simple_statistics.js
Match rating: 66.48% · See similar code snippets
javascript logo
function linear_regression() {
        var linreg = {},
            data = [];

        // Assign data to the model. Data is assumed to be an array.
        linreg.data = function(x) {
            if (!arguments.length) return data;
            data = x.slice();
            return linreg;
        };

        // Calculate the slope and y-intercept of the regression line
        // by calculating the least sum of squares
        linreg.mb = 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 };
        };

        // a shortcut for simply getting the slope of the regression line
        linreg.m = function() {
            return linreg.mb().m;
        };

        // a shortcut for simply getting the y-intercept of the regression
        // line.
        linreg.b = function() {
            return linreg.mb().b;
        };

        // ## Fitting The Regression Line
        //
        // This is called after `.data()` and returns the
        // equation `y = f(x)` which gives the position
        // of the regression line at each point in `x`.
        linreg.line = function() {

            // Get the slope, `m`, and y-intercept, `b`, of the line.
            var mb = linreg.mb(),
                m = mb.m,
                b = mb.b;

            // Return a function that computes a `y` value for each
            // x value it is given, based on the values of `b` and `a`
            // that we just computed.
            return function(x) {
                return b + (m * x);
            };
        };

        return linreg;
    }
7.
jprichardson/least-squares · least-squares.js
Match rating: 66.25% · See similar code snippets
javascript logo
function(){

  //I should make this a UMD sometime
if (typeof module != 'undefined' && typeof module.exports != 'undefined') {
  module.exports = LeastSquares
} else {
  window.lsq = LeastSquares
}

function LeastSquares (X, Y, computeError, ret) {
  if (typeof computeError == 'object') {
    ret = computeError
    computeError = false
  }

  if (typeof ret == 'undefined') ret = {}

  var sumX = 0
  var sumY = 0
  var sumXY = 0
  var sumXSq = 0
  var N = X.length

  for(var i = 0; i < N; ++i) {
    sumX += X[i]
    sumY += Y[i]
    sumXY += X[i] * Y[i]
    sumXSq += X[i] * X[i]
  }

  ret.m = ((sumXY - sumX * sumY / N) ) / (sumXSq - sumX * sumX / N)
  ret.b = sumY / N - ret.m * sumX / N

  if (computeError) {
    var varSum = 0
    for (var j = 0; j < N; ++j) {
      varSum += (Y[j] - ret.b - ret.m*X[j]) * (Y[j] - ret.b - ret.m*X[j])
    }

    var delta = N * sumXSq - sumX*sumX
    var vari = 1.0 / (N - 2.0) * varSum

    ret.bErr = Math.sqrt(vari / delta * sumXSq)
    ret.mErr = Math.sqrt(N / delta * vari)
  }

  return function(x) {
    return ret.m * x + ret.b
  }
}

}
8.
BorisChumichev/everpolate · linearRegression.js
Match rating: 66.23% · See similar code snippets
javascript logo
function linearRegression(functionValuesX, functionValuesY){
  var regression = {}
    , x = functionValuesX
    , y = functionValuesY
    , n = y.length
    , sum_x = 0
    , sum_y = 0
    , sum_xy = 0
    , sum_xx = 0
    , sum_yy = 0

  for (var i = 0; i < y.length; i++) {
    sum_x += x[i]
    sum_y += y[i]
    sum_xy += (x[i]*y[i])
    sum_xx += (x[i]*x[i])
    sum_yy += (y[i]*y[i])
  }

  regression.slope = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x)
  regression.intercept = (sum_y - regression.slope * sum_x)/n
  regression.rSquared = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2)
  regression.evaluate = function (pointsToEvaluate) {
    var x = help.makeItArrayIfItsNot(pointsToEvaluate)
      , result = []
      , that = this
    x.forEach(function (point) {
      result.push(that.slope*point + that.intercept)
    })
    return result
  }

  return regression
}
9.
filerjs/filer · simple_statistics.js
Match rating: 66.13% · 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.
gammasoft/utils · mathUtils.js
Match rating: 64.54% · See similar code snippets
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));
    }