Search in sources :

Example 16 with DiagonalMatrix

use of org.apache.commons.math3.linear.DiagonalMatrix in project GDSC-SMLM by aherbert.

the class PCPALMMolecules method optimiseLeastSquares.

private double[] optimiseLeastSquares(float[] x, float[] y, double[] initialSolution) {
    // Least-squares optimisation using numerical gradients
    final SkewNormalDifferentiableFunction function = new SkewNormalDifferentiableFunction(initialSolution);
    function.addData(x, y);
    LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
    //@formatter:off
    LeastSquaresProblem problem = new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(3000).start(initialSolution).target(function.calculateTarget()).weight(new DiagonalMatrix(function.calculateWeights())).model(function, new MultivariateMatrixFunction() {

        public double[][] value(double[] point) throws IllegalArgumentException {
            return function.jacobian(point);
        }
    }).build();
    //@formatter:on
    Optimum optimum = optimizer.optimize(problem);
    double[] skewParameters = optimum.getPoint().toArray();
    return skewParameters;
}
Also used : Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) MultivariateMatrixFunction(org.apache.commons.math3.analysis.MultivariateMatrixFunction) LeastSquaresBuilder(org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)

Example 17 with DiagonalMatrix

use of org.apache.commons.math3.linear.DiagonalMatrix in project gatk by broadinstitute.

the class DecomposeSingularValuesIntegrationTest method assertSVDValues.

private void assertSVDValues(final File outputFileV, final File outputFileS, final File outputFileU) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(CONTROL_PCOV_FULL_FILE);
        final SVD svd = SVDFactory.createSVD(rcc.counts());
        final RealMatrix sDiag = new DiagonalMatrix(svd.getSingularValues());
        assertOutputFileValues(outputFileU, svd.getU());
        assertOutputFileValues(outputFileS, sDiag);
        assertOutputFileValues(outputFileV, svd.getV());
        assertUnitaryMatrix(svd.getV());
        assertUnitaryMatrix(svd.getU());
        Assert.assertTrue(MatrixUtils.isSymmetric(sDiag, 1e-32));
    } catch (final IOException ioe) {
        Assert.fail("Could not open test file: " + CONTROL_PCOV_FULL_FILE, ioe);
    }
}
Also used : SVD(org.broadinstitute.hellbender.utils.svd.SVD) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) IOException(java.io.IOException)

Example 18 with DiagonalMatrix

use of org.apache.commons.math3.linear.DiagonalMatrix in project gatk by broadinstitute.

the class DecomposeSingularValues method runPipeline.

@Override
protected void runPipeline(final JavaSparkContext ctx) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(inputFile);
        final SVD svd = SVDFactory.createSVD(rcc.counts(), ctx);
        writeMatrix(svd.getV(), outputFileV);
        writeMatrix(svd.getU(), outputFileU);
        writeMatrix(new DiagonalMatrix(svd.getSingularValues()), outputFileS);
    } catch (final IOException ioe) {
        throw new UserException.CouldNotReadInputFile(inputFile, ioe.getMessage());
    }
}
Also used : SVD(org.broadinstitute.hellbender.utils.svd.SVD) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) IOException(java.io.IOException) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 19 with DiagonalMatrix

use of org.apache.commons.math3.linear.DiagonalMatrix in project gatk-protected by broadinstitute.

the class DecomposeSingularValuesIntegrationTest method assertSVDValues.

private void assertSVDValues(final File outputFileV, final File outputFileS, final File outputFileU) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(CONTROL_PCOV_FULL_FILE);
        final SVD svd = SVDFactory.createSVD(rcc.counts());
        final RealMatrix sDiag = new DiagonalMatrix(svd.getSingularValues());
        assertOutputFileValues(outputFileU, svd.getU());
        assertOutputFileValues(outputFileS, sDiag);
        assertOutputFileValues(outputFileV, svd.getV());
        assertUnitaryMatrix(svd.getV());
        assertUnitaryMatrix(svd.getU());
        Assert.assertTrue(MatrixUtils.isSymmetric(sDiag, 1e-32));
    } catch (final IOException ioe) {
        Assert.fail("Could not open test file: " + CONTROL_PCOV_FULL_FILE, ioe);
    }
}
Also used : SVD(org.broadinstitute.hellbender.utils.svd.SVD) RealMatrix(org.apache.commons.math3.linear.RealMatrix) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) IOException(java.io.IOException)

Example 20 with DiagonalMatrix

use of org.apache.commons.math3.linear.DiagonalMatrix in project gatk-protected by broadinstitute.

the class DecomposeSingularValues method runPipeline.

@Override
protected void runPipeline(final JavaSparkContext ctx) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(inputFile);
        final SVD svd = SVDFactory.createSVD(rcc.counts(), ctx);
        writeMatrix(svd.getV(), outputFileV);
        writeMatrix(svd.getU(), outputFileU);
        writeMatrix(new DiagonalMatrix(svd.getSingularValues()), outputFileS);
    } catch (final IOException ioe) {
        throw new UserException.CouldNotReadInputFile(inputFile, ioe.getMessage());
    }
}
Also used : SVD(org.broadinstitute.hellbender.utils.svd.SVD) DiagonalMatrix(org.apache.commons.math3.linear.DiagonalMatrix) IOException(java.io.IOException) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Aggregations

DiagonalMatrix (org.apache.commons.math3.linear.DiagonalMatrix)22 LeastSquaresBuilder (org.apache.commons.math3.fitting.leastsquares.LeastSquaresBuilder)18 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)18 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)18 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)18 ConvergenceException (org.apache.commons.math3.exception.ConvergenceException)15 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)15 MultivariateMatrixFunction (org.apache.commons.math3.analysis.MultivariateMatrixFunction)9 PointValuePair (org.apache.commons.math3.optim.PointValuePair)8 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)5 IOException (java.io.IOException)4 InitialGuess (org.apache.commons.math3.optim.InitialGuess)4 MaxEval (org.apache.commons.math3.optim.MaxEval)4 OptimizationData (org.apache.commons.math3.optim.OptimizationData)4 SimpleBounds (org.apache.commons.math3.optim.SimpleBounds)4 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)4 CMAESOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer)4 SVD (org.broadinstitute.hellbender.utils.svd.SVD)4 RealMatrix (org.apache.commons.math3.linear.RealMatrix)3 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)3