commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luc Maisonobe <>
Subject Re: [MATH] Need help on math libraries for curve generation
Date Thu, 21 Jan 2010 19:24:32 GMT a écrit :
> We are evaluating the apache math library (
for use on one of projects. In this project we need to generate curves based on the following
> 2DCurve3rdOrderXPolynomial
> 2DCurveExponentialX
> 2DCurveNaturalLogX
> 2DCurveSquareRootX
> 2DCurveTimeConstantX
> 2DCurveExponentialDecayX
> 2DCurveLogarithmicDecayX
> 3DCurve4thOrderXPolynomial
> 3DCurveExponentialX
> 3DCurveNaturalLogX
> 3DCurveSquareRootX
> 3DCurveTimeConstantX
> 3DCurve3rdOrderZTimes4thOrderX
> 3DCurveExponentialDecayX
> 3DCurveLogarithmicDecayX
> 3DCurveExponentialDecayZ
> 3DCurveLogarithmicDecayZ
> 3DCurveHyprebolicDecayX
> For each function generated from data we also need:
> Coefficient of Determination
> Sum of Squares
> Standard Error of Regression
> Does anyone have experience with this library to direct us to which classes can be used
to handle these requirements?

The classes you need are located in the
org.apache.commons.math.optimization.fitting package. The CurveFitter
class is the most general fitter. The PolynomialFitter class is
specialized for polynomials. Both classes need an underlying optimizer
as the engine that will do the real work (the classes are mainly
wrappers that convert a curve fitting problem into a general least
squares problem). Once built, you add the observed points in a loop.
When all observed points have been added, the fit methods should be
called to perform the fitting and they will provide you the coefficients
(for Polynomial, you will directly get a PolynomialFunction instance
that holds the coefficients.

Neither class will directly provide the residuals, sum of squares or
standard deviation. However, you can get them if you use one of the
least squares optimizers as the engine when you build the curve fitter.
This would be something along the following lines:

  // set up the optimizer and the fitter
  LevenbergMarquardtOptimizer optimizer =
    new LevenbergMarquardtOptimizer();
  CurveFitter fitter = new CurveFitter(optimizer);
  for (int i = 0; i < n; ++i) {
    fitter.addObservedPoint(x[i], y[i]);

  // you should provide an implementation for your function
  ParametricRealFunction myFunction =
    new 2DCurveNaturalLogX();

  // perform the fitting
  double[] fittingParameters =, initialGuess);
  double rms = optimizer.getRMS();
  double[][] cov = optimizer.getCovariances();
  double[] e = optimizer.guessParameterErrors();

For 3D curve fitting, you should create you own fitters, probably using
the 2D CurveFitter class as a guide.


> Thanks
> Roger Ball

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message