deeplearning4j/deeplearning4j · MathUtils.java
java logo
public static double[] weightsFor(List<Double> vector) {
        /* split coordinate system */
        List<double[]> coords = coordSplit(vector);
        /* x vals */
        double[] x = coords.get(0);
        /* y vals */
        double[] y = coords.get(1);


        double meanX = sum(x) / x.length;
        double meanY = sum(y) / y.length;

        double sumOfMeanDifferences = sumOfMeanDifferences(x, y);
        double xDifferenceOfMean = sumOfMeanDifferencesOnePoint(x);

        double w_1 = sumOfMeanDifferences / xDifferenceOfMean;

        double w_0 = meanY - (w_1) * meanX;

        //double w_1=(n*sumOfProducts(x,y) - sum(x) * sum(y))/(n*sumOfSquares(x) - Math.pow(sum(x),2));

        //	double w_0=(sum(y) - (w_1 * sum(x)))/n;

        double[] ret = new double[vector.size()];
        ret[0] = w_0;
        ret[1] = w_1;

        return ret;
    }
Similar code snippets
1.
deeplearning4j/deeplearning4j · MathUtils.java
Match rating: 64.69% · See similar code snippets
java logo
public static double sumOfMeanDifferencesOnePoint(double[] vector) {
        double mean = sum(vector) / vector.length;
        double ret = 0;
        for (int i = 0; i < vector.length; i++) {
            double vec1Diff = Math.pow(vector[i] - mean, 2);
            ret += vec1Diff;
        }
        return ret;
    }
2.
Harium/keel · Distance.java
Match rating: 63.34% · See similar code snippets
java logo
private static double Covariance(double[] x, double[] y, double meanX, double meanY) {
        double result = 0;
        for (int i = 0; i < x.length; i++) {
            result += (x[i] - meanX) * (y[i] - meanY);
        }

        return result / (double) (x.length);
    }
public static void doubleCenterSymmetric(double[][] m) {
    final int size = m.length;
    // Storage for mean values - initially all 0.
    double[] means = new double[size];
    for(int x = 0; x < m.length; x++) {
      final double[] rowx = m[x];
      // We already added "x" values in previous iterations.
      // Fake-add 0: mean + (0 - mean) / (x + 1)
      double rmean = means[x] - means[x] / (x + 1);
      for(int y = x + 1; y < rowx.length; y++) {
        final double nv = rowx[y];
        final double dx = nv - rmean, dy = nv - means[y];
        // For x < y, this is the yth entry.
        rmean += dx / (y + 1);
        // For y > x, this is the xth entry
        means[y] += dy / (x + 1);
      }
      means[x] = rmean;
    }
    // Compute total mean by averaging column means.
    double mean = means[0];
    for(int x = 1; x < size; x++) {
      double dm = means[x] - mean;
      mean += dm / (x + 1);
    }
    // Row and column center; also make symmetric.
    for(int x = 0; x < size; x++) {
      m[x][x] = -2. * means[x] + mean;
      for(int y = x + 1; y < size; y++) {
        final double nv = m[x][y] - means[x] - means[y] + mean;
        m[x][y] = nv;
        m[y][x] = nv;
      }
    }
  }
4.
haifengl/smile · Math.java
Match rating: 57.21% · See similar code snippets
java logo
public static double[] rowMeans(double[][] data) {
        double[] x = new double[data.length];

        for (int i = 0; i < x.length; i++) {
            x[i] = mean(data[i]);
        }

        return x;
    }
5.
TheHortonMachine/hortonmachine · Stats.java
Match rating: 57.19% · See similar code snippets
java logo
private static double[] calcLinReg(double[] xData, double[] yData) {
        double sumX = 0;
        double sumY = 0;
        double prod = 0;
        int nstat = xData.length;
        double[] regCoef = new double[3]; //(intercept, gradient, r2)

        double meanYValue = mean(yData);
        double meanXValue = mean(xData);

        //calculating regression coefficients
        for (int i = 0; i < nstat; i++) {
            sumX += (xData[i] - meanXValue) * (xData[i] - meanXValue);
            sumY += (yData[i] - meanYValue) * (yData[i] - meanYValue);
            prod += (xData[i] - meanXValue) * (yData[i] - meanYValue);
        }
        if (sumX > 0 && sumY > 0) {
            regCoef[1] = prod / sumX;  //gradient
            regCoef[0] = meanYValue - regCoef[1] * meanXValue; //intercept
            regCoef[2] = Math.pow((prod / Math.sqrt(sumX * sumY)), 2); //r2
        }
        return regCoef;
    }
6.
mgormley/optimize · HeterogeneousL2.java
Match rating: 55.87% · See similar code snippets
java logo
@Override
	public double getValue(IntDoubleVector point) {
		int n = means.length;
		double sum = 0d;
		for(int i=0; i<n; i++) {
			double d = point.get(i) - means[i];
			sum += d * d / (2d * variances[i]);
		}
		return -sum;
	}
7.
deeplearning4j/deeplearning4j · MathUtils.java
Match rating: 54.84% · See similar code snippets
java logo
public static double squaredLoss(double[] x, double[] y, double w_0, double w_1) {
        double sum = 0;
        for (int j = 0; j < x.length; j++) {
            sum += Math.pow((y[j] - (w_1 * x[j] + w_0)), 2);
        }
        return sum;
    }
8.
jMetal/jMetal · Ebes.java
Match rating: 54.78% · See similar code snippets
java logo
public double FunctionsMahalanobis_Distance_With_Variance(int hi) {
    // Mahalanobis Distance With Variance for estimated value respect to estimated data

    double MD = 0.0; // mahalanobis distance
    double[] MDi = new double [geometryCheck_.length]; // mahalanobis distance

    for (int i = 0; i < geometryCheck_.length; i++)
    {
      int N = geometryCheck_[i].length;
      double[] distY = new double[N];
      double[] distZ = new double[N];
      double sumY = 0.0; //
      double sumZ = 0.0; //
      double sumYxY = 0.0; //
      double sumZxZ = 0.0; //
      double sumYxZ = 0.0; //
      double meanY = 0.0; // means Y distance
      double meanZ = 0.0; // means Z distance
      double S2Y = 0.0; // variance Y distance
      double S2Z = 0.0; // variance Z distance
      double SY = 0.0; // variance Y distance
      double SZ = 0.0; // variance Z distance
      //double CS2 = 0.0; // covariance
      double r = 0.0; // Pearson correlation

      for (int j = 0; j < geometryCheck_[i].length; j++) {

        distY[j] = Groups_[geometryCheck_[i][j]][Y_];
        distZ[j] = Groups_[geometryCheck_[i][j]][Z_];

        sumY += distY[j];
        sumZ += distZ[j];
        sumYxY += distY[j]*distY[j];
        sumZxZ += distZ[j]*distZ[j];
        sumYxZ += distY[j]*distZ[j];

        meanY += distY[j];
        meanZ += distZ[j];

      }
      //mean of the observed data and values estimated
      meanY /= N;
      meanZ /= N;

      // Pearson’s correlation coefficient
      r = (N*sumYxZ-sumY*sumZ)/(Math.sqrt((N*sumYxY-Math.pow(sumY,2.0))*(N*sumZxZ-Math.pow(sumZ,2.0))));


      // variance
      for (int k = 0; k < N; k++) {
        S2Y += Math.pow((distY[k] - meanY), 2.0);
        S2Z += Math.pow((distZ[k] - meanZ), 2.0);
      }

      S2Y /= (N-1);
      S2Z /= (N-1);
      SY = Math.sqrt(S2Y);
      SZ = Math.sqrt(S2Z);
      //CS2 /= (N-1);

      // Mahalanobis distance
      for (int k = 1; k < N; k++) {
        MDi[i] += Math.pow((0-distY[k]), 2.0) / S2Y + Math.pow((0-distZ[k]), 2.0) / S2Z - 2.0*r*(0-distY[k])*(0-distZ[k])/(SY*SZ);
      }

      MDi[i] = Math.sqrt(1/(1-Math.pow(r,2.0))*MDi[i]);
    }

    for (int i = 0; i < geometryCheck_.length; i++) {
      MD += MDi[i];
    }

    return MD;
  }
9.
elki-project/elki · AbstractKMeans.java
Match rating: 54.29% · See similar code snippets
java logo
private static double[][] sparseMeans(List<? extends DBIDs> clusters, double[][] means, Relation<? extends SparseNumberVector> relation) {
    final int k = means.length;
    double[][] newMeans = new double[k][];
    for(int i = 0; i < k; i++) {
      DBIDs list = clusters.get(i);
      if(list.isEmpty()) {
        // Keep degenerated means as-is for now.
        newMeans[i] = means[i];
        continue;
      }
      DBIDIter iter = list.iter();
      // Initialize with first.
      double[] mean = relation.get(iter).toArray();
      // Update with remaining instances
      for(iter.advance(); iter.valid(); iter.advance()) {
        SparseNumberVector vec = relation.get(iter);
        for(int j = vec.iter(); vec.iterValid(j); j = vec.iterAdvance(j)) {
          mean[vec.iterDim(j)] += vec.iterDoubleValue(j);
        }
      }
      newMeans[i] = timesEquals(mean, 1.0 / list.size());
    }
    return newMeans;
  }
10.
lessthanoptimal/BoofCV · LowLevelMultiViewOps.java
Match rating: 53.66% · See similar code snippets
java logo
public static void computeNormalizationLL(List<List<Point2D_F64>> points, NormalizationPoint2D normalize )
	{
		double meanX = 0;
		double meanY = 0;

		int count = 0;

		for (int i = 0; i < points.size(); i++) {
			List<Point2D_F64> l = points.get(i);
			for (int j = 0; j < l.size(); j++) {
				Point2D_F64 p = l.get(j);
				meanX += p.x;
				meanY += p.y;
			}
			count += l.size();
		}

		meanX /= count;
		meanY /= count;

		double stdX = 0;
		double stdY = 0;

		for (int i = 0; i < points.size(); i++) {
			List<Point2D_F64> l = points.get(i);
			for (int j = 0; j < l.size(); j++) {
				Point2D_F64 p = l.get(j);
				double dx = p.x - meanX;
				double dy = p.y - meanY;
				stdX += dx*dx;
				stdY += dy*dy;
			}
		}

		normalize.meanX = meanX;
		normalize.meanY = meanY;

		normalize.stdX = Math.sqrt(stdX/count);
		normalize.stdY = Math.sqrt(stdY/count);
	}