org.apache.commons.math.linear.InvalidMatrixException Java Examples

The following examples show how to use org.apache.commons.math.linear.InvalidMatrixException. 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: EigenSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test non invertible matrix */
public void testNonInvertible() {
    Random r = new Random(9994100315209l);
    RealMatrix m =
        EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
    DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
    assertFalse(es.isNonSingular());
    try {
        es.getInverse();
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #2
Source File: MfFeaturesManager.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
/**
  * http://www.slideshare.net/fullscreen/srowen/matrix-factorization/16 
  * @param recentitemInteractions
  * @param productFeaturesInverse
  * @param idMap
  * @return
  */
 private double[][] computeUserFoldInMatrix(double[][] itemFactors) 
 {
 	try
 	{
 		RealMatrix Y = new Array2DRowRealMatrix(itemFactors);
 		RealMatrix YTY = Y.transpose().multiply(Y);
 		RealMatrix YTYInverse = new LUDecompositionImpl(YTY).getSolver().getInverse();

 		return Y.multiply(YTYInverse).getData();
 	}
 	catch (InvalidMatrixException e)
 	{
 		logger.warn("Failed to create inverse of products feature matrix",e);
 		return null;
 	}
}
 
Example #3
Source File: EigenSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test non invertible matrix */
public void testNonInvertible() {
    Random r = new Random(9994100315209l);
    RealMatrix m =
        EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
    DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
    assertFalse(es.isNonSingular());
    try {
        es.getInverse();
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #4
Source File: EigenSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test non invertible matrix */
public void testNonInvertible() {
    Random r = new Random(9994100315209l);
    RealMatrix m =
        EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
    DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
    assertFalse(es.isNonSingular());
    try {
        es.getInverse();
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #5
Source File: EigenSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test non invertible matrix */
public void testNonInvertible() {
    Random r = new Random(9994100315209l);
    RealMatrix m =
        EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
    DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
    assertFalse(es.isNonSingular());
    try {
        es.getInverse();
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #6
Source File: EigenSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test non invertible matrix */
public void testNonInvertible() {
    Random r = new Random(9994100315209l);
    RealMatrix m =
        EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
    DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
    assertFalse(es.isNonSingular());
    try {
        es.getInverse();
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #7
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #8
Source File: TriDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testNonSquare() {
    try {
        new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #9
Source File: TriDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testNonSquare() {
    try {
        new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #10
Source File: FieldLUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non-square matrix */
public void testNonSquare() {
    try {
        new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(new Fraction[][] {
                { Fraction.ZERO, Fraction.ZERO },
                { Fraction.ZERO, Fraction.ZERO },
                { Fraction.ZERO, Fraction.ZERO }
        }));
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #11
Source File: LUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non-square matrix */
public void testNonSquare() {
    try {
        new LUDecompositionImpl(MatrixUtils.createRealMatrix(new double[3][2]));
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #12
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #13
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #14
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #15
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #16
Source File: TriDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testNonSquare() {
    try {
        new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #17
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #18
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {
 
    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int rows = problem.getMeasurements().length;
    final int cols = problem.getUnboundParameters().length;
    final int max  = cols * rows;
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += cols) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #19
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {
 
    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int rows = problem.getMeasurements().length;
    final int cols = problem.getUnboundParameters().length;
    final int max  = cols * rows;
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += cols) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #20
Source File: FieldLUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non-square matrix */
public void testNonSquare() {
    try {
        new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(new Fraction[][] {
                { Fraction.ZERO, Fraction.ZERO },
                { Fraction.ZERO, Fraction.ZERO },
                { Fraction.ZERO, Fraction.ZERO }
        }));
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #21
Source File: TriDiagonalTransformerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testNonSquare() {
    try {
        new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2]));
        fail("an exception should have been thrown");
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #22
Source File: FieldLUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non-square matrix */
public void testNonSquare() {
    try {
        new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(new Fraction[][] {
                { Fraction.ZERO, Fraction.ZERO },
                { Fraction.ZERO, Fraction.ZERO },
                { Fraction.ZERO, Fraction.ZERO }
        }));
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #23
Source File: LUDecompositionImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** test non-square matrix */
public void testNonSquare() {
    try {
        new LUDecompositionImpl(MatrixUtils.createRealMatrix(new double[3][2]));
    } catch (InvalidMatrixException ime) {
        // expected behavior
    } catch (Exception e) {
        fail("wrong exception caught");
    }
}
 
Example #24
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {
 
    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int rows = problem.getMeasurements().length;
    final int cols = problem.getUnboundParameters().length;
    final int max  = cols * rows;
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += cols) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #25
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #26
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #27
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #28
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example #29
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example #30
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}