/**
   * Applies rank transform to each of the columns of <code>matrix</code> using the current <code>
   * rankingAlgorithm</code>
   *
   * @param matrix matrix to transform
   * @return a rank-transformed matrix
   */
  private RealMatrix rankTransform(final RealMatrix matrix) {
    RealMatrix transformed = null;

    if (rankingAlgorithm instanceof NaturalRanking
        && ((NaturalRanking) rankingAlgorithm).getNanStrategy() == NaNStrategy.REMOVED) {
      final Set<Integer> nanPositions = new HashSet<Integer>();
      for (int i = 0; i < matrix.getColumnDimension(); i++) {
        nanPositions.addAll(getNaNPositions(matrix.getColumn(i)));
      }

      // if we have found NaN values, we have to update the matrix size
      if (!nanPositions.isEmpty()) {
        transformed =
            new BlockRealMatrix(
                matrix.getRowDimension() - nanPositions.size(), matrix.getColumnDimension());
        for (int i = 0; i < transformed.getColumnDimension(); i++) {
          transformed.setColumn(i, removeValues(matrix.getColumn(i), nanPositions));
        }
      }
    }

    if (transformed == null) {
      transformed = matrix.copy();
    }

    for (int i = 0; i < transformed.getColumnDimension(); i++) {
      transformed.setColumn(i, rankingAlgorithm.rank(transformed.getColumn(i)));
    }

    return transformed;
  }
 /**
  * Computes the Kendall's Tau rank correlation matrix for the columns of the input matrix.
  *
  * @param matrix matrix with columns representing variables to correlate
  * @return correlation matrix
  */
 public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) {
   int nVars = matrix.getColumnDimension();
   RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
   for (int i = 0; i < nVars; i++) {
     for (int j = 0; j < i; j++) {
       double corr = correlation(matrix.getColumn(i), matrix.getColumn(j));
       outMatrix.setEntry(i, j, corr);
       outMatrix.setEntry(j, i, corr);
     }
     outMatrix.setEntry(i, i, 1d);
   }
   return outMatrix;
 }
  public double computeSimilarity(RealMatrix sourceDoc, RealMatrix targetDoc) {
    if (sourceDoc.getRowDimension() != targetDoc.getRowDimension()
        || sourceDoc.getColumnDimension() != targetDoc.getColumnDimension()
        || sourceDoc.getColumnDimension() != 1) {
      throw new IllegalArgumentException(
          "Matrices are not column matrices or not of the same size");
    }
    double[] source = sourceDoc.getColumn(0);
    double[] target = targetDoc.getColumn(0);

    double dotProduct = dot(source, target);
    double distance = norm(source) * norm(target);
    return dotProduct / distance;
  }
Exemple #4
0
 /**
  * Compute a covariance matrix from a matrix whose columns represent covariates.
  *
  * @param matrix input matrix (must have at least one column and two rows)
  * @param biasCorrected determines whether or not covariance estimates are bias-corrected
  * @return covariance matrix
  * @throws MathIllegalArgumentException if the matrix does not contain sufficient data
  */
 protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
     throws MathIllegalArgumentException {
   int dimension = matrix.getColumnDimension();
   Variance variance = new Variance(biasCorrected);
   RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
   for (int i = 0; i < dimension; i++) {
     for (int j = 0; j < i; j++) {
       double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
       outMatrix.setEntry(i, j, cov);
       outMatrix.setEntry(j, i, cov);
     }
     outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
   }
   return outMatrix;
 }
 /**
  * Returns a list containing the indices of NaN values in the input array.
  *
  * @param input the input array
  * @return a list of NaN positions in the input array
  */
 private void rankTransform(RealMatrix matrix) {
   for (int i = 0; i < matrix.getColumnDimension(); i++) {
     matrix.setColumn(i, rankingAlgorithm.rank(matrix.getColumn(i)));
   }
 }