diff options
author | Karl Shaffer <karlshaffer@google.com> | 2023-08-10 22:35:48 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-08-10 22:35:48 +0000 |
commit | 5484895ffd3d0c8337d159667cafc127c459f677 (patch) | |
tree | ace24ba4307d4978ee3134f7da671a77ad172da0 /src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java | |
parent | bbf9548f049f99fd8e5a593baae983532dd983f4 (diff) | |
parent | b3715644fba79ef08acd9a2e157d078865281767 (diff) | |
download | apache-commons-math-5484895ffd3d0c8337d159667cafc127c459f677.tar.gz |
Check-in commons-math 3.6.1 am: 1354beaf45 am: 0018f64b87 am: b3715644fb
Original change: https://android-review.googlesource.com/c/platform/external/apache-commons-math/+/2702413
Change-Id: I5ad9b2a0822d668b5b6a62933c6d4c1f0b802001
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java')
-rw-r--r-- | src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java b/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java new file mode 100644 index 0000000..c3f7239 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java @@ -0,0 +1,141 @@ +/* + * 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; + +import org.apache.commons.math3.analysis.MultivariateMatrixFunction; +import org.apache.commons.math3.analysis.MultivariateVectorFunction; +import org.apache.commons.math3.analysis.ParametricUnivariateFunction; +import org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer; +import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem; +import org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer; + +import java.util.Collection; + +/** + * Base class that contains common code for fitting parametric univariate real functions <code> + * y = f(p<sub>i</sub>;x)</code>, where {@code x} is the independent variable and the <code> + * p<sub>i</sub></code> are the <em>parameters</em>. <br> + * A fitter will find the optimal values of the parameters by <em>fitting</em> the curve so it + * remains very close to a set of {@code N} observed points <code>(x<sub>k</sub>, y<sub>k</sub>) + * </code>, {@code 0 <= k < N}. <br> + * An algorithm usually performs the fit by finding the parameter values that minimizes the + * objective function + * + * <pre><code> + * ∑y<sub>k</sub> - f(x<sub>k</sub>)<sup>2</sup>, + * </code></pre> + * + * which is actually a least-squares problem. This class contains boilerplate code for calling the + * {@link #fit(Collection)} method for obtaining the parameters. The problem setup, such as the + * choice of optimization algorithm for fitting a specific function is delegated to subclasses. + * + * @since 3.3 + */ +public abstract class AbstractCurveFitter { + /** + * Fits a curve. This method computes the coefficients of the curve that best fit the sample of + * observed points. + * + * @param points Observations. + * @return the fitted parameters. + */ + public double[] fit(Collection<WeightedObservedPoint> points) { + // Perform the fit. + return getOptimizer().optimize(getProblem(points)).getPoint().toArray(); + } + + /** + * Creates an optimizer set up to fit the appropriate curve. + * + * <p>The default implementation uses a {@link LevenbergMarquardtOptimizer Levenberg-Marquardt} + * optimizer. + * + * @return the optimizer to use for fitting the curve to the given {@code points}. + */ + protected LeastSquaresOptimizer getOptimizer() { + return new LevenbergMarquardtOptimizer(); + } + + /** + * Creates a least squares problem corresponding to the appropriate curve. + * + * @param points Sample points. + * @return the least squares problem to use for fitting the curve to the given {@code points}. + */ + protected abstract LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> points); + + /** Vector function for computing function theoretical values. */ + protected static class TheoreticalValuesFunction { + /** Function to fit. */ + private final ParametricUnivariateFunction f; + + /** Observations. */ + private final double[] points; + + /** + * @param f function to fit. + * @param observations Observations. + */ + public TheoreticalValuesFunction( + final ParametricUnivariateFunction f, + final Collection<WeightedObservedPoint> observations) { + this.f = f; + + final int len = observations.size(); + this.points = new double[len]; + int i = 0; + for (WeightedObservedPoint obs : observations) { + this.points[i++] = obs.getX(); + } + } + + /** + * @return the model function values. + */ + public MultivariateVectorFunction getModelFunction() { + return new MultivariateVectorFunction() { + /** {@inheritDoc} */ + public double[] value(double[] p) { + final int len = points.length; + final double[] values = new double[len]; + for (int i = 0; i < len; i++) { + values[i] = f.value(points[i], p); + } + + return values; + } + }; + } + + /** + * @return the model function Jacobian. + */ + public MultivariateMatrixFunction getModelFunctionJacobian() { + return new MultivariateMatrixFunction() { + /** {@inheritDoc} */ + public double[][] value(double[] p) { + final int len = points.length; + final double[][] jacobian = new double[len][]; + for (int i = 0; i < len; i++) { + jacobian[i] = f.gradient(points[i], p); + } + return jacobian; + } + }; + } + } +} |