@Override
  public <A> double[] dependence(NumberArrayAdapter<?, A> adapter, List<? extends A> data) {
    final int dims = data.size();
    final int len = size(adapter, data);
    double[] means = new double[dims];
    // Two passes - often faster due to the lower numerical cost
    // And accurate, don't use sum-of-squares.
    for(int j = 0; j < dims; j++) {
      double m = 0.;
      A da = data.get(j);
      for(int i = 0; i < len; i++) {
        m += adapter.getDouble(da, i);
      }
      means[j] = m / len;
    }
    // Build the covariance matrix, lower triangular half
    double[] vst = new double[dims];
    double[] cov = new double[(dims * (dims - 1)) >> 1];
    double[] buf = new double[dims];
    for(int i = 0; i < len; i++) {
      for(int j = 0; j < dims; j++) {
        buf[j] = adapter.getDouble(data.get(j), i) - means[j];
      }
      for(int y = 0, c = 0; y < dims; y++) {
        for(int x = 0; x < y; x++) {
          cov[c++] += buf[x] * buf[y];
        }
        vst[y] += buf[y] * buf[y];
      }
    }
    // Compute standard deviations (times sqrt(len)!):
    for(int y = 0; y < dims; y++) {
      if(vst[y] == 0.) {
        LOG.warning("Correlation is not well defined for constant attributes.");
      }
      vst[y] = FastMath.sqrt(vst[y]);
    }
    for(int y = 1, c = 0; y < dims; y++) {
      for(int x = 0; x < y; x++) {
        // We don't need to divide by sqrt(len), because it will cancel out with
        // the division we skipped just above.
        cov[c] = cov[c] / (vst[x] * vst[y]);
        c++;
      }
    }
    return cov;
  }
Similar code snippets
1.
java logo
@Override
  public <A> double[] dependence(NumberArrayAdapter<?, A> adapter, List<? extends A> data) {
    final int dims = data.size();
    final int len = size(adapter, data);
    double[][] dMatrix = new double[dims][];
    for(int i = 0; i < dims; i++) {
      dMatrix[i] = computeDistances(adapter, data.get(i));
    }
    double[] dVar = new double[dims];
    for(int i = 0; i < dims; i++) {
      dVar[i] = computeDCovar(dMatrix[i], dMatrix[i], len);
    }
    double[] dCor = new double[(dims * (dims - 1)) >> 1];
    for(int y = 1, c = 0; y < dims; y++) {
      for(int x = 0; x < y; x++) {
        if(!(dVar[x] * dVar[y] > 0.)) {
          dCor[c++] = 0.;
          continue;
        }
        double dCovar = computeDCovar(dMatrix[x], dMatrix[y], len);
        dCor[c++] = FastMath.sqrt(dCovar / FastMath.sqrt(dVar[x] * dVar[y]));
      }
    }
    return dCor;
  }
2.
haifengl/smile · Math.java
Match rating: 56.61% · See similar code snippets
java logo
public static double[] colMeans(double[][] data) {
        double[] x = data[0].clone();

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

        scale(1.0 / data.length, x);

        return x;
    }
3.
haifengl/smile · Math.java
Match rating: 55.42% · See similar code snippets
java logo
public static double[] colSums(double[][] data) {
        double[] x = data[0].clone();

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

        return x;
    }
4.
haifengl/smile · KPCA.java
Match rating: 55.33% · See similar code snippets
java logo
@Override
    public double[][] project(T[] x) {
        int m = x.length;
        int n = data.length;
        double[][] y = new double[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                y[i][j] = kernel.k(x[i], data[j]);
            }

            double my = Math.mean(y[i]);
            for (int j = 0; j < n; j++) {
                y[i][j] = y[i][j] - my - mean[j] + mu;
            }

        }

        double[][] z = new double[x.length][p];
        for (int i = 0; i < y.length; i++) {
            projection.ax(y[i], z[i]);
        }
        return z;
    }
5.
biojava/biojava · Matrix.java
Match rating: 54.3% · See similar code snippets
java logo
public static double[] multiply(double[][] A, double[] x) {
		int m = A.length;
		int n = A[0].length;
		if (x.length != n) {
			throw new RuntimeException("Illegal matrix dimensions.");
		}
		double[] y = new double[m];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				y[i] += (A[i][j] * x[j]);
			}
		}
		return y;
	}
6.
haifengl/smile · Math.java
Match rating: 54.05% · See similar code snippets
java logo
public static double[] colMax(double[][] data) {
        double[] x = new double[data[0].length];
        for (int i = 0; i < x.length; i++) {
            x[i] = Double.NEGATIVE_INFINITY;
        }

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

        return x;
    }
7.
haifengl/smile · Math.java
Match rating: 53.98% · See similar code snippets
java logo
public static double[] colMin(double[][] data) {
        double[] x = new double[data[0].length];
        for (int i = 0; i < x.length; i++) {
            x[i] = Double.POSITIVE_INFINITY;
        }

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

        return x;
    }
8.
haifengl/smile · Math.java
Match rating: 52.18% · 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;
    }
9.
biojava/biojava · Matrix.java
Match rating: 52.06% · See similar code snippets
java logo
public static double[][] add(double[][] A, double[][] B) {
		int m = A.length;
		int n = A[0].length;
		double[][] C = new double[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				C[i][j] = A[i][j] + B[i][j];
			}
		}
		return C;
	}
10.
jMetal/jMetal · Benchmark.java
Match rating: 50.88% · See similar code snippets
java logo
static public void xA(double[] result, double[] x, double[][] A) {
    for (int i = 0; i < result.length; i++) {
      result[i] = 0.0;
      for (int j = 0; j < result.length; j++) {
        result[i] += (x[j] * A[j][i]);
      }
    }
  }