001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.fitting;
018
019import java.util.Collection;
020
021import org.apache.commons.math3.analysis.MultivariateVectorFunction;
022import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
023import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
024import org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer;
025import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem;
026import org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer;
027
028/**
029 * Base class that contains common code for fitting parametric univariate
030 * real functions <code>y = f(p<sub>i</sub>;x)</code>, where {@code x} is
031 * the independent variable and the <code>p<sub>i</sub></code> are the
032 * <em>parameters</em>.
033 * <br/>
034 * A fitter will find the optimal values of the parameters by
035 * <em>fitting</em> the curve so it remains very close to a set of
036 * {@code N} observed points <code>(x<sub>k</sub>, y<sub>k</sub>)</code>,
037 * {@code 0 <= k < N}.
038 * <br/>
039 * An algorithm usually performs the fit by finding the parameter
040 * values that minimizes the objective function
041 * <pre><code>
042 *  &sum;y<sub>k</sub> - f(x<sub>k</sub>)<sup>2</sup>,
043 * </code></pre>
044 * which is actually a least-squares problem.
045 * This class contains boilerplate code for calling the
046 * {@link #fit(Collection)} method for obtaining the parameters.
047 * The problem setup, such as the choice of optimization algorithm
048 * for fitting a specific function is delegated to subclasses.
049 *
050 * @version $Id: AbstractCurveFitter.html 908881 2014-05-15 07:10:28Z luc $
051 * @since 3.3
052 */
053public abstract class AbstractCurveFitter {
054    /**
055     * Fits a curve.
056     * This method computes the coefficients of the curve that best
057     * fit the sample of observed points.
058     *
059     * @param points Observations.
060     * @return the fitted parameters.
061     */
062    public double[] fit(Collection<WeightedObservedPoint> points) {
063        // Perform the fit.
064        return getOptimizer().optimize(getProblem(points)).getPoint().toArray();
065    }
066
067    /**
068     * Creates an optimizer set up to fit the appropriate curve.
069     * <p>
070     * The default implementation uses a {@link LevenbergMarquardtOptimizer
071     * Levenberg-Marquardt} optimizer.
072     * </p>
073     * @return the optimizer to use for fitting the curve to the
074     * given {@code points}.
075     */
076    protected LeastSquaresOptimizer getOptimizer() {
077        return new LevenbergMarquardtOptimizer();
078    }
079
080    /**
081     * Creates a least squares problem corresponding to the appropriate curve.
082     *
083     * @param points Sample points.
084     * @return the least squares problem to use for fitting the curve to the
085     * given {@code points}.
086     */
087    protected abstract LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> points);
088
089    /**
090     * Vector function for computing function theoretical values.
091     */
092    protected static class TheoreticalValuesFunction {
093        /** Function to fit. */
094        private final ParametricUnivariateFunction f;
095        /** Observations. */
096        private final double[] points;
097
098        /**
099         * @param f function to fit.
100         * @param observations Observations.
101         */
102        public TheoreticalValuesFunction(final ParametricUnivariateFunction f,
103                                         final Collection<WeightedObservedPoint> observations) {
104            this.f = f;
105
106            final int len = observations.size();
107            this.points = new double[len];
108            int i = 0;
109            for (WeightedObservedPoint obs : observations) {
110                this.points[i++] = obs.getX();
111            }
112        }
113
114        /**
115         * @return the model function values.
116         */
117        public MultivariateVectorFunction getModelFunction() {
118            return new MultivariateVectorFunction() {
119                /** {@inheritDoc} */
120                public double[] value(double[] p) {
121                    final int len = points.length;
122                    final double[] values = new double[len];
123                    for (int i = 0; i < len; i++) {
124                        values[i] = f.value(points[i], p);
125                    }
126
127                    return values;
128                }
129            };
130        }
131
132        /**
133         * @return the model function Jacobian.
134         */
135        public MultivariateMatrixFunction getModelFunctionJacobian() {
136            return new MultivariateMatrixFunction() {
137                public double[][] value(double[] p) {
138                    final int len = points.length;
139                    final double[][] jacobian = new double[len][];
140                    for (int i = 0; i < len; i++) {
141                        jacobian[i] = f.gradient(points[i], p);
142                    }
143                    return jacobian;
144                }
145            };
146        }
147    }
148}