Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving nonlinear equations numerically

I need to solve nonlinear minimization (least residual squares of N unknowns) problems in my Java program. The usual way to solve these is the Levenberg-Marquardt algorithm. I have a couple of questions

  • Does anybody have experience on the different LM implementations available? There exist slightly different flavors of LM, and I've heard that the exact implementation of the algorithm has a major effect on the its numerical stability. My functions are pretty well-behaved so this will probably not be a problem, but of course I'd like to choose one of the better alternatives. Here are some alternatives I've found:

    • FPL Statistics Group's Nonlinear Optimization Java Package. This includes a Java translation of the classic Fortran MINPACK routines.

    • JLAPACK, another Fortran translation.

    • Optimization Algorithm Toolkit.

    • Javanumerics.

    • Some Python implementation. Pure Python would be fine, since it can be compiled to Java with jythonc.

  • Are there any commonly used heuristics to do the initial guess that LM requires?

  • In my application I need to set some constraints on the solution, but luckily they are simple: I just require that the solutions (in order to be physical solutions) are nonnegative. Slightly negative solutions are result of measurement inaccuracies in the data, and should obviously be zero. I was thinking to use "regular" LM but iterate so that if some of the unknowns becomes negative, I set it to zero and resolve the rest from that. Real mathematicians will probably laugh at me, but do you think that this could work?

Thanks for any opinions!

Update: This is not rocket science, the number of parameters to solve (N) is at most 5 and the data sets are barely big enough to make solving possible, so I believe Java is quite efficient enough to solve this. And I believe that this problem has been solved numerous times by clever applied mathematicians, so I'm just looking for some ready solution rather than cooking my own. E.g. Scipy.optimize.minpack.leastsq would probably be fine if it was pure Python..

like image 677
Joonas Pulakka Avatar asked Feb 12 '09 07:02

Joonas Pulakka


People also ask

What are the 3 methods in solving system of nonlinear equations?

Solving Systems of Nonlinear EquationsSubstitution. Elimination. Using a Combination of methods. Using absolute value.

Which method is best for solving nonlinear equations?

The Newton method is one of the best techniques to solve nonlinear equations and optimization problems.


4 Answers

The closer your initial guess is to the solution, the faster you'll converge.

You said it was a non-linear problem. You can do a least squares solution that's linearized. Maybe you can use that solution as a first guess. A few non-linear iterations will tell you something about how good or bad an assumption that is.

Another idea would be trying another optimization algorithm. Genetic and ant colony algorithms can be a good choice if you can run them on many CPUs. They also don't require continuous derivatives, so they're nice if you have discrete, discontinuous data.

like image 199
duffymo Avatar answered Oct 22 '22 09:10

duffymo


You should not use an unconstrained solver if your problem has constraints. For instance if know that some of your variables must be nonnegative you should tell this to your solver.

If you are happy to use Scipy, I would recommend scipy.optimize.fmin_l_bfgs_b You can place simple bounds on your variables with L-BFGS-B.

Note that L-BFGS-B takes a general nonlinear objective function, not just a nonlinear least-squares problem.

like image 31
codehippo Avatar answered Oct 22 '22 09:10

codehippo


I agree with codehippo; I think that the best way to solve problems with constraints is to use algorithms which are specifically designed to deal with them. The L-BFGS-B algorithm should probably be a good solution in this case.

However, if using python's scipy.optimize.fmin_l_bfgs_b module is not a viable option in your case (because you are using Java), you can try using a library I have written: a Java wrapper for the original Fortran code of the L-BFGS-B algorithm. You can download it from http://www.mini.pw.edu.pl/~mkobos/programs/lbfgsb_wrapper and see if it matches your needs.

like image 40
Mateusz Kobos Avatar answered Oct 22 '22 08:10

Mateusz Kobos


The FPL package is quite reliable but has a few quirks (array access starts at 1) due to its very literal interpretation of the old fortran code. The LM method itself is quite reliable if your function is well behaved. A simple way to force non-negative constraints is to use the square of parameters instead of the parameters directly. This can introduce spurious solutions but for simple models, these solutions are easy to screen out.

There is code available for a "constrained" LM method. Look here http://www.physics.wisc.edu/~craigm/idl/fitting.html for mpfit. There is a python (relies on Numeric unfortunately) and a C version. The LM method is around 1500 lines of code, so you might be inclined to port the C to Java. In fact, the "constrained" LM method is not much different than the method you envisioned. In mpfit, the code adjusts the step size relative to bounds on the variables. I've had good results with mpfit as well.

I don't have that much experience with BFGS, but the code is much more complex and I've never been clear on the licensing of the code.

Good luck.

like image 1
kpatvt Avatar answered Oct 22 '22 09:10

kpatvt