use of org.apache.commons.math3.linear.EigenDecomposition in project jstructure by JonStargaryen.
the class MultiDimensionalScaling method computeEmbedding.
public List<double[]> computeEmbedding(RealMatrix distanceMap, int targetDimension) {
numberOfDataPoints = distanceMap.getRowDimension();
this.targetDimension = targetDimension;
if (this.targetDimension > numberOfDataPoints) {
throw new IllegalArgumentException("target dimension must not exceed number of data points");
}
this.distanceMap = distanceMap;
proximityMap = computeSquaredProximityMap(this.distanceMap);
centeringMap = computeConfiguration(proximityMap);
normalizedEigenvectors = new ArrayList<>();
embedding = new ArrayList<>();
EigenDecomposition evd = new EigenDecomposition(centeringMap);
// we are looking for the m biggest eigenvalues - they are at the last elements of the matrix
RealMatrix eigenvectors = evd.getV();
// Matrix eigenvalues = evd.getD();
double[] eigenvalues = evd.getRealEigenvalues();
Map<Integer, Double> eigenvalueMap = new HashMap<>();
for (int eigenvalueIndex = 0; eigenvalueIndex < eigenvalues.length; eigenvalueIndex++) {
eigenvalueMap.put(eigenvalueIndex, eigenvalues[eigenvalueIndex]);
}
List<Entry<Integer, Double>> sortedEigenvalues = entriesSortedByValues(eigenvalueMap).subList(0, targetDimension);
// normalize eigenvectors
for (Entry<Integer, Double> sortedEigenvalue : sortedEigenvalues) {
if (sortedEigenvalue.getValue() <= 0) {
throw new IllegalArgumentException("eigenvalue is negative: " + sortedEigenvalue.getValue());
}
double[] eigenvector = eigenvectors.getColumn(sortedEigenvalue.getKey());
normalizedEigenvectors.add(normalize(eigenvector, Math.sqrt(sortedEigenvalue.getValue())));
}
// compose embedded data points from normalized eigenvectors
for (int dataPointIndex = 0; dataPointIndex < numberOfDataPoints; dataPointIndex++) {
double[] dataPoint = new double[this.targetDimension];
for (int dataPointDimension = 0; dataPointDimension < this.targetDimension; dataPointDimension++) {
dataPoint[dataPointDimension] = normalizedEigenvectors.get(dataPointDimension)[dataPointIndex];
}
embedding.add(dataPoint);
}
return embedding;
}
use of org.apache.commons.math3.linear.EigenDecomposition in project incubator-systemml by apache.
the class LibCommonsMath method computeEigen.
/**
* Function to perform Eigen decomposition on a given matrix.
* Input must be a symmetric matrix.
*
* @param in matrix object
* @return array of matrix blocks
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
private static MatrixBlock[] computeEigen(MatrixObject in) throws DMLRuntimeException {
if (in.getNumRows() != in.getNumColumns()) {
throw new DMLRuntimeException("Eigen Decomposition can only be done on a square matrix. Input matrix is rectangular (rows=" + in.getNumRows() + ", cols=" + in.getNumColumns() + ")");
}
Array2DRowRealMatrix matrixInput = DataConverter.convertToArray2DRowRealMatrix(in);
EigenDecomposition eigendecompose = new EigenDecomposition(matrixInput);
RealMatrix eVectorsMatrix = eigendecompose.getV();
double[][] eVectors = eVectorsMatrix.getData();
double[] eValues = eigendecompose.getRealEigenvalues();
//Sort the eigen values (and vectors) in increasing order (to be compatible w/ LAPACK.DSYEVR())
int n = eValues.length;
for (int i = 0; i < n; i++) {
int k = i;
double p = eValues[i];
for (int j = i + 1; j < n; j++) {
if (eValues[j] < p) {
k = j;
p = eValues[j];
}
}
if (k != i) {
eValues[k] = eValues[i];
eValues[i] = p;
for (int j = 0; j < n; j++) {
p = eVectors[j][i];
eVectors[j][i] = eVectors[j][k];
eVectors[j][k] = p;
}
}
}
MatrixBlock mbValues = DataConverter.convertToMatrixBlock(eValues, true);
MatrixBlock mbVectors = DataConverter.convertToMatrixBlock(eVectors);
return new MatrixBlock[] { mbValues, mbVectors };
}
use of org.apache.commons.math3.linear.EigenDecomposition in project gatk by broadinstitute.
the class CopyNumberTriStateTransitionProbabilityCacheUnitTest method markovianPropertiesTest.
//test various properties of a transition matrix
@Test(dataProvider = "meanEventSizeAndEventStartProbability")
public void markovianPropertiesTest(final double meanEventSize, final double eventStartProbability) {
final CopyNumberTriStateTransitionProbabilityCache cache = new CopyNumberTriStateTransitionProbabilityCache(meanEventSize, eventStartProbability);
for (final int d : DISTANCES) {
//check symmetries -- these are part of the model and need not be true in the future
final RealMatrix transitionMatrix = cache.getAsMatrixInProbabilitySpace(d);
assertSymmetries(transitionMatrix);
//check that columns sums equal 1
for (int column = 0; column < transitionMatrix.getColumnDimension(); column++) {
Assert.assertEquals(MathUtils.sum(transitionMatrix.getColumn(column)), 1, EPSILON);
}
//check that all elements are positive
transitionMatrix.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
@Override
public void visit(int row, int column, double value) {
Assert.assertTrue(value >= 0);
}
});
//check that T(2d) = T(d)*T(d)
assertEqualMatrices(cache.getAsMatrixInProbabilitySpace(2 * d), transitionMatrix.multiply(transitionMatrix));
//check that the largest eigenvalue of the transition matrix is 1 (this corresponds to the asymptotic stationary state)
Assert.assertEquals(MathUtils.arrayMax(new EigenDecomposition(transitionMatrix).getRealEigenvalues()), 1, EPSILON);
}
// check that at long distances memory of the initial state is lost and all initial distributions tend toward
// the same asymptotic stationary distribution. That is, all columns of the large-distance transition matrix are equal
final RealMatrix asymptoticMatrix = cache.getAsMatrixInProbabilitySpace(HUGE_DISTANCE);
for (int column = 1; column < asymptoticMatrix.getColumnDimension(); column++) {
Assert.assertEquals(asymptoticMatrix.getColumnVector(0).subtract(asymptoticMatrix.getColumnVector(column)).getL1Norm(), 0, EPSILON);
}
}
use of org.apache.commons.math3.linear.EigenDecomposition in project gatk-protected by broadinstitute.
the class CopyNumberTriStateTransitionProbabilityCacheUnitTest method markovianPropertiesTest.
//test various properties of a transition matrix
@Test(dataProvider = "meanEventSizeAndEventStartProbability")
public void markovianPropertiesTest(final double meanEventSize, final double eventStartProbability) {
final CopyNumberTriStateTransitionProbabilityCache cache = new CopyNumberTriStateTransitionProbabilityCache(meanEventSize, eventStartProbability);
for (final int d : DISTANCES) {
//check symmetries -- these are part of the model and need not be true in the future
final RealMatrix transitionMatrix = cache.getAsMatrixInProbabilitySpace(d);
assertSymmetries(transitionMatrix);
//check that columns sums equal 1
for (int column = 0; column < transitionMatrix.getColumnDimension(); column++) {
Assert.assertEquals(MathUtils.sum(transitionMatrix.getColumn(column)), 1, EPSILON);
}
//check that all elements are positive
transitionMatrix.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
@Override
public void visit(int row, int column, double value) {
Assert.assertTrue(value >= 0);
}
});
//check that T(2d) = T(d)*T(d)
assertEqualMatrices(cache.getAsMatrixInProbabilitySpace(2 * d), transitionMatrix.multiply(transitionMatrix));
//check that the largest eigenvalue of the transition matrix is 1 (this corresponds to the asymptotic stationary state)
Assert.assertEquals(MathUtils.arrayMax(new EigenDecomposition(transitionMatrix).getRealEigenvalues()), 1, EPSILON);
}
// check that at long distances memory of the initial state is lost and all initial distributions tend toward
// the same asymptotic stationary distribution. That is, all columns of the large-distance transition matrix are equal
final RealMatrix asymptoticMatrix = cache.getAsMatrixInProbabilitySpace(HUGE_DISTANCE);
for (int column = 1; column < asymptoticMatrix.getColumnDimension(); column++) {
Assert.assertEquals(asymptoticMatrix.getColumnVector(0).subtract(asymptoticMatrix.getColumnVector(column)).getL1Norm(), 0, EPSILON);
}
}
Aggregations