Home | History | Annotate | Download | only in fitting
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package org.apache.commons.math.optimization.fitting;
     19 
     20 import org.apache.commons.math.FunctionEvaluationException;
     21 import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
     22 import org.apache.commons.math.optimization.OptimizationException;
     23 import org.apache.commons.math.optimization.fitting.CurveFitter;
     24 import org.apache.commons.math.optimization.fitting.WeightedObservedPoint;
     25 
     26 /**
     27  * Fits points to a Gaussian function (that is, a {@link GaussianFunction}).
     28  * <p>
     29  * Usage example:
     30  * <pre>
     31  *   GaussianFitter fitter = new GaussianFitter(
     32  *     new LevenbergMarquardtOptimizer());
     33  *   fitter.addObservedPoint(4.0254623,  531026.0);
     34  *   fitter.addObservedPoint(4.03128248, 984167.0);
     35  *   fitter.addObservedPoint(4.03839603, 1887233.0);
     36  *   fitter.addObservedPoint(4.04421621, 2687152.0);
     37  *   fitter.addObservedPoint(4.05132976, 3461228.0);
     38  *   fitter.addObservedPoint(4.05326982, 3580526.0);
     39  *   fitter.addObservedPoint(4.05779662, 3439750.0);
     40  *   fitter.addObservedPoint(4.0636168,  2877648.0);
     41  *   fitter.addObservedPoint(4.06943698, 2175960.0);
     42  *   fitter.addObservedPoint(4.07525716, 1447024.0);
     43  *   fitter.addObservedPoint(4.08237071, 717104.0);
     44  *   fitter.addObservedPoint(4.08366408, 620014.0);
     45  *  GaussianFunction fitFunction = fitter.fit();
     46  * </pre>
     47  *
     48  * @see ParametricGaussianFunction
     49  * @since 2.2
     50  * @version $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 fvr. 2011) $
     51  */
     52 public class GaussianFitter {
     53 
     54     /** Fitter used for fitting. */
     55     private final CurveFitter fitter;
     56 
     57     /**
     58      * Constructs an instance using the specified optimizer.
     59      *
     60      * @param optimizer optimizer to use for the fitting
     61      */
     62     public GaussianFitter(DifferentiableMultivariateVectorialOptimizer optimizer) {
     63         fitter = new CurveFitter(optimizer);
     64     }
     65 
     66     /**
     67      * Adds point (<code>x</code>, <code>y</code>) to list of observed points
     68      * with a weight of 1.0.
     69      *
     70      * @param x <tt>x</tt> point value
     71      * @param y <tt>y</tt> point value
     72      */
     73     public void addObservedPoint(double x, double y) {
     74         addObservedPoint(1.0, x, y);
     75     }
     76 
     77     /**
     78      * Adds point (<code>x</code>, <code>y</code>) to list of observed points
     79      * with a weight of <code>weight</code>.
     80      *
     81      * @param weight weight assigned to point
     82      * @param x <tt>x</tt> point value
     83      * @param y <tt>y</tt> point value
     84      */
     85     public void addObservedPoint(double weight, double x, double y) {
     86         fitter.addObservedPoint(weight, x, y);
     87     }
     88 
     89     /**
     90      * Fits Gaussian function to the observed points.
     91      *
     92      * @return Gaussian function best fitting the observed points
     93      *
     94      * @throws FunctionEvaluationException if <code>CurveFitter.fit</code> throws it
     95      * @throws OptimizationException if <code>CurveFitter.fit</code> throws it
     96      * @throws IllegalArgumentException if <code>CurveFitter.fit</code> throws it
     97      *
     98      * @see CurveFitter
     99      */
    100     public GaussianFunction fit() throws FunctionEvaluationException, OptimizationException {
    101         return new GaussianFunction(fitter.fit(new ParametricGaussianFunction(),
    102                                                createParametersGuesser(fitter.getObservations()).guess()));
    103     }
    104 
    105     /**
    106      * Factory method to create a <code>GaussianParametersGuesser</code>
    107      * instance initialized with the specified observations.
    108      *
    109      * @param observations points used to initialize the created
    110      *        <code>GaussianParametersGuesser</code> instance
    111      *
    112      * @return new <code>GaussianParametersGuesser</code> instance
    113      */
    114     protected GaussianParametersGuesser createParametersGuesser(WeightedObservedPoint[] observations) {
    115         return new GaussianParametersGuesser(observations);
    116     }
    117 }
    118