finMath lib documentation
net.finmath.optimizer

## Class StochasticPathwiseLevenbergMarquardt

• All Implemented Interfaces:
Serializable, Cloneable, StochasticOptimizerInterface

public abstract class StochasticPathwiseLevenbergMarquardt
extends Object
implements Serializable, Cloneable, StochasticOptimizerInterface
This class implements a stochastic Levenberg Marquardt non-linear least-squares fit algorithm.

The design avoids the need to define the objective function as a separate class. The objective function is defined by overriding a class method, see the sample code below.

The Levenberg-Marquardt solver is implemented in using multi-threading. The calculation of the derivatives (in case a specific implementation of setDerivatives(RandomVariableInterface[] parameters, RandomVariableInterface[][] derivatives) is not provided) may be performed in parallel by setting the parameter numberOfThreads.

To use the solver inherit from it and implement the objective function as setValues(RandomVariableInterface[] parameters, RandomVariableInterface[] values) where values has to be set to the value of the objective functions for the given parameters.
You may also provide an a derivative for your objective function by additionally overriding the function setDerivatives(RandomVariableInterface[] parameters, RandomVariableInterface[][] derivatives), otherwise the solver will calculate the derivative via finite differences.

To reject a point, it is allowed to set an element of values to Double.NaN in the implementation of setValues(RandomVariableInterface[] parameters, RandomVariableInterface[] values). Put differently: The solver handles NaN values in values as an error larger than the current one (regardless of the current error) and rejects the point.
Note, however, that is is an error if the initial parameter guess results in an NaN value. That is, the solver should be initialized with an initial parameter in an admissible region.

The following simple example finds a solution for the equation
 0.0 * x1 + 1.0 * x2 = 5.0 2.0 * x1 + 1.0 * x2 = 10.0

LevenbergMarquardt optimizer = new LevenbergMarquardt() {
// Override your objective function here
public void setValues(RandomVariableInterface[] parameters, RandomVariableInterface[] values) {
values[0] = parameters[0] * 0.0 + parameters[1];
values[1] = parameters[0] * 2.0 + parameters[1];
}
};

// Set solver parameters
optimizer.setInitialParameters(new RandomVariableInterface[] { 0, 0 });
optimizer.setWeights(new RandomVariableInterface[] { 1, 1 });
optimizer.setMaxIteration(100);
optimizer.setTargetValues(new RandomVariableInterface[] { 5, 10 });

optimizer.run();

RandomVariableInterface[] bestParameters = optimizer.getBestFitParameters();

See the example in the main method below.

The class can be initialized to use a multi-threaded valuation. If initialized this way the implementation of setValues must be thread-safe. The solver will evaluate the gradient of the value vector in parallel, i.e., use as many threads as the number of parameters.

Note: Iteration steps will be logged (java.util.logging) with LogLevel.FINE
Version:
1.6
Author:
Christian Fries
Serialized Form
• ### Constructor Detail

• #### StochasticPathwiseLevenbergMarquardt

public StochasticPathwiseLevenbergMarquardt(RandomVariableInterface[] initialParameters,
RandomVariableInterface[] targetValues,
RandomVariableInterface[] weights,
RandomVariableInterface[] parameterSteps,
int maxIteration,
RandomVariableInterface errorTolerance,
ExecutorService executorService)
Create a Levenberg-Marquardt solver.
Parameters:
initialParameters - Initial value for the parameters where the solver starts its search.
targetValues - Target values to achieve.
weights - Weights applied to the error.
parameterSteps - Step used for finite difference approximation.
maxIteration - Maximum number of iterations.
errorTolerance - Error tolerance / accuracy.
executorService - Executor to be used for concurrent valuation of the derivatives. This is only performed if setDerivative is not overwritten. Warning: The implementation of setValues has to be thread safe!
• #### StochasticPathwiseLevenbergMarquardt

public StochasticPathwiseLevenbergMarquardt(RandomVariableInterface[] initialParameters,
RandomVariableInterface[] targetValues,
int maxIteration,
Create a Levenberg-Marquardt solver.
Parameters:
initialParameters - Initial value for the parameters where the solver starts its search.
targetValues - Target values to achieve.
maxIteration - Maximum number of iterations.
numberOfThreads - Maximum number of threads. Warning: If this number is larger than one, the implementation of setValues has to be thread safe!
• #### StochasticPathwiseLevenbergMarquardt

public StochasticPathwiseLevenbergMarquardt(List<RandomVariableInterface> initialParameters,
List<RandomVariableInterface> targetValues,
int maxIteration,
ExecutorService executorService)
Create a Levenberg-Marquardt solver.
Parameters:
initialParameters - List of initial values for the parameters where the solver starts its search.
targetValues - List of target values to achieve.
maxIteration - Maximum number of iterations.
executorService - Executor to be used for concurrent valuation of the derivatives. This is only performed if setDerivative is not overwritten. Warning: The implementation of setValues has to be thread safe!
• #### StochasticPathwiseLevenbergMarquardt

public StochasticPathwiseLevenbergMarquardt(List<RandomVariableInterface> initialParameters,
List<RandomVariableInterface> targetValues,
int maxIteration,