[MATH] Need help on math libraries for curve generation

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[MATH] Need help on math libraries for curve generation

rfball2

We are evaluating the apache math library (http://commons.apache.org/math/index.html) for use on one of projects. In this project we need to generate curves based on the following functions:

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?

Thanks
Roger Ball

Reply | Threaded
Open this post in threaded view
|

Re: [MATH] Need help on math libraries for curve generation

Luc Maisonobe
[hidden email] a écrit :

> We are evaluating the apache math library (http://commons.apache.org/math/index.html) for use on one of projects. In this project we need to generate curves based on the following functions:
>
> 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 = fitter.fit(myFunction, 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.

Luc


> Thanks
> Roger Ball
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [MATH] Need help on math libraries for curve generation

rfball2
In reply to this post by rfball2
Luc
     Thanks for your comments. I have taken the 2DCurveExponentialX as a first attempt here. The basic equation is y = a + b*e^(c*x) (is the math e, natural exponential function). I have written the following implementation of the of the ParametricRealFunction for this, see below. Not having any experience with this type a implementation I did the best I could. However, I am getting this exception:

org.apache.commons.math.optimization.OptimizationException: unable to compute covariances: singular problem
I unfortunately do not have any idea what this means or how to remedy it. Your help is appreciated

Thanks
Roger

/**
    * implementation of ParametricRealFunction clase for
    *    y = a + be^(cx)
    */
   public static class TwoDCurveNaturalLogX implements ParametricRealFunction
   {
       /*
        *"double[] coeffs = must include at least 1 but not more than 3 coefficients."
        */
        @Override
        public double value(double x, double[] coeffs) throws FunctionEvaluationException
        {
           if(coeffs == null || coeffs.length == 0 || coeffs.length > 3)
           {
                if (coeffs != null)
                {
                    for (int ii=0; ii < coeffs.length; ii++)
                    {
                        //System.out.println("\t coeffs ["+ii+"]"+coeffs[ii]);
                    }
                }
                else
                {
                   //System.out.println("No coeffs were passed in");
                }
                throw new FunctionEvaluationException(coeffs);
           }
           double a = coeffs[0];
           double b = 0;
           double c = 0;
           if(coeffs.length >= 2)
               b = coeffs[1];
           if(coeffs.length >= 3)
               c = coeffs[2];
          double value = a + b*Math.pow(Math.E, (c*x));
          //System.out.println("\t value ["+value+"]");
          return value;
        }
        /*
         * derivative: y = b*c*e^(c*x)
         * double[] coeffs = must include at least 1 but not more than 3 coefficients."
         */
        @Override
        public double[] gradient(double x, double[] coeffs) throws FunctionEvaluationException {
           if(coeffs == null || coeffs.length ==0 || coeffs.length > 3)
           {
                throw new FunctionEvaluationException(coeffs);
           }
           System.out.println("\t coeffs length = ["+coeffs.length+"]");
           double a = coeffs[0];
           double b = 0;
           double c = 0;
           if(coeffs.length >= 2)
               b = coeffs[1];
           if(coeffs.length >= 3)
               c = coeffs[2];
           double gradient = b*c*Math.pow(Math.E, (c*x));
           double[] gradientVector = new double[3];
           gradientVector[0] = gradient;
           gradientVector[1] = 0;
           gradientVector[2] = 0;
           System.out.println("\t gradient ["+gradient+"]");
           return gradientVector;
        }
   }


Luc
________________________________
From: [hidden email]
Sent: Thursday, January 21, 2010 11:46 AM
To: [hidden email]
Subject: [MATH] Need help on math libraries for curve generation


We are evaluating the apache math library (http://commons.apache.org/math/index.html) for use on one of projects. In this project we need to generate curves based on the following functions:

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?

Thanks
Roger Ball

Reply | Threaded
Open this post in threaded view
|

Re: [MATH] Need help on math libraries for curve generation

Luc Maisonobe
[hidden email] a écrit :

> Luc
>      Thanks for your comments. I have taken the 2DCurveExponentialX as a first attempt here. The basic equation is y = a + b*e^(c*x) (is the math e, natural exponential function). I have written the following implementation of the of the ParametricRealFunction for this, see below. Not having any experience with this type a implementation I did the best I could. However, I am getting this exception:
>
> org.apache.commons.math.optimization.OptimizationException: unable to compute covariances: singular problem
> I unfortunately do not have any idea what this means or how to remedy it. Your help is appreciated
>
> Thanks
> Roger
>
> /**
>     * implementation of ParametricRealFunction clase for
>     *    y = a + be^(cx)
>     */
>    public static class TwoDCurveNaturalLogX implements ParametricRealFunction
>    {
>        /*
>         *"double[] coeffs = must include at least 1 but not more than 3 coefficients."
>         */
>         @Override
>         public double value(double x, double[] coeffs) throws FunctionEvaluationException
>         {
>            if(coeffs == null || coeffs.length == 0 || coeffs.length > 3)
>            {
>                 if (coeffs != null)
>                 {
>                     for (int ii=0; ii < coeffs.length; ii++)
>                     {
>                         //System.out.println("\t coeffs ["+ii+"]"+coeffs[ii]);
>                     }
>                 }
>                 else
>                 {
>                    //System.out.println("No coeffs were passed in");
>                 }
>                 throw new FunctionEvaluationException(coeffs);
>            }
>            double a = coeffs[0];
>            double b = 0;
>            double c = 0;
>            if(coeffs.length >= 2)
>                b = coeffs[1];
>            if(coeffs.length >= 3)
>                c = coeffs[2];
>           double value = a + b*Math.pow(Math.E, (c*x));
>           //System.out.println("\t value ["+value+"]");
>           return value;
>         }
>         /*
>          * derivative: y = b*c*e^(c*x)
>          * double[] coeffs = must include at least 1 but not more than 3 coefficients."
>          */
>         @Override
>         public double[] gradient(double x, double[] coeffs) throws FunctionEvaluationException {
>            if(coeffs == null || coeffs.length ==0 || coeffs.length > 3)
>            {
>                 throw new FunctionEvaluationException(coeffs);
>            }
>            System.out.println("\t coeffs length = ["+coeffs.length+"]");
>            double a = coeffs[0];
>            double b = 0;
>            double c = 0;
>            if(coeffs.length >= 2)
>                b = coeffs[1];
>            if(coeffs.length >= 3)
>                c = coeffs[2];
>            double gradient = b*c*Math.pow(Math.E, (c*x));
>            double[] gradientVector = new double[3];
>            gradientVector[0] = gradient;
>            gradientVector[1] = 0;
>            gradientVector[2] = 0;

The gradient is computed with respect to the coefficients (i.e. a, b and
c here), not with respect to the independant variable x. It also *must*
have the same length as the parameters array. So you should probably use:

public double[] gradient(double x, double[] coeffs)
  throws FunctionEvaluationException {
    final n = coeffs.length;
    final double b = (n > 1) ? coeffs[1] : 0;
    final double c = (n > 2) ? coeffs[2] : 0;
    double[] gradient = new double[n];
    gradient[0] = 1.0; // this is dy/da
    if (n > 1) {
       final double exp = Math.exp(c * x);
       gradient[1] = exp; // this is dy/db
       if (n > 2) {
         gradient[2] = b * x * exp; // this is dy/dc
      }
    }
    return gradient;
}

The reason you get a singular problem is proably because of your wrong
gradient, the optimizer thinks the problem does not depend on b and c
(you tell it dy/db = 0 and dy/dc = 0), so it has no way to know how to
choose b and c. The jacobian matrix has too many zeroes.

I also suggest to use Math.exp(c * x) rather than Math.pow(Math.E, c *
x), it is more stable numerically and probably faster.

hope this helps
Luc


>            System.out.println("\t gradient ["+gradient+"]");
>            return gradientVector;
>         }
>    }
>
>
> Luc
> ________________________________
> From: [hidden email]
> Sent: Thursday, January 21, 2010 11:46 AM
> To: [hidden email]
> Subject: [MATH] Need help on math libraries for curve generation
>
>
> We are evaluating the apache math library (http://commons.apache.org/math/index.html) for use on one of projects. In this project we need to generate curves based on the following functions:
>
> 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?
>
> Thanks
> Roger Ball
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [MATH] Need help on math libraries for curve generation

rfball2
In reply to this post by rfball2
Luc
    Thanks for tips on the parametric real function (see botton). Based on these changes and a data set I am getting the following results, see below. I am not quite sure how to use this information to compute:

Coefficient of Determination
Sum of Squares
Standard Error of Regression


Thanks

Roger


CurveFitter using LevenbergMarquardtOptimizer
   function is y = a + be^(cx)
   16 data points for "Trail Shaft Configuration"
         best coeff [0]291.14035683044796
         best coeff [1]182.57603828133813
         best coeff [2]0.09714949397797856
         Root Mean Square [26.34790434234611]
         Covariances [0][0]8.856485460793724
         Covariances [0][1]-5.701795559353768
         Covariances [0][2]0.0012611571197298362
         Covariances [1][0]-5.701795559353772
         Covariances [1][1]3.7453325583603876
         Covariances [1][2]-8.360156212815445E-4
         Covariances [2][0]0.0012611571197298384
         Covariances [2][1]-8.36015621281545E-4
         Covariances [2][2]1.8778882839946132E-7
         guessParametersError [0]86.98915260216266
         guessParametersError [1]56.56914127885704
         guessParametersError [2]0.012666868717084282
         chi Square [11107.393011734734]
------------------------------------------------------
Function value as 5 = 587.8975295919015, Actual = 576.0
Function value as 20 = 1565.445870365208, Actual = 1590.0

-------------------------------------------------------------------------

The gradient is computed with respect to the coefficients (i.e. a, b and
c here), not with respect to the independant variable x. It also *must*
have the same length as the parameters array. So you should probably use:

public double[] gradient(double x, double[] coeffs)
  throws FunctionEvaluationException {
    final n = coeffs.length;
    final double b = (n > 1) ? coeffs[1] : 0;
    final double c = (n > 2) ? coeffs[2] : 0;
    double[] gradient = new double[n];
    gradient[0] = 1.0; // this is dy/da
    if (n > 1) {
       final double exp = Math.exp(c * x);
       gradient[1] = exp; // this is dy/db
       if (n > 2) {
         gradient[2] = b * x * exp; // this is dy/dc
      }
    }
    return gradient;
}

The reason you get a singular problem is proably because of your wrong
gradient, the optimizer thinks the problem does not depend on b and c
(you tell it dy/db = 0 and dy/dc = 0), so it has no way to know how to
choose b and c. The jacobian matrix has too many zeroes.

I also suggest to use Math.exp(c * x) rather than Math.pow(Math.E, c *
x), it is more stable numerically and probably faster.

hope this helps
Luc




Roger Ball
________________________________
From: [hidden email]
Sent: Friday, January 22, 2010 10:59 AM
To: [hidden email]
Subject: RE: [MATH] Need help on math libraries for curve generation

Luc
     Thanks for your comments. I have taken the 2DCurveExponentialX as a first attempt here. The basic equation is y = a + b*e^(c*x) (is the math e, natural exponential function). I have written the following implementation of the of the ParametricRealFunction for this, see below. Not having any experience with this type a implementation I did the best I could. However, I am getting this exception:

org.apache.commons.math.optimization.OptimizationException: unable to compute covariances: singular problem
I unfortunately do not have any idea what this means or how to remedy it. Your help is appreciated

Thanks
Roger

/**
    * implementation of ParametricRealFunction clase for
    *    y = a + be^(cx)
    */
   public static class TwoDCurveNaturalLogX implements ParametricRealFunction
   {
       /*
        *"double[] coeffs = must include at least 1 but not more than 3 coefficients."
        */
        @Override
        public double value(double x, double[] coeffs) throws FunctionEvaluationException
        {
           if(coeffs == null || coeffs.length == 0 || coeffs.length > 3)
           {
                if (coeffs != null)
                {
                    for (int ii=0; ii < coeffs.length; ii++)
                    {
                        //System.out.println("\t coeffs ["+ii+"]"+coeffs[ii]);
                    }
                }
                else
                {
                   //System.out.println("No coeffs were passed in");
                }
                throw new FunctionEvaluationException(coeffs);
           }
           double a = coeffs[0];
           double b = 0;
           double c = 0;
           if(coeffs.length >= 2)
               b = coeffs[1];
           if(coeffs.length >= 3)
               c = coeffs[2];
          double value = a + b*Math.pow(Math.E, (c*x));
          //System.out.println("\t value ["+value+"]");
          return value;
        }
        /*
         * derivative: y = b*c*e^(c*x)
         * double[] coeffs = must include at least 1 but not more than 3 coefficients."
         */
        @Override
        public double[] gradient(double x, double[] coeffs) throws FunctionEvaluationException {
           if(coeffs == null || coeffs.length ==0 || coeffs.length > 3)
           {
                throw new FunctionEvaluationException(coeffs);
           }
           System.out.println("\t coeffs length = ["+coeffs.length+"]");
           double a = coeffs[0];
           double b = 0;
           double c = 0;
           if(coeffs.length >= 2)
               b = coeffs[1];
           if(coeffs.length >= 3)
               c = coeffs[2];
           double gradient = b*c*Math.pow(Math.E, (c*x));
           double[] gradientVector = new double[3];
           gradientVector[0] = gradient;
           gradientVector[1] = 0;
           gradientVector[2] = 0;
           System.out.println("\t gradient ["+gradient+"]");
           return gradientVector;
        }
   }


Luc
________________________________
From: [hidden email]
Sent: Thursday, January 21, 2010 11:46 AM
To: [hidden email]
Subject: [MATH] Need help on math libraries for curve generation


We are evaluating the apache math library (http://commons.apache.org/math/index.html) for use on one of projects. In this project we need to generate curves based on the following functions:

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?

Thanks
Roger Ball

Reply | Threaded
Open this post in threaded view
|

Re: [MATH] Need help on math libraries for curve generation

Luc Maisonobe
[hidden email] a écrit :

> Luc
>     Thanks for tips on the parametric real function (see botton). Based on these changes and a data set I am getting the following results, see below. I am not quite sure how to use this information to compute:
>
> Coefficient of Determination
> Sum of Squares
> Standard Error of Regression
>
>
> Thanks
>
> Roger
>
>
> CurveFitter using LevenbergMarquardtOptimizer
>    function is y = a + be^(cx)
>    16 data points for "Trail Shaft Configuration"
>          best coeff [0]291.14035683044796
>          best coeff [1]182.57603828133813
>          best coeff [2]0.09714949397797856

The coefficients above are the coefficients of your function. a =
291.140..., b = 182.576..., c = 0.0971...

>          Root Mean Square [26.34790434234611]

The RMS above is linked to the sum of squares (which is the objective
function the optimizer minimizes) by : rms = sqrt(sum / n) where n is
the number of data points. So you can retrieve sume by computing n * RMS^2.

>          Covariances [0][0]8.856485460793724
>          Covariances [0][1]-5.701795559353768
>          Covariances [0][2]0.0012611571197298362
>          Covariances [1][0]-5.701795559353772
>          Covariances [1][1]3.7453325583603876
>          Covariances [1][2]-8.360156212815445E-4
>          Covariances [2][0]0.0012611571197298384
>          Covariances [2][1]-8.36015621281545E-4
>          Covariances [2][2]1.8778882839946132E-7

The covariance above is linked to the standard error, but when you have
multiple parameters (here 3) it is a complete matrix.

Perhaps what you want is rather the chi-square value provided by
getChiSquare() ? I'm not sure about what it means, I don't know anything
about the statistical aspects of optimization algorithms.

Luc

>          guessParametersError [0]86.98915260216266
>          guessParametersError [1]56.56914127885704
>          guessParametersError [2]0.012666868717084282
>          chi Square [11107.393011734734]
> ------------------------------------------------------
> Function value as 5 = 587.8975295919015, Actual = 576.0
> Function value as 20 = 1565.445870365208, Actual = 1590.0
>
> -------------------------------------------------------------------------
>
> The gradient is computed with respect to the coefficients (i.e. a, b and
> c here), not with respect to the independant variable x. It also *must*
> have the same length as the parameters array. So you should probably use:
>
> public double[] gradient(double x, double[] coeffs)
>   throws FunctionEvaluationException {
>     final n = coeffs.length;
>     final double b = (n > 1) ? coeffs[1] : 0;
>     final double c = (n > 2) ? coeffs[2] : 0;
>     double[] gradient = new double[n];
>     gradient[0] = 1.0; // this is dy/da
>     if (n > 1) {
>        final double exp = Math.exp(c * x);
>        gradient[1] = exp; // this is dy/db
>        if (n > 2) {
>          gradient[2] = b * x * exp; // this is dy/dc
>       }
>     }
>     return gradient;
> }
>
> The reason you get a singular problem is proably because of your wrong
> gradient, the optimizer thinks the problem does not depend on b and c
> (you tell it dy/db = 0 and dy/dc = 0), so it has no way to know how to
> choose b and c. The jacobian matrix has too many zeroes.
>
> I also suggest to use Math.exp(c * x) rather than Math.pow(Math.E, c *
> x), it is more stable numerically and probably faster.
>
> hope this helps
> Luc
>
>
>
>
> Roger Ball
> ________________________________
> From: [hidden email]
> Sent: Friday, January 22, 2010 10:59 AM
> To: [hidden email]
> Subject: RE: [MATH] Need help on math libraries for curve generation
>
> Luc
>      Thanks for your comments. I have taken the 2DCurveExponentialX as a first attempt here. The basic equation is y = a + b*e^(c*x) (is the math e, natural exponential function). I have written the following implementation of the of the ParametricRealFunction for this, see below. Not having any experience with this type a implementation I did the best I could. However, I am getting this exception:
>
> org.apache.commons.math.optimization.OptimizationException: unable to compute covariances: singular problem
> I unfortunately do not have any idea what this means or how to remedy it. Your help is appreciated
>
> Thanks
> Roger
>
> /**
>     * implementation of ParametricRealFunction clase for
>     *    y = a + be^(cx)
>     */
>    public static class TwoDCurveNaturalLogX implements ParametricRealFunction
>    {
>        /*
>         *"double[] coeffs = must include at least 1 but not more than 3 coefficients."
>         */
>         @Override
>         public double value(double x, double[] coeffs) throws FunctionEvaluationException
>         {
>            if(coeffs == null || coeffs.length == 0 || coeffs.length > 3)
>            {
>                 if (coeffs != null)
>                 {
>                     for (int ii=0; ii < coeffs.length; ii++)
>                     {
>                         //System.out.println("\t coeffs ["+ii+"]"+coeffs[ii]);
>                     }
>                 }
>                 else
>                 {
>                    //System.out.println("No coeffs were passed in");
>                 }
>                 throw new FunctionEvaluationException(coeffs);
>            }
>            double a = coeffs[0];
>            double b = 0;
>            double c = 0;
>            if(coeffs.length >= 2)
>                b = coeffs[1];
>            if(coeffs.length >= 3)
>                c = coeffs[2];
>           double value = a + b*Math.pow(Math.E, (c*x));
>           //System.out.println("\t value ["+value+"]");
>           return value;
>         }
>         /*
>          * derivative: y = b*c*e^(c*x)
>          * double[] coeffs = must include at least 1 but not more than 3 coefficients."
>          */
>         @Override
>         public double[] gradient(double x, double[] coeffs) throws FunctionEvaluationException {
>            if(coeffs == null || coeffs.length ==0 || coeffs.length > 3)
>            {
>                 throw new FunctionEvaluationException(coeffs);
>            }
>            System.out.println("\t coeffs length = ["+coeffs.length+"]");
>            double a = coeffs[0];
>            double b = 0;
>            double c = 0;
>            if(coeffs.length >= 2)
>                b = coeffs[1];
>            if(coeffs.length >= 3)
>                c = coeffs[2];
>            double gradient = b*c*Math.pow(Math.E, (c*x));
>            double[] gradientVector = new double[3];
>            gradientVector[0] = gradient;
>            gradientVector[1] = 0;
>            gradientVector[2] = 0;
>            System.out.println("\t gradient ["+gradient+"]");
>            return gradientVector;
>         }
>    }
>
>
> Luc
> ________________________________
> From: [hidden email]
> Sent: Thursday, January 21, 2010 11:46 AM
> To: [hidden email]
> Subject: [MATH] Need help on math libraries for curve generation
>
>
> We are evaluating the apache math library (http://commons.apache.org/math/index.html) for use on one of projects. In this project we need to generate curves based on the following functions:
>
> 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?
>
> Thanks
> Roger Ball
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [MATH] Need help on math libraries for curve generation

Christiaan
This post has NOT been accepted by the mailing list yet.
Very useful thread! I am currently trying the natural exponential function fitter. If I generate y values for this function:
y = 20 + 10*e^(2*x)

my result is:
y = 1.040425114042751E13 + 21.13 * e^(1.7x)
RMS:2.895606396069334E25

which isn't a good fit. Any ideas if and how this can be improved?

initialGuess = new double[] {1,1,1};
Exp function is like this:

 public class NaturalExp2D implements ParametricRealFunction {

     @Override
     public double value(double x, double[] coeffs) throws FunctionEvaluationException {
    double a = coeffs[0];
    double b = coeffs[1];  
    double c = coeffs[2];
    double value = a + b*Math.exp(c * x);
    return value;
     }
     
     @Override
     public double[] gradient(double x, double[] coeffs) throws FunctionEvaluationException {
    double[] gradient = new double[3];
    double a = coeffs[0];
    double b = coeffs[1];  
    double c = coeffs[2];
    gradient[0] = 1.0; // this is dy/da
    final double exp = Math.exp(c * x);
    gradient[1] = exp; // this is dy/db
    gradient[2] = b * x * exp; // this is dy/dc
    return gradient;
     }
}

Generated data:
point: 1.0 - 93.89056099 function: 1.0404251140543246E13
point: 2.0 - 565.9815003 function: 1.0404251141061297E13
point: 3.0 - 4054.287935 function: 1.0404251143898256E13
point: 4.0 - 29829.57987 function: 1.0404251159434023E13
point: 5.0 - 220284.6579 function: 1.040425124451111E13
point: 6.0 - 1627567.914 function: 1.0404251710410879E13
point: 7.0 - 1.202606284E7 function: 1.0404254261774613E13
point: 8.0 - 8.886112521E7 function: 1.0404268233571447E13
point: 9.0 - 6.565997114E8 function: 1.040434474602795E13
point: 10.0 - 4.851651974E9 function: 1.040476374410452E13
point: 11.0 - 3.5849128481E10 function: 1.0407058264450576E13
point: 12.0 - 2.64891E11 function: 1.0419623533631664E13
point: 13.0 - 1.9573E12 function: 1.0488433553061055E13
point: 14.0 - 1.44626E13 function: 1.086525148713279E13
point: 15.0 - 1.06865E14 function: 1.292878462686391E13
point: 16.0 - 7.8963E14 function: 2.4229119971194812E13
point: 17.0 - 5.83462E15 function: 8.611209942795956E13
point: 18.0 - 4.31123E16 function: 4.249961262960237E14
point: 19.0 - 3.18559E17 function: 2.2807953246289075E15
point: 20.0 - 2.35385E18 function: 1.2443536532475274E16
point: 21.0 - 1.73927E19 function: 6.8096814619047912E16
point: 22.0 - 1.28516E20 function: 3.7286570506270682E17
point: 23.0 - 9.49612E20 function: 2.04184334260224461E18
point: 24.0 - 7.01674E21 function: 1.1181510946313906E19
point: 25.0 - 5.18471E22 function: 6.123222584353946E19
point: 26.0 - 3.83101E23 function: 3.353203185774627E20
point: 27.0 - 2.83075E24 function: 1.8362835462312472E21
point: 28.0 - 2.09166E25 function: 1.0055869403818766E22
point: 29.0 - 1.54554E26 function: 5.506802588140335E22

kind regards,
Christiaan