Package net.finmath.optimizer


package net.finmath.optimizer

This package provides classes with numerical algorithm for optimization of an objective function and a factory to easy construction of the optimizers.

Why a package for optimization algorithms?

Given that there are a variety of numerical libraries featuring optimization algorithms (e.g., Apache Commons Math), why do we provide a package inside finmath lib? This packages provides a unified interface for passing optimizers to other classes via an OptimizationFactoryInterface and an Optimizer and an Optimizer.ObjectiveFunction. This allows use of different optimization frameworks without bothering with the framework specific constructors and framework specific definitions of objective functions.

A class implementing the OptimizationFactoryInterface allows the specification of parameters specific to the optimizer, but leave the specification of the initial values and the objective function still open. It provides a factory method which takes the objective function and initial values as parameters and constructs the specific optimizer by returning an object implementing Optimizer.

Example

The following code is an example of an optimization problem using an OptimizerFactory as argument.


        public void testOptimizerWithRosenbrockFunction(OptimizerFactory optimizerFactory) throws SolverException {
                Optimizer.ObjectiveFunction objectiveFunction = new Optimizer.ObjectiveFunction() {
                                public void setValues(double[] parameters, double[] values) {
                                        values[0] = 10.0 * (parameters[1] - parameters[0]*parameters[0]);
                                        values[1] = 1.0 - parameters[0];
                                }
                };

                Optimizer optimizer = optimizerFactory.getOptimizer(
                                objectiveFunction,
                                new double[] { 0.5, 0.5 }, // initialParameters
                                new double[] { Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY }, // lowerBound
                                new double[] { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }, // upperBound
                                new double[] { 0.5, 0.5 }, // parameterStep
                                new double[] { 0.0, 0.0 }); // targetValues

                optimizer.run();

                double[] bestParameters = optimizer.getBestFitParameters();
                System.out.println("The solver " + optimizer.getClass() + " for problem 'Rosebrock' required " + optimizer.getIterations() + " iterations. Accuracy is " + optimizer.getRootMeanSquaredError() + ". The best fit parameters are:");
                for (int i = 0; i < bestParameters.length; i++) System.out.println("\tparameter[" + i + "]: " + bestParameters[i]);

                System.out.println();

                Assert.assertTrue(Math.abs(bestParameters[0] - 1.0) < 1E-10);
                Assert.assertTrue(Math.abs(bestParameters[1] - 1.0) < 1E-10);
        }
Now, we may pass different optimizers to the optimization problem. For example the CMA-ES solver from commons math:
        public void testRosenbrockFunctionWithCMAES() throws SolverException {

                OptimizerFactory optimizerFactory = new OptimizerFactoryCMAES(0.0, 200);
                this.testOptimizerWithRosenbrockFunction(optimizerFactory);
        }
Or the multi-threadded Levenberg Marquardt solver (using two threads) from finmath-lib:
        public void testRosenbrockFunctionWithLevenbergMarquard() throws SolverException {

                OptimizerFactory optimizerFactory = new OptimizerFactoryLevenbergMarquardt(200, 2);
                this.testOptimizerWithRosenbrockFunction(optimizerFactory);
        }
Optimization algorithms

The package also contains an implementation of the Levenberg Marquardt optimizer, a multi-dimensional non-linear least-square. In addition we provide wrappers (via specific OptimizationFactoryInterface implementations) to some optimizers from Apache commons-math.