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 */ 017 018 package org.apache.commons.math.optimization.fitting; 019 020 import org.apache.commons.math.FunctionEvaluationException; 021 import org.apache.commons.math.analysis.polynomials.PolynomialFunction; 022 import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer; 023 import org.apache.commons.math.optimization.OptimizationException; 024 025 /** This class implements a curve fitting specialized for polynomials. 026 * <p>Polynomial fitting is a very simple case of curve fitting. The 027 * estimated coefficients are the polynomial coefficients. They are 028 * searched by a least square estimator.</p> 029 * @version $Revision: 1073270 $ $Date: 2011-02-22 10:19:27 +0100 (mar. 22 f??vr. 2011) $ 030 * @since 2.0 031 */ 032 033 public class PolynomialFitter { 034 035 /** Fitter for the coefficients. */ 036 private final CurveFitter fitter; 037 038 /** Polynomial degree. */ 039 private final int degree; 040 041 /** Simple constructor. 042 * <p>The polynomial fitter built this way are complete polynomials, 043 * ie. a n-degree polynomial has n+1 coefficients.</p> 044 * @param degree maximal degree of the polynomial 045 * @param optimizer optimizer to use for the fitting 046 */ 047 public PolynomialFitter(int degree, final DifferentiableMultivariateVectorialOptimizer optimizer) { 048 this.fitter = new CurveFitter(optimizer); 049 this.degree = degree; 050 } 051 052 /** Add an observed weighted (x,y) point to the sample. 053 * @param weight weight of the observed point in the fit 054 * @param x abscissa of the point 055 * @param y observed value of the point at x, after fitting we should 056 * have P(x) as close as possible to this value 057 */ 058 public void addObservedPoint(double weight, double x, double y) { 059 fitter.addObservedPoint(weight, x, y); 060 } 061 062 /** 063 * Remove all observations. 064 * @since 2.2 065 */ 066 public void clearObservations() { 067 fitter.clearObservations(); 068 } 069 070 /** Get the polynomial fitting the weighted (x, y) points. 071 * @return polynomial function best fitting the observed points 072 * @exception OptimizationException if the algorithm failed to converge 073 */ 074 public PolynomialFunction fit() throws OptimizationException { 075 try { 076 return new PolynomialFunction(fitter.fit(new ParametricPolynomial(), new double[degree + 1])); 077 } catch (FunctionEvaluationException fee) { 078 // should never happen 079 throw new RuntimeException(fee); 080 } 081 } 082 083 /** Dedicated parametric polynomial class. */ 084 private static class ParametricPolynomial implements ParametricRealFunction { 085 086 /** {@inheritDoc} */ 087 public double[] gradient(double x, double[] parameters) { 088 final double[] gradient = new double[parameters.length]; 089 double xn = 1.0; 090 for (int i = 0; i < parameters.length; ++i) { 091 gradient[i] = xn; 092 xn *= x; 093 } 094 return gradient; 095 } 096 097 /** {@inheritDoc} */ 098 public double value(final double x, final double[] parameters) { 099 double y = 0; 100 for (int i = parameters.length - 1; i >= 0; --i) { 101 y = y * x + parameters[i]; 102 } 103 return y; 104 } 105 106 } 107 108 }