Java Code Examples for org.apache.commons.math3.linear.RealMatrix#addToEntry()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#addToEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpectralMethods.java    From egads with GNU General Public License v3.0 6 votes vote down vote up
public static RealMatrix averageHankelMatrix(RealMatrix hankelMat, int windowSize) {

        int k = hankelMat.getRowDimension();
        int m = hankelMat.getColumnDimension() / windowSize;
        int n = k + windowSize - 1;

        RealMatrix result = MatrixUtils.createRealMatrix(n, m);

        for (int t = 0; t < n; ++t) {
            int i = (t < windowSize) ? 0 : (t - windowSize + 1);
            int j = (t < windowSize) ? t : (windowSize - 1);
            int counter = 0;

            for (; i < k && j >= 0; ++i, --j, ++counter) {
                for (int h = 0; h < m; ++h) {
                    result.addToEntry(t, h, hankelMat.getEntry(i, j * m + h));
                }
            }

            for (int h = 0; h < m; ++h) {
                result.setEntry(t, h, result.getEntry(t, h) / counter);
            }
        }

        return result;
    }
 
Example 2
Source File: PreAdapterOrientationScorer.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Gets PreAdapterQ collapsed over contexts.
 *
 * rows {PRO, CON} x cols {ref, alt}
 * @param metrics metrics usually read from a picard preadapter detail file.
 * @return mapping to score all orientation bias artifact modes to a PreAdapterQ score.  This score can be used as a bam-file level score for level of artifacts.
 */
@VisibleForTesting
static Map<Transition, RealMatrix> countOrientationBiasMetricsOverContext(final List<SequencingArtifactMetrics.PreAdapterDetailMetrics> metrics) {
    Utils.nonNull(metrics, "Input metrics cannot be null");

    // Artifact mode to a matrix
    final Map<Transition, RealMatrix> result = new HashMap<>();

    // Collapse over context
    for (SequencingArtifactMetrics.PreAdapterDetailMetrics metric : metrics) {
        final Transition key = Transition.transitionOf(metric.REF_BASE, metric.ALT_BASE);
        result.putIfAbsent(key, new Array2DRowRealMatrix(2, 2));
        final RealMatrix preAdapterCountMatrix = result.get(key);
        preAdapterCountMatrix.addToEntry(PreAdapterOrientationScorer.PRO, PreAdapterOrientationScorer.ALT, metric.PRO_ALT_BASES);
        preAdapterCountMatrix.addToEntry(PreAdapterOrientationScorer.CON, PreAdapterOrientationScorer.ALT, metric.CON_ALT_BASES);
        preAdapterCountMatrix.addToEntry(PreAdapterOrientationScorer.PRO, PreAdapterOrientationScorer.REF, metric.PRO_REF_BASES);
        preAdapterCountMatrix.addToEntry(PreAdapterOrientationScorer.CON, PreAdapterOrientationScorer.REF, metric.CON_REF_BASES);
    }

    return result;
}
 
Example 3
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 4
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 5
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 6
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath891Matrix() {
    final double[] xArray = new double[] { Double.NaN, 1.9, 2, 100, 3 };
    final double[] yArray = new double[] { 10, 2, 10, Double.NaN, 4 };

    RealMatrix matrix = MatrixUtils.createRealMatrix(xArray.length, 2);
    for (int i = 0; i < xArray.length; i++) {
        matrix.addToEntry(i, 0, xArray[i]);
        matrix.addToEntry(i, 1, yArray[i]);
    }

    // compute correlation
    NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED);
    SpearmansCorrelation spearman = new SpearmansCorrelation(matrix, ranking);
    
    Assert.assertEquals(0.5, spearman.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
}
 
Example 7
Source File: AlternatingLeastSquares.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
/**
 * Like {@link MatrixUtils#transposeTimesSelf(FastByIDMap)}, but instead of computing MT * M, 
 * it computes MT * C * M, where C is a diagonal matrix of 1s and 0s. This is like pretending some
 * rows of M are 0.
 * 
 * @see MatrixUtils#transposeTimesSelf(FastByIDMap) 
 * @see #LOSS_IGNORES_UNSPECIFIED
 */
private static RealMatrix partialTransposeTimesSelf(FastByIDMap<float[]> M, 
                                                    int dimension, 
                                                    LongPrimitiveIterator keys) {
  RealMatrix result = new Array2DRowRealMatrix(dimension, dimension);
  while (keys.hasNext()) {
    long key = keys.next();
    float[] vector = M.get(key);
    for (int row = 0; row < dimension; row++) {
      float rowValue = vector[row];
      for (int col = 0; col < dimension; col++) {
        result.addToEntry(row, col, rowValue * vector[col]);
      }
    }
  }
  return result;
}
 
Example 8
Source File: LinearRegression.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/** Calculates the coefficients of linear ridge regression. */
public static double[] performRegression(Matrix a, Matrix b, double ridge) {
	RealMatrix x = MatrixUtils.createRealMatrix(a.getArray());
	RealMatrix y = MatrixUtils.createRealMatrix(b.getArray());
	int numberOfColumns = x.getColumnDimension();
	double[] coefficients = new double[numberOfColumns];
	RealMatrix xTransposed = x.transpose();
	Matrix result;
	boolean finished = false;
	while (!finished) {
		RealMatrix xTx = xTransposed.multiply(x);

		for (int i = 0; i < numberOfColumns; i++) {
			xTx.addToEntry(i, i, ridge);
		}

		RealMatrix xTy = xTransposed.multiply(y);
		coefficients = xTy.getColumn(0);

		try {
			// do not use Apache LUDecomposition for solve instead because it creates different
			// results
			result = new Matrix(xTx.getData()).solve(new Matrix(coefficients, coefficients.length));
			for (int i = 0; i < numberOfColumns; i++) {
				coefficients[i] = result.get(i, 0);
			}
			finished = true;
		} catch (Exception ex) {
			double ridgeOld = ridge;
			if (ridge > 0) {
				ridge *= 10;
			} else {
				ridge = 0.0000001;
			}
			finished = false;
			logger.warning("Error during calculation: " + ex.getMessage() + ": Increasing ridge factor from " + ridgeOld
					+ " to " + ridge);
		}
	}
	return coefficients;
}
 
Example 9
Source File: TestUserProfileAnomalyEigenEvaluator.java    From Eagle with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetect(){
    UserProfileAnomalyEigenEvaluator eigenEvaluator = new UserProfileAnomalyEigenEvaluator();
    String[] testCmdType = {"getfileinfo", "open", "listStatus", "setTimes", "setPermission", "rename",
                                "mkdirs", "create", "setReplication", "contentSummary", "delete", "setOwner",
                                "fsck"};
    List<String> tmpCmdTypesAsList = new ArrayList<String>();
    tmpCmdTypesAsList = Arrays.asList(testCmdType);
    Seq<String> testCmd = JavaConversions.asScalaBuffer(tmpCmdTypesAsList);
    String testSite = "sandbox";
    long testTimestamp = 14054440;
    String testUser = "test_user";
    RealMatrix testMatrix = new Array2DRowRealMatrix(1,testCmdType.length);
    for(int i=0; i < testMatrix.getColumnDimension();i++)
        testMatrix.addToEntry(0, i, 3.0);

    UserActivityAggModel testAggModel = new UserActivityAggModel(testUser, testMatrix, testCmd,testSite, testTimestamp);

    Long testVersion = new Long(1);
    RealMatrix testUMat = new Array2DRowRealMatrix(testCmdType.length,testCmdType.length);
    RealMatrix testDiagonalMat = new Array2DRowRealMatrix(testCmdType.length, testCmdType.length);

    for(int i=0; i< testCmdType.length; i++){
        for(int  j=0; j < testCmdType.length; j++){
            testUMat.addToEntry(i,j,1.0);
            testDiagonalMat.addToEntry(i,j,1.0);
        }
    }

    int dimension = testCmdType.length -1;
    double[] minVector = new double[testCmdType.length];
    double[] maxVector = new double[testCmdType.length];
    for(int i=0; i < minVector.length;i++) {
        minVector[i] = 1;
        maxVector[i] = 1;
    }

    RealVector testMinVec = new ArrayRealVector(minVector);
    RealVector testMaxVec = new ArrayRealVector(maxVector);
    RealVector[] testPCs = new ArrayRealVector[testCmdType.length];

    for(int i =0; i < testCmdType.length; i++) {
        testPCs[i] = new ArrayRealVector(testCmdType.length);
        for(int j = 0; j < testPCs[i].getDimension(); j++){
            testPCs[i].addToEntry(j, 1.0);
        }
    }

    RealVector testMaxL2Norm = new ArrayRealVector(maxVector);
    RealVector testMinL2Norm = new ArrayRealVector(minVector);
    UserCommandStatistics userCommandStatistics[] = new UserCommandStatistics[testCmdType.length];

    for(int i=0; i< testCmdType.length;i++){
        userCommandStatistics[i] = new UserCommandStatistics();
        userCommandStatistics[i].setCommandName(testCmdType[i]);
        userCommandStatistics[i].setLowVariant(false);
        userCommandStatistics[i].setMean(1.0);
        userCommandStatistics[i].setStddev(1.0);
    }

    UserProfileEigenModel testEigenModel = new UserProfileEigenModel(testVersion,testSite,testUser,testUMat,testDiagonalMat,dimension, testMinVec, testMaxVec, testPCs, testMaxL2Norm, testMinL2Norm, userCommandStatistics);

    List<MLCallbackResult> testResults = eigenEvaluator.detect("test_user", "eigen", testAggModel, testEigenModel);

    Assert.assertEquals(testResults.size(), testMatrix.getRowDimension());
    for(MLCallbackResult result: testResults){
        Assert.assertEquals(result.isAnomaly(), true);
    }
}