I frequently need to do non-linear fitting of data, and have never been satisfied with the tools I found. I want to work in a high-level, general numerical analysis framework. All I ask for is a powerful, convenient, robust, flexible, free, etc. implementation. The levmar library (which uses the lapack and blas libraries) is the only C library that I found that appears to be featureful (eg, box and linear constraints ), efficient (uses lapack and blas and has conditionals in the code to choose different methods depending on the size of matrices, etc.), well organized and easy to use, and free. I wrote this PDL module to try to fill the remaining requirements. The interface completely covers the functionality of levmar 2.1.3 (and provides some more).
The fit function can be supplied as a perl function, or as a string containing a C function which is transparantly compiled and linked, in which case the the entire fit procedure is done in compiled C. (In some textbook optimization problems that have a small amount of data, but require a very large number of iterations to converge, this provides more than an order of magnitude increase in speed.) In addition, the perl module includes a very simple pre-processor language that is as fast as C, but much more concise. Here is an example that fits data arrays ($x,$t) to a gaussian
$result = levmar($params,$x,$t, FUNC =>
' function
x = p0 * exp(-t*t * p1);
');
print $result->{P};
The hash $result contains the optimized parameters ( P ) and a lot of other information, such as the covariance matrix, and the values of all the quantities relevant to the stopping criteria. If levmar is given additional arguments specifying linear or box constraints or an analytic jacobian, or single or double precision arguments, the appropriate C algorithms are chosen transparantly. The implementation is designed to provide the simplest black box possible as well as maximum control if desired.
Scipy has an optimization module. Because I can't find good online documentation, I am not sure how it compares to this PDL module. At any rate, this is the only other software I know of that might fill the requirements listed above. New: pylevmar python binding to levmar. It seems that this library is becoming popular ...
You can get some information on modules that are not part of the pdl distribution with this CPAN query, but a few old packages that have been integrated into PDL or don't build with the current version or whatever are also listed there as well. Its a good starting point.
Linux/Unix: You must have PDL installed (and working) on your machine to build this module. There are a few dependencies that are standard packages in all linux distributions and are probably most unix systems. Once these packages (lapack and blas) are installed the build should be automatic, at least under linux. Feedback is welcome-- send it to the pdl-porters list (see the PDL main page) or send a message to mylastname at physics arizona edu. (that's an old, heavily spammed address; if have a more recent one, it's preferable.) You must put the word 'levmar' in the subject or I will not get the message.
August 2008 -- the debian packages are out of date. Use the link to CPAN above to get the generic package.
On a debian system, installing PDL and the dependencies and installing and testing this module would look like this:
apt-get install pdl lapack3-dev refblas3-dev tar xzf PDL-Fit-Levmar-x.xxx.tar.gz cd PDL-Fit-Levmar-x.xxx perl Makefile.PL && make && make test make install
Chris Marshall has gotten a cygwin port of PDL working (apparantly the cvs version of PDL builds and runs under cygwin) This probably means that using PDL and building modules for Win32 will be easier.
I looked at the draft of the PDL book . Considering the dearth of good PDL documentation, it seems a shame that all that hard work is currently hidden. I fixed a few things (formatting, typos, ...) and I added a little content. The changes are listed in a Changes file in the source tree. In particular, before you could only build pdf from the source. Now you can build ps, pdf, and html from the same source. I actually originally wanted an html version for my own use. Here is the PDL book online in html. Here is the book as gzipped html. Here is the source (15 MB) .