You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2013/08/23 16:12:07 UTC
svn commit: r1516854 -
/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java
Author: erans
Date: Fri Aug 23 14:12:06 2013
New Revision: 1516854
URL: http://svn.apache.org/r1516854
Log:
MATH-1014
New class "AbstractCurveFitter" as replacement for "CurveFitter".
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java (with props)
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java?rev=1516854&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java Fri Aug 23 14:12:06 2013
@@ -0,0 +1,136 @@
+/*
+ * 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 java.util.Collection;
+import org.apache.commons.math3.analysis.MultivariateVectorFunction;
+import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
+import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
+import org.apache.commons.math3.optim.PointVectorValuePair;
+import org.apache.commons.math3.optim.AbstractOptimizer;
+
+/**
+ * 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 storing observed data
+ * points, and calling the {@link #fit()} method for obtaining the
+ * parameters.
+ * The problem setup, such as the choice of optimization algorithm
+ * for fitting a specific function is delegated to subclasses.
+ *
+ * @param <OPTIM> Optimizer to use for the fit.
+ *
+ * @version $Id$
+ * @since 3.3
+ */
+public abstract class AbstractCurveFitter<OPTIM extends AbstractOptimizer<PointVectorValuePair, OPTIM>> {
+ /**
+ * 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(points).optimize().getPoint();
+ }
+
+ /**
+ * Creates an optimizer set up to fit the appropriate curve.
+ *
+ * @param points Sample points.
+ * @return the optimizer to use for fitting the curve to the
+ * given {@code points}.
+ */
+ protected abstract OPTIM getOptimizer(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() {
+ 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;
+ }
+ };
+ }
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/AbstractCurveFitter.java
------------------------------------------------------------------------------
svn:keywords = Id Revision