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

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#setEntry() . 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: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Derives a correlation matrix from a covariance matrix.
 *
 * <p>Uses the formula <br/>
 * <code>r(X,Y) = cov(X,Y)/s(X)s(Y)</code> where
 * <code>r(&middot,&middot;)</code> is the correlation coefficient and
 * <code>s(&middot;)</code> means standard deviation.</p>
 *
 * @param covarianceMatrix the covariance matrix
 * @return correlation matrix
 */
public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) {
    int nVars = covarianceMatrix.getColumnDimension();
    RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
    for (int i = 0; i < nVars; i++) {
        double sigma = FastMath.sqrt(covarianceMatrix.getEntry(i, i));
        outMatrix.setEntry(i, i, 1d);
        for (int j = 0; j < i; j++) {
            double entry = covarianceMatrix.getEntry(i, j) /
                   (sigma * FastMath.sqrt(covarianceMatrix.getEntry(j, j)));
            outMatrix.setEntry(i, j, entry);
            outMatrix.setEntry(j, i, entry);
        }
    }
    return outMatrix;
}
 
Example 2
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Derives a correlation matrix from a covariance matrix.
 *
 * <p>Uses the formula <br/>
 * <code>r(X,Y) = cov(X,Y)/s(X)s(Y)</code> where
 * <code>r(&middot,&middot;)</code> is the correlation coefficient and
 * <code>s(&middot;)</code> means standard deviation.</p>
 *
 * @param covarianceMatrix the covariance matrix
 * @return correlation matrix
 */
public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) {
    int nVars = covarianceMatrix.getColumnDimension();
    RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
    for (int i = 0; i < nVars; i++) {
        double sigma = FastMath.sqrt(covarianceMatrix.getEntry(i, i));
        outMatrix.setEntry(i, i, 1d);
        for (int j = 0; j < i; j++) {
            double entry = covarianceMatrix.getEntry(i, j) /
                   (sigma * FastMath.sqrt(covarianceMatrix.getEntry(j, j)));
            outMatrix.setEntry(i, j, entry);
            outMatrix.setEntry(j, i, entry);
        }
    }
    return outMatrix;
}
 
Example 3
Source File: VectorialCovariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the covariance matrix.
 * @return covariance matrix
 */
public RealMatrix getResult() {

    int dimension = sums.length;
    RealMatrix result = MatrixUtils.createRealMatrix(dimension, dimension);

    if (n > 1) {
        double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
        int k = 0;
        for (int i = 0; i < dimension; ++i) {
            for (int j = 0; j <= i; ++j) {
                double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
                result.setEntry(i, j, e);
                result.setEntry(j, i, e);
            }
        }
    }

    return result;

}
 
Example 4
Source File: RamPCACoveragePoNUnitTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void assertReducedPanelPInverseCounts(final PCACoveragePoN filePoN, final PCACoveragePoN ramPoN) {
    final RealMatrix ramNormalizedCounts = ramPoN.getReducedPanelPInverseCounts();
    final RealMatrix fileNormalizedCounts = filePoN.getReducedPanelPInverseCounts();

    Assert.assertEquals(ramNormalizedCounts.subtract(fileNormalizedCounts).getNorm(), 0, 1e-9);
    ramNormalizedCounts.setEntry(3, 4, 500000);
    Assert.assertEquals(ramNormalizedCounts.getEntry(3, 4), 500000, 1e-9);
    Assert.assertNotEquals(ramPoN.getReducedPanelPInverseCounts().getEntry(3, 4), 500000, 1e-9);

    Assert.assertFalse(ramNormalizedCounts.subtract(fileNormalizedCounts).getNorm() < 1e-9);

    final RealMatrix fileNormalizedCounts2 = filePoN.getReducedPanelPInverseCounts();
    Assert.assertFalse(ramNormalizedCounts.subtract(fileNormalizedCounts2).getNorm() < 1e-9);
}
 
Example 5
Source File: Math_13_AbstractLeastSquaresOptimizer_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Computes the square-root of the weight matrix.
 *
 * @param m Symmetric, positive-definite (weight) matrix.
 * @return the square-root of the weight matrix.
 */
private RealMatrix squareRoot(RealMatrix m) {
    if (m instanceof DiagonalMatrix) {
        final int dim = m.getRowDimension();
        final RealMatrix sqrtM = new DiagonalMatrix(dim);
        for (int i = 0; i < dim; i++) {
           sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
        }
        return sqrtM;
    } else {
        final EigenDecomposition dec = new EigenDecomposition(m);
        return dec.getSquareRoot();
    }
}
 
Example 6
Source File: GMMTrainerTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static void test1() throws Exception{
    MultiLabelClfDataSet dataSet = TRECFormat.loadMultiLabelClfDataSet("/Users/chengli/Downloads/scene/train_test_split/train", DataSetType.ML_CLF_DENSE,true);
    RealMatrix data = new Array2DRowRealMatrix(dataSet.getNumDataPoints(),dataSet.getNumFeatures());
    for (int i=0;i<dataSet.getNumDataPoints();i++){
        for (int j=0;j<dataSet.getNumFeatures();j++){
            data.setEntry(i,j,dataSet.getRow(i).get(j));
        }
    }

    int numComponents=10;
    GMM gmm = new GMM(dataSet.getNumFeatures(),numComponents, data);

    GMMTrainer trainer = new GMMTrainer(data, gmm);


    for (int i=1;i<=5;i++){
        System.out.println("iteration = "+i);
        trainer.iterate();
        double logLikelihood = IntStream.range(0,dataSet.getNumDataPoints()).parallel()
                .mapToDouble(j->gmm.logDensity(data.getRowVector(j))).sum();
        System.out.println("log likelihood = "+logLikelihood);
        Serialization.serialize(gmm, "/Users/chengli/tmp/gmm/model_iter_"+i);
        for (int k=0;k<gmm.getNumComponents();k++){
            FileUtils.writeStringToFile(new File("/Users/chengli/tmp/gmm/mean_iter_"+i+"_component_"+(k+1)),
                    gmm.getGaussianDistributions()[k].getMean().toString().replace("{","")
                            .replace("}","").replace(";",","));
        }
    }


}
 
Example 7
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the correlation matrix for the columns of the
 * input matrix.
 *
 * @param matrix matrix with columns representing variables to correlate
 * @return correlation matrix
 */
public RealMatrix computeCorrelationMatrix(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;
}
 
Example 8
Source File: LinearAlgebra.java    From january with Eclipse Public License 1.0 5 votes vote down vote up
private static RealMatrix createRealMatrix(Dataset a) {
	if (a.getRank() != 2) {
		throw new IllegalArgumentException("Dataset must be rank 2");
	}
	int[] shape = a.getShapeRef();
	IndexIterator it = a.getIterator(true);
	int[] pos = it.getPos();
	RealMatrix m = MatrixUtils.createRealMatrix(shape[0], shape[1]);
	while (it.hasNext()) {
		m.setEntry(pos[0], pos[1], a.getElementDoubleAbs(it.index));
	}
	return m;
}
 
Example 9
Source File: CorrelatedRandomVectorGeneratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public CorrelatedRandomVectorGeneratorTest() {
    mean = new double[] { 0.0, 1.0, -3.0, 2.3 };

    RealMatrix b = MatrixUtils.createRealMatrix(4, 3);
    int counter = 0;
    for (int i = 0; i < b.getRowDimension(); ++i) {
        for (int j = 0; j < b.getColumnDimension(); ++j) {
            b.setEntry(i, j, 1.0 + 0.1 * ++counter);
        }
    }
    RealMatrix bbt = b.multiply(b.transpose());
    covariance = MatrixUtils.createRealMatrix(mean.length, mean.length);
    for (int i = 0; i < covariance.getRowDimension(); ++i) {
        covariance.setEntry(i, i, bbt.getEntry(i, i));
        for (int j = 0; j < covariance.getColumnDimension(); ++j) {
            double s = bbt.getEntry(i, j);
            covariance.setEntry(i, j, s);
            covariance.setEntry(j, i, s);
        }
    }

    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    generator = new CorrelatedRandomVectorGenerator(mean,
                                                    covariance,
                                                    1.0e-12 * covariance.getNorm(),
                                                    rawGenerator);
}
 
Example 10
Source File: GamaFloatMatrix.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public RealMatrix toApacheMatrix(final IScope scope) {
	final RealMatrix rm = new Array2DRowRealMatrix(numRows, numCols);
	for (int i = 0; i < numCols; i++) {
		for (int j = 0; j < numRows; j++) {
			final double val = get(scope, i, j);
			rm.setEntry(j, i, val);
		}
	}
	return rm;
}
 
Example 11
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
/**
 * Lanczos tridiagonalization for a symmetric matrix C to make s * s tridiagonal matrix T.
 *
 * @see http://www.cas.mcmaster.ca/~qiao/publications/spie05.pdf
 * @param C target symmetric matrix
 * @param a initial vector
 * @param T result is stored here
 */
public static void lanczosTridiagonalization(@Nonnull final RealMatrix C,
        @Nonnull final double[] a, @Nonnull final RealMatrix T) {
    Preconditions.checkArgument(Arrays.deepEquals(C.getData(), C.transpose().getData()),
        "Target matrix C must be a symmetric matrix");
    Preconditions.checkArgument(C.getColumnDimension() == a.length,
        "Column size of A and length of a should be same");
    Preconditions.checkArgument(T.getRowDimension() == T.getColumnDimension(),
        "T must be a square matrix");

    int s = T.getRowDimension();

    // initialize T with zeros
    T.setSubMatrix(new double[s][s], 0, 0);

    RealVector a0 = new ArrayRealVector(a.length);
    RealVector r = new ArrayRealVector(a);

    double beta0 = 1.d;

    for (int i = 0; i < s; i++) {
        RealVector a1 = r.mapDivide(beta0);
        RealVector Ca1 = C.operate(a1);

        double alpha1 = a1.dotProduct(Ca1);

        r = Ca1.add(a1.mapMultiply(-1.d * alpha1)).add(a0.mapMultiply(-1.d * beta0));

        double beta1 = r.getNorm();

        T.setEntry(i, i, alpha1);
        if (i - 1 >= 0) {
            T.setEntry(i, i - 1, beta0);
        }
        if (i + 1 < s) {
            T.setEntry(i, i + 1, beta1);
        }

        a0 = a1.copy();
        beta0 = beta1;
    }
}
 
Example 12
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1,
                               RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 13
Source File: CMAESOptimizer.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1,
                               RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 14
Source File: JGenProg2017_00111_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 15
Source File: Math_20_CMAESOptimizer_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 16
Source File: Arja_0079_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 17
Source File: Arja_00166_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 18
Source File: Cardumen_00213_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 19
Source File: Math_19_CMAESOptimizer_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}
 
Example 20
Source File: JGenProg2017_0020_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Copies a column from m1 to m2.
 *
 * @param m1 Source matrix 1.
 * @param col1 Source column.
 * @param m2 Target matrix.
 * @param col2 Target column.
 */
private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) {
    for (int i = 0; i < m1.getRowDimension(); i++) {
        m2.setEntry(i, col2, m1.getEntry(i, col1));
    }
}