org.apache.commons.math3.linear.SingularMatrixException Java Examples

The following examples show how to use org.apache.commons.math3.linear.SingularMatrixException. 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: TrilaterationTest.java    From trilateration with MIT License 6 votes vote down vote up
private void outputResult() {
	output = new StringBuilder();
	printDoubleArray("expectedPosition: ", expectedPosition);
	printDoubleArray("linear calculatedPosition: ", linearCalculatedPosition.toArray());
	printDoubleArray("non-linear calculatedPosition: ", nonLinearOptimum.getPoint().toArray());
	output.append("numberOfIterations: ").append(nonLinearOptimum.getIterations()).append("\n");
	output.append("numberOfEvaluations: ").append(nonLinearOptimum.getEvaluations()).append("\n");
	try {
		RealVector standardDeviation = nonLinearOptimum.getSigma(0);
		printDoubleArray("standardDeviation: ", standardDeviation.toArray());
		output.append("Norm of deviation: ").append(standardDeviation.getNorm()).append("\n");
		RealMatrix covarianceMatrix = nonLinearOptimum.getCovariances(0);
		output.append("covarianceMatrix: ").append(covarianceMatrix).append("\n");
	} catch (SingularMatrixException e) {
		System.err.println(e.getMessage());
	}

	System.out.println(output.toString());
}
 
Example #2
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    optimizer.getCovariances(1.5e-14);
}
 
Example #3
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #4
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    double[][] cov = optimizer.getCovariances(1.5e-14);
}
 
Example #5
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #6
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #7
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #8
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    double[][] cov = optimizer.getCovariances(1.5e-14);
}
 
Example #9
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #10
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #11
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #12
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 });
    Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #13
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #14
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static RealMatrix inverse(@Nonnull final RealMatrix m, final boolean exact)
        throws SingularMatrixException {
    LUDecomposition LU = new LUDecomposition(m);
    DecompositionSolver solver = LU.getSolver();
    final RealMatrix inv;
    if (exact || solver.isNonSingular()) {
        inv = solver.getInverse();
    } else {
        SingularValueDecomposition SVD = new SingularValueDecomposition(m);
        inv = SVD.getSolver().getInverse();
    }
    return inv;
}
 
Example #15
Source File: MinCovDet.java    From macrobase with Apache License 2.0 5 votes vote down vote up
private void updateInverseCovariance() {
    try {
        inverseCov = new LUDecomposition(cov).getSolver().getInverse();
    } catch (SingularMatrixException e) {
        singularCovariances.inc();
        inverseCov = new SingularValueDecomposition(cov).getSolver().getInverse();
    }
}
 
Example #16
Source File: TestOrdinaryLeastSquares.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstantPrediction(){

    Double timestamp = 1565444720000.0;
    Double inputCount = 1000.0;
    Double outputCount = 1000.0;
    Double queueCount = 50.0;

    Double[] feature0 = {timestamp - 1000,outputCount/inputCount};
    Double[] feature1 = {timestamp,outputCount/inputCount};
    Double[] feature2 = {timestamp + 1000,outputCount/inputCount};
    Double[] feature3 = {timestamp + 2000,outputCount/inputCount};

    Double[][] features = {feature0, feature1,feature2,feature3};
    Double[] labels = {queueCount,queueCount,queueCount, queueCount};

    OrdinaryLeastSquares model = new OrdinaryLeastSquares();
    boolean exOccurred = false;
    try {
        model.learn(Stream.of(features), Stream.of(labels));
    } catch (SingularMatrixException sme){
        exOccurred = true;
    }
    // SingularMatrixException should not be thrown, it will instead be logged
    assertFalse(exOccurred);

}
 
Example #17
Source File: OrdinaryLeastSquares.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Double> getScores() {
    if (coefficients == null) {
        return null;
    } else {
        Map<String, Double> scores = new HashMap<>();
        try {
            scores.put("rSquared", olsModel.calculateRSquared());
            scores.put("totalSumOfSquares", olsModel.calculateTotalSumOfSquares());
        } catch (SingularMatrixException sme) {
            LOG.debug("The OLSMultipleLinearRegression model's matrix has no inverse (i.e. it is singular) so no scores can be calculated at this time.");
        }
        return scores;
    }
}
 
Example #18
Source File: MatrixUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
/**
 * L = A x R
 *
 * @return a matrix A that minimizes A x R - L
 */
@Nonnull
public static RealMatrix solve(@Nonnull final RealMatrix L, @Nonnull final RealMatrix R,
        final boolean exact) throws SingularMatrixException {
    LUDecomposition LU = new LUDecomposition(R);
    DecompositionSolver solver = LU.getSolver();
    final RealMatrix A;
    if (exact || solver.isNonSingular()) {
        A = LU.getSolver().solve(L);
    } else {
        SingularValueDecomposition SVD = new SingularValueDecomposition(R);
        A = SVD.getSolver().solve(L);
    }
    return A;
}
 
Example #19
Source File: KalmanFilter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z
 *            the measurement vector
 * @throws NullArgumentException
 *             if the measurement vector is {@code null}
 * @throws DimensionMismatchException
 *             if the dimension of the measurement vector does not fit
 * @throws SingularMatrixException
 *             if the covariance matrix could not be inverted
 */
public void correct(final RealVector z)
        throws NullArgumentException, DimensionMismatchException, SingularMatrixException {

    // sanity checks
    MathUtils.checkNotNull(z);
    if (z.getDimension() != measurementMatrix.getRowDimension()) {
        throw new DimensionMismatchException(z.getDimension(),
                                             measurementMatrix.getRowDimension());
    }

    // S = H * P(k) - * H' + R
    RealMatrix s = measurementMatrix.multiply(errorCovariance)
        .multiply(measurementMatrixT)
        .add(measurementModel.getMeasurementNoise());

    // invert S
    // as the error covariance matrix is a symmetric positive
    // semi-definite matrix, we can use the cholesky decomposition
    DecompositionSolver solver = new CholeskyDecomposition(s).getSolver();
    RealMatrix invertedS = solver.getInverse();

    // Inn = z(k) - H * xHat(k)-
    RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));

    // calculate gain matrix
    // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
    // K(k) = P(k)- * H' * S^-1
    RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS);

    // update estimate with measurement z(k)
    // xHat(k) = xHat(k)- + K * Inn
    stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));

    // update covariance of prediction error
    // P(k) = (I - K * H) * P(k)-
    RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
    errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
 
Example #20
Source File: QRDecompositionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=SingularMatrixException.class)
public void testQRSingular() {
    final RealMatrix a = MatrixUtils.createRealMatrix(new double[][] {
        { 1, 6, 4 }, { 2, 4, -1 }, { -1, 2, 5 }
    });
    final RealVector b = new ArrayRealVector(new double[]{ 5, 6, 1 });
    new QRDecomposition(a, 1.0e-15).getSolver().solve(b);
}
 
Example #21
Source File: KalmanFilter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z
 *            the measurement vector
 * @throws NullArgumentException
 *             if the measurement vector is {@code null}
 * @throws DimensionMismatchException
 *             if the dimension of the measurement vector does not fit
 * @throws SingularMatrixException
 *             if the covariance matrix could not be inverted
 */
public void correct(final RealVector z)
        throws NullArgumentException, DimensionMismatchException, SingularMatrixException {

    // sanity checks
    MathUtils.checkNotNull(z);
    if (z.getDimension() != measurementMatrix.getRowDimension()) {
        throw new DimensionMismatchException(z.getDimension(),
                                             measurementMatrix.getRowDimension());
    }

    // S = H * P(k) - * H' + R
    RealMatrix s = measurementMatrix.multiply(errorCovariance)
        .multiply(measurementMatrixT)
        .add(measurementModel.getMeasurementNoise());

    // invert S
    // as the error covariance matrix is a symmetric positive
    // semi-definite matrix, we can use the cholesky decomposition
    DecompositionSolver solver = new CholeskyDecomposition(s).getSolver();
    RealMatrix invertedS = solver.getInverse();

    // Inn = z(k) - H * xHat(k)-
    RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));

    // calculate gain matrix
    // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
    // K(k) = P(k)- * H' * S^-1
    RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS);

    // update estimate with measurement z(k)
    // xHat(k) = xHat(k)- + K * Inn
    stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));

    // update covariance of prediction error
    // P(k) = (I - K * H) * P(k)-
    RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
    errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
 
Example #22
Source File: QRDecompositionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    QRDecomposition qr =
        new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular));

    final RealMatrix inv = qr.getSolver().getInverse();
}
 
Example #23
Source File: QRDecompositionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    QRDecomposition qr =
        new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular));

    final RealMatrix inv = qr.getSolver().getInverse();
}
 
Example #24
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    final LevenbergMarquardtOptimizer optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(20)
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    final double[] optimum = optimizer.optimize().getPoint();
    Assert.assertTrue(FastMath.sqrt(optimizer.getTarget().length) * optimizer.computeRMS(optimum) > 0.6);

    optimizer.computeCovariances(optimum, 1.5e-14);
}
 
Example #25
Source File: QRDecompositionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    QRDecomposition qr =
        new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular));

    final RealMatrix inv = qr.getSolver().getInverse();
}
 
Example #26
Source File: InvertMatrix.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the right pseudo inverse. Input matrix must have full row rank.
 *
 * See also: <a href="https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Definition">Moore–Penrose inverse</a>
 *
 * @param arr Input matrix
 * @param inPlace Whether to store the result in {@code arr}
 * @return Right pseudo inverse of {@code arr}
 * @exception IllegalArgumentException Input matrix {@code arr} did not have full row rank.
 */
public static INDArray pRightInvert(INDArray arr, boolean inPlace) {
    try{
        final INDArray inv = arr.transpose().mmul(invert(arr.mmul(arr.transpose()), inPlace));
        if (inPlace) arr.assign(inv);
        return inv;
    } catch (SingularMatrixException e){
        throw new IllegalArgumentException(
            "Full row rank condition for right pseudo inverse was not met.");
    }
}
 
Example #27
Source File: KalmanFilter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Correct the current state estimate with an actual measurement.
 *
 * @param z
 *            the measurement vector
 * @throws NullArgumentException
 *             if the measurement vector is {@code null}
 * @throws DimensionMismatchException
 *             if the dimension of the measurement vector does not fit
 * @throws SingularMatrixException
 *             if the covariance matrix could not be inverted
 */
public void correct(final RealVector z)
        throws NullArgumentException, DimensionMismatchException, SingularMatrixException {

    // sanity checks
    MathUtils.checkNotNull(z);
    if (z.getDimension() != measurementMatrix.getRowDimension()) {
        throw new DimensionMismatchException(z.getDimension(),
                                             measurementMatrix.getRowDimension());
    }

    // S = H * P(k) - * H' + R
    RealMatrix s = measurementMatrix.multiply(errorCovariance)
        .multiply(measurementMatrixT)
        .add(measurementModel.getMeasurementNoise());

    // invert S
    // as the error covariance matrix is a symmetric positive
    // semi-definite matrix, we can use the cholesky decomposition
    DecompositionSolver solver = new CholeskyDecomposition(s).getSolver();
    RealMatrix invertedS = solver.getInverse();

    // Inn = z(k) - H * xHat(k)-
    RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));

    // calculate gain matrix
    // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
    // K(k) = P(k)- * H' * S^-1
    RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS);

    // update estimate with measurement z(k)
    // xHat(k) = xHat(k)- + K * Inn
    stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));

    // update covariance of prediction error
    // P(k) = (I - K * H) * P(k)-
    RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
    errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
 
Example #28
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    final LevenbergMarquardtOptimizer optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(20)
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    final double[] optimum = optimizer.optimize().getPoint();
    Assert.assertTrue(FastMath.sqrt(optimizer.getTarget().length) * optimizer.computeRMS(optimum) > 0.6);

    optimizer.computeCovariances(optimum, 1.5e-14);
}
 
Example #29
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testNonInvertible() {
    try{
        /*
         * Overrides the method from parent class, since the default singularity
         * threshold (1e-14) does not trigger the expected exception.
         */
        LinearProblem problem = new LinearProblem(new double[][] {
                {  1, 2, -3 },
                {  2, 1,  3 },
                { -3, 0, -9 }
        }, new double[] { 1, 1, 1 });

        final Optimum optimum = optimizer.optimize(
                problem.getBuilder().maxIterations(20).build());

        //TODO check that it is a bad fit? Why the extra conditions?
        Assert.assertTrue(FastMath.sqrt(problem.getTarget().length) * optimum.getRMS() > 0.6);

        optimum.getCovariances(1.5e-14);

        fail(optimizer);
    }catch (SingularMatrixException e){
        //expected
    }
}
 
Example #30
Source File: QRDecompositionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=SingularMatrixException.class)
public void testQRSingular() {
    final RealMatrix a = MatrixUtils.createRealMatrix(new double[][] {
        { 1, 6, 4 }, { 2, 4, -1 }, { -1, 2, 5 }
    });
    final RealVector b = new ArrayRealVector(new double[]{ 5, 6, 1 });
    new QRDecomposition(a, 1.0e-15).getSolver().solve(b);
}