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