use of org.apache.commons.math3.linear.RealVector in project knime-core by knime.
the class BinaryNominalSplitsPCATest method testCalculateWeightedCovarianceMatrix.
@Test
public void testCalculateWeightedCovarianceMatrix() {
final CombinedAttributeValues[] attVals = createTestAttVals();
final double totalSumWeight = 300;
final int numTargetVals = 3;
final RealVector meanClassProbabilityVector = BinaryNominalSplitsPCA.calculateMeanClassProbabilityVector(attVals, totalSumWeight, numTargetVals);
RealMatrix weightedCovarianceMatrix = BinaryNominalSplitsPCA.calculateWeightedCovarianceMatrix(attVals, meanClassProbabilityVector, totalSumWeight, numTargetVals);
// the reference matrix is altered to be easily readable therefore we have to do the same to the calculated matrix
weightedCovarianceMatrix = weightedCovarianceMatrix.scalarMultiply(1 / weightedCovarianceMatrix.getEntry(0, 0));
weightedCovarianceMatrix = weightedCovarianceMatrix.scalarMultiply(10);
final RealMatrix expectedWeightedCovarianceMatrix = MatrixUtils.createRealMatrix(new double[][] { { 10.0, -4.167, -5.833 }, { -4.167, 14.167, -10.0 }, { -5.833, -10.0, 15.833 } });
// RealMatrix does overwrite equals but all entries must be exactly the same for two matrices to be equal
// Therefore we need to use the asserEquals method that allows to define a delta
assertEquals(expectedWeightedCovarianceMatrix.getRowDimension(), weightedCovarianceMatrix.getRowDimension());
assertEquals(expectedWeightedCovarianceMatrix.getColumnDimension(), weightedCovarianceMatrix.getColumnDimension());
for (int r = 0; r < weightedCovarianceMatrix.getRowDimension(); r++) {
for (int c = 0; c < weightedCovarianceMatrix.getColumnDimension(); c++) {
assertEquals(expectedWeightedCovarianceMatrix.getEntry(r, c), weightedCovarianceMatrix.getEntry(r, c), 0.001);
}
}
}
use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.
the class AffineFit method fit.
@Override
public void fit(List<double[]> X, List<double[]> Y) {
// fits n-dimensional data sets with affine model
if (X.size() != Y.size())
throw new IllegalArgumentException("point sequences X, Y must have same length");
this.m = X.size();
this.n = X.get(0).length;
RealMatrix M = MatrixUtils.createRealMatrix(2 * m, 2 * (n + 1));
RealVector b = new ArrayRealVector(2 * m);
// mount matrix M:
int row = 0;
for (double[] x : X) {
for (int j = 0; j < n; j++) {
M.setEntry(row, j, x[j]);
M.setEntry(row, n, 1);
row++;
}
for (int j = 0; j < n; j++) {
M.setEntry(row, j + n + 1, x[j]);
M.setEntry(row, 2 * n + 1, 1);
row++;
}
}
// mount vector b
row = 0;
for (double[] y : Y) {
for (int j = 0; j < n; j++) {
b.setEntry(row, y[j]);
row++;
}
}
SingularValueDecomposition svd = new SingularValueDecomposition(M);
DecompositionSolver solver = svd.getSolver();
RealVector a = solver.solve(b);
A = makeTransformationMatrix(a);
}
use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.
the class ProcrustesFit method makeDataMatrix.
private RealMatrix makeDataMatrix(List<double[]> X, double[] meanX) {
if (meanX == null) {
return makeDataMatrix(X);
}
final int m = X.size();
final int n = X.get(0).length;
RealMatrix M = MatrixUtils.createRealMatrix(n, m);
RealVector mean = MatrixUtils.createRealVector(meanX);
int i = 0;
for (double[] x : X) {
RealVector xi = MatrixUtils.createRealVector(x).subtract(mean);
M.setColumnVector(i, xi);
i++;
}
return M;
}
use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.
the class ProcrustesFit method fit.
@Override
public void fit(List<double[]> X, List<double[]> Y) {
if (X.size() != Y.size())
throw new IllegalArgumentException("point sequences X, Y must have same length");
this.m = X.size();
this.n = X.get(0).length;
double[] meanX = null;
double[] meanY = null;
if (this.allowTranslation) {
meanX = getMeanVec(X);
meanY = getMeanVec(Y);
}
RealMatrix P = makeDataMatrix(X, meanX);
RealMatrix Q = makeDataMatrix(Y, meanY);
// P, Q of same dimensions?
MatrixUtils.checkAdditionCompatible(P, Q);
RealMatrix QPt = Q.multiply(P.transpose());
SingularValueDecomposition svd = new SingularValueDecomposition(QPt);
RealMatrix U = svd.getU();
RealMatrix S = svd.getS();
RealMatrix V = svd.getV();
double d = (svd.getRank() >= n) ? det(QPt) : det(U) * det(V);
RealMatrix D = MatrixUtils.createRealIdentityMatrix(n);
if (d < 0 && forceRotation)
D.setEntry(n - 1, n - 1, -1);
R = U.multiply(D).multiply(V.transpose());
double normP = P.getFrobeniusNorm();
double normQ = Q.getFrobeniusNorm();
c = (this.allowScaling) ? S.multiply(D).getTrace() / sqr(normP) : 1.0;
if (allowTranslation) {
RealVector ma = MatrixUtils.createRealVector(meanX);
RealVector mb = MatrixUtils.createRealVector(meanY);
t = mb.subtract(R.scalarMultiply(c).operate(ma));
} else {
// zero vector
t = new ArrayRealVector(n);
}
err = sqr(normQ) - sqr(S.multiply(D).getTrace() / normP);
}
use of org.apache.commons.math3.linear.RealVector in project imagingbook-common by imagingbook.
the class ProcrustesFit method getEuclideanError.
/**
* Calculates the total error for the estimated fit as
* the sum of the squared Euclidean distances between the
* transformed point set X and the reference set Y.
* This method is provided for testing as an alternative to
* the quicker {@link getError} method.
* @param X Sequence of n-dimensional points.
* @param Y Sequence of n-dimensional points (reference).
* @return The total error for the estimated fit.
*/
public double getEuclideanError(List<double[]> X, List<double[]> Y) {
RealMatrix cR = R.scalarMultiply(c);
double ee = 0;
for (int i = 0; i < X.size(); i++) {
RealVector ai = new ArrayRealVector(X.get(i));
RealVector bi = new ArrayRealVector(Y.get(i));
RealVector aiT = cR.operate(ai).add(t);
double ei = aiT.subtract(bi).getNorm();
ee = ee + sqr(ei);
}
return ee;
}
Aggregations