|
Java example source code file (LeastSquaresFactory.java)
The LeastSquaresFactory.java Java example source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.fitting.leastsquares; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.DiagonalMatrix; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.optim.AbstractOptimizationProblem; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.util.Pair; /** * A Factory for creating {@link LeastSquaresProblem}s. * * @since 3.3 */ public class LeastSquaresFactory { /** Prevent instantiation. */ private LeastSquaresFactory() {} /** * Create a {@link org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem} * from the given elements. There will be no weights applied (unit weights). * * @param model the model function. Produces the computed values. * @param observed the observed (target) values * @param start the initial guess. * @param weight the weight matrix * @param checker convergence checker * @param maxEvaluations the maximum number of times to evaluate the model * @param maxIterations the maximum number to times to iterate in the algorithm * @param lazyEvaluation Whether the call to {@link Evaluation#evaluate(RealVector)} * will defer the evaluation until access to the value is requested. * @param paramValidator Model parameters validator. * @return the specified General Least Squares problem. * * @since 3.4 */ public static LeastSquaresProblem create(final MultivariateJacobianFunction model, final RealVector observed, final RealVector start, final RealMatrix weight, final ConvergenceChecker<Evaluation> checker, final int maxEvaluations, final int maxIterations, final boolean lazyEvaluation, final ParameterValidator paramValidator) { final LeastSquaresProblem p = new LocalLeastSquaresProblem(model, observed, start, checker, maxEvaluations, maxIterations, lazyEvaluation, paramValidator); if (weight != null) { return weightMatrix(p, weight); } else { return p; } } /** * Create a {@link org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem} * from the given elements. There will be no weights applied (unit weights). * * @param model the model function. Produces the computed values. * @param observed the observed (target) values * @param start the initial guess. * @param checker convergence checker * @param maxEvaluations the maximum number of times to evaluate the model * @param maxIterations the maximum number to times to iterate in the algorithm * @return the specified General Least Squares problem. */ public static LeastSquaresProblem create(final MultivariateJacobianFunction model, final RealVector observed, final RealVector start, final ConvergenceChecker<Evaluation> checker, final int maxEvaluations, final int maxIterations) { return create(model, observed, start, null, checker, maxEvaluations, maxIterations, false, null); } /** * Create a {@link org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem} * from the given elements. * * @param model the model function. Produces the computed values. * @param observed the observed (target) values * @param start the initial guess. * @param weight the weight matrix * @param checker convergence checker * @param maxEvaluations the maximum number of times to evaluate the model * @param maxIterations the maximum number to times to iterate in the algorithm * @return the specified General Least Squares problem. */ public static LeastSquaresProblem create(final MultivariateJacobianFunction model, final RealVector observed, final RealVector start, final RealMatrix weight, final ConvergenceChecker<Evaluation> checker, final int maxEvaluations, final int maxIterations) { return weightMatrix(create(model, observed, start, checker, maxEvaluations, maxIterations), weight); } /** * Create a {@link org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem} * from the given elements. * <p> * This factory method is provided for continuity with previous interfaces. Newer * applications should use {@link #create(MultivariateJacobianFunction, RealVector, * RealVector, ConvergenceChecker, int, int)}, or {@link #create(MultivariateJacobianFunction, * RealVector, RealVector, RealMatrix, ConvergenceChecker, int, int)}. * * @param model the model function. Produces the computed values. * @param jacobian the jacobian of the model with respect to the parameters * @param observed the observed (target) values * @param start the initial guess. * @param weight the weight matrix * @param checker convergence checker * @param maxEvaluations the maximum number of times to evaluate the model * @param maxIterations the maximum number to times to iterate in the algorithm * @return the specified General Least Squares problem. */ public static LeastSquaresProblem create(final MultivariateVectorFunction model, final MultivariateMatrixFunction jacobian, final double[] observed, final double[] start, final RealMatrix weight, final ConvergenceChecker<Evaluation> checker, final int maxEvaluations, final int maxIterations) { return create(model(model, jacobian), new ArrayRealVector(observed, false), new ArrayRealVector(start, false), weight, checker, maxEvaluations, maxIterations); } /** * Apply a dense weight matrix to the {@link LeastSquaresProblem}. * * @param problem the unweighted problem * @param weights the matrix of weights * @return a new {@link LeastSquaresProblem} with the weights applied. The original * {@code problem} is not modified. */ public static LeastSquaresProblem weightMatrix(final LeastSquaresProblem problem, final RealMatrix weights) { final RealMatrix weightSquareRoot = squareRoot(weights); return new LeastSquaresAdapter(problem) { /** {@inheritDoc} */ @Override public Evaluation evaluate(final RealVector point) { return new DenseWeightedEvaluation(super.evaluate(point), weightSquareRoot); } }; } /** * Apply a diagonal weight matrix to the {@link LeastSquaresProblem}. * * @param problem the unweighted problem * @param weights the diagonal of the weight matrix * @return a new {@link LeastSquaresProblem} with the weights applied. The original * {@code problem} is not modified. */ public static LeastSquaresProblem weightDiagonal(final LeastSquaresProblem problem, final RealVector weights) { // TODO more efficient implementation return weightMatrix(problem, new DiagonalMatrix(weights.toArray())); } /** * Count the evaluations of a particular problem. The {@code counter} will be * incremented every time {@link LeastSquaresProblem#evaluate(RealVector)} is called on * the <em>returned problem. * * @param problem the problem to track. * @param counter the counter to increment. * @return a least squares problem that tracks evaluations */ public static LeastSquaresProblem countEvaluations(final LeastSquaresProblem problem, final Incrementor counter) { return new LeastSquaresAdapter(problem) { /** {@inheritDoc} */ @Override public Evaluation evaluate(final RealVector point) { counter.incrementCount(); return super.evaluate(point); } // Delegate the rest. }; } /** * View a convergence checker specified for a {@link PointVectorValuePair} as one * specified for an {@link Evaluation}. * * @param checker the convergence checker to adapt. * @return a convergence checker that delegates to {@code checker}. */ public static ConvergenceChecker<Evaluation> evaluationChecker(final ConvergenceChecker Other Java examples (source code examples)Here is a short list of links related to this Java LeastSquaresFactory.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.