Class LevenbergMarquardt
- java.lang.Object
-
- net.finmath.optimizer.LevenbergMarquardt
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, OptimizerInterface
public abstract class LevenbergMarquardt extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable, OptimizerInterface
This class implements a parallel 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(double[] parameters, double[][] derivatives)is not provided) may be performed in parallel by setting the parameternumberOfThreads.To use the solver inherit from it and implement the objective function as
setValues(double[] parameters, double[] 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 functionsetDerivatives(double[] parameters, double[][] derivatives), otherwise the solver will calculate the derivative via finite differences.To reject a point, it is allowed to set an element of
The following simple example finds a solution for the equationvaluestoDouble.NaNin the implementation ofsetValues(double[] parameters, double[] values). Put differently: The solver handles NaN values invaluesas 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.
Sample linear system of equations. 0.0 * x1 + 1.0 * x2 = 5.0 2.0 * x1 + 1.0 * x2 = 10.0
See the example in the main method below.LevenbergMarquardt optimizer = new LevenbergMarquardt() { // Override your objective function here public void setValues(double[] parameters, double[] values) { values[0] = parameters[0] * 0.0 + parameters[1]; values[1] = parameters[0] * 2.0 + parameters[1]; } }; // Set solver parameters optimizer.setInitialParameters(new double[] { 0, 0 }); optimizer.setWeights(new double[] { 1, 1 }); optimizer.setMaxIteration(100); optimizer.setTargetValues(new double[] { 5, 10 }); optimizer.run(); double[] bestParameters = optimizer.getBestFitParameters();The class can be initialized to use a multi-threaded valuation. If initialized this way the implementation of
Note: Iteration steps will be logged (java.util.logging) with LogLevel.FINEsetValuesmust 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.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.finmath.optimizer.OptimizerInterface
OptimizerInterface.ObjectiveFunction
-
-
Constructor Summary
Constructors Constructor and Description LevenbergMarquardt()Create a Levenberg-Marquardt solver.LevenbergMarquardt(double[] initialParameters, double[] targetValues, int maxIteration, java.util.concurrent.ExecutorService executorService)Create a Levenberg-Marquardt solver.LevenbergMarquardt(double[] initialParameters, double[] targetValues, int maxIteration, int numberOfThreads)Create a Levenberg-Marquardt solver.LevenbergMarquardt(int numberOfThreads)Create a Levenberg-Marquardt solver.LevenbergMarquardt(java.util.List<java.lang.Number> initialParameters, java.util.List<java.lang.Number> targetValues, int maxIteration, java.util.concurrent.ExecutorService executorService)Create a Levenberg-Marquardt solver.LevenbergMarquardt(java.util.List<java.lang.Number> initialParameters, java.util.List<java.lang.Number> targetValues, int maxIteration, int numberOfThreads)Create a Levenberg-Marquardt solver.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description LevenbergMarquardtclone()Create a clone of this LevenbergMarquardt optimizer.double[]getBestFitParameters()Get the best fit parameter vector.LevenbergMarquardtgetCloneWithModifiedTargetValues(double[] newTargetVaues, double[] newWeights, boolean isUseBestParametersAsInitialParameters)Create a clone of this LevenbergMarquardt optimizer with a new vector for the target values and weights.LevenbergMarquardtgetCloneWithModifiedTargetValues(java.util.List<java.lang.Number> newTargetVaues, java.util.List<java.lang.Number> newWeights, boolean isUseBestParametersAsInitialParameters)Create a clone of this LevenbergMarquardt optimizer with a new vector for the target values and weights.intgetIterations()Get the number of iterations.doublegetMeanSquaredError(double[] value)doublegetRootMeanSquaredError()static voidmain(java.lang.String[] args)voidrun()Runs the optimization.voidsetDerivatives(double[] parameters, double[][] derivatives)The derivative of the objective function.voidsetErrorMeanSquaredCurrent(double errorMeanSquaredCurrent)LevenbergMarquardtsetErrorTolerance(double errorTolerance)Set the error tolerance.LevenbergMarquardtsetInitialParameters(double[] initialParameters)Set the initial parameters for the solver.LevenbergMarquardtsetMaxIteration(int maxIteration)Set the maximum number of iterations to be performed until the solver gives up.LevenbergMarquardtsetParameterSteps(double[] parameterSteps)Set the parameter step for the solver.LevenbergMarquardtsetTargetValues(double[] targetValues)Set the target values for the solver.abstract voidsetValues(double[] parameters, double[] values)The objective function.LevenbergMarquardtsetWeights(double[] weights)Set the weight for the objective function.
-
-
-
Constructor Detail
-
LevenbergMarquardt
public LevenbergMarquardt(double[] initialParameters, double[] targetValues, int maxIteration, java.util.concurrent.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.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!
-
LevenbergMarquardt
public LevenbergMarquardt(double[] initialParameters, double[] targetValues, int maxIteration, int numberOfThreads)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!
-
LevenbergMarquardt
public LevenbergMarquardt(java.util.List<java.lang.Number> initialParameters, java.util.List<java.lang.Number> targetValues, int maxIteration, java.util.concurrent.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!
-
LevenbergMarquardt
public LevenbergMarquardt(java.util.List<java.lang.Number> initialParameters, java.util.List<java.lang.Number> targetValues, int maxIteration, int numberOfThreads)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!
-
LevenbergMarquardt
public LevenbergMarquardt()
Create a Levenberg-Marquardt solver.
-
LevenbergMarquardt
public LevenbergMarquardt(int numberOfThreads)
Create a Levenberg-Marquardt solver.- Parameters:
numberOfThreads- Maximum number of threads. Warning: If this number is larger than one, the implementation of setValues has to be thread safe!
-
-
Method Detail
-
main
public static void main(java.lang.String[] args) throws SolverException, java.lang.CloneNotSupportedException- Throws:
SolverExceptionjava.lang.CloneNotSupportedException
-
setInitialParameters
public LevenbergMarquardt setInitialParameters(double[] initialParameters)
Set the initial parameters for the solver.- Parameters:
initialParameters- The initial parameters.- Returns:
- A self reference.
-
setParameterSteps
public LevenbergMarquardt setParameterSteps(double[] parameterSteps)
Set the parameter step for the solver. The parameter step is used to evaluate the derivatives via finite differences, if analytic derivatives are not provided.- Parameters:
parameterSteps- The parameter step.- Returns:
- A self reference.
-
setTargetValues
public LevenbergMarquardt setTargetValues(double[] targetValues)
Set the target values for the solver. The solver will solver the equation weights * objectiveFunction = targetValues.- Parameters:
targetValues- The target values.- Returns:
- A self reference.
-
setMaxIteration
public LevenbergMarquardt setMaxIteration(int maxIteration)
Set the maximum number of iterations to be performed until the solver gives up.- Parameters:
maxIteration- The maximum number of iterations.- Returns:
- A self reference.
-
setWeights
public LevenbergMarquardt setWeights(double[] weights)
Set the weight for the objective function.- Parameters:
weights- The weights for the objective function.- Returns:
- A self reference.
-
setErrorTolerance
public LevenbergMarquardt setErrorTolerance(double errorTolerance)
Set the error tolerance. The solver considers the solution "found" if the error is not improving by this given error tolerance.- Parameters:
errorTolerance- The error tolerance.- Returns:
- A self reference.
-
getBestFitParameters
public double[] getBestFitParameters()
Description copied from interface:OptimizerInterfaceGet the best fit parameter vector.- Specified by:
getBestFitParametersin interfaceOptimizerInterface- Returns:
- The best fit parameter.
-
getRootMeanSquaredError
public double getRootMeanSquaredError()
- Specified by:
getRootMeanSquaredErrorin interfaceOptimizerInterface- Returns:
- the the root mean square error of achieved with the the best fit parameter
-
setErrorMeanSquaredCurrent
public void setErrorMeanSquaredCurrent(double errorMeanSquaredCurrent)
- Parameters:
errorMeanSquaredCurrent- the errorMeanSquaredCurrent to set
-
getIterations
public int getIterations()
Description copied from interface:OptimizerInterfaceGet the number of iterations.- Specified by:
getIterationsin interfaceOptimizerInterface- Returns:
- The number of iterations required
-
setValues
public abstract void setValues(double[] parameters, double[] values) throws SolverExceptionThe objective function. Override this method to implement your custom function.- Parameters:
parameters- Input value. The parameter vector.values- Output value. The vector of values f(i,parameters), i=1,...,n- Throws:
SolverException- Thrown if the valuation fails, specific cause may be available via thecause()method.
-
setDerivatives
public void setDerivatives(double[] parameters, double[][] derivatives) throws SolverExceptionThe derivative of the objective function. You may override this method if you like to implement your own derivative.- Parameters:
parameters- Input value. The parameter vector.derivatives- Output value, where derivatives[i][j] is d(value(j)) / d(parameters(i)- Throws:
SolverException- Thrown if the valuation fails, specific cause may be available via thecause()method.
-
run
public void run() throws SolverExceptionDescription copied from interface:OptimizerInterfaceRuns the optimization.- Specified by:
runin interfaceOptimizerInterface- Throws:
SolverException- Thrown if the valuation fails, specific cause may be available via thecause()method.
-
getMeanSquaredError
public double getMeanSquaredError(double[] value)
-
clone
public LevenbergMarquardt clone() throws java.lang.CloneNotSupportedException
Create a clone of this LevenbergMarquardt optimizer. The clone will use the same objective function than this implementation, i.e., the implementation ofsetValues(double[], double[])and that ofsetDerivatives(double[], double[][])is reused.- Overrides:
clonein classjava.lang.Object- Throws:
java.lang.CloneNotSupportedException
-
getCloneWithModifiedTargetValues
public LevenbergMarquardt getCloneWithModifiedTargetValues(double[] newTargetVaues, double[] newWeights, boolean isUseBestParametersAsInitialParameters) throws java.lang.CloneNotSupportedException
Create a clone of this LevenbergMarquardt optimizer with a new vector for the target values and weights. The clone will use the same objective function than this implementation, i.e., the implementation ofsetValues(double[], double[])and that ofsetDerivatives(double[], double[][])is reused. The initial values of the cloned optimizer will either be the original initial values of this object or the best parameters obtained by this optimizer, the latter is used only if this optimized signals adone().- Parameters:
newTargetVaues- New array of target values.newWeights- New array of weights.isUseBestParametersAsInitialParameters- If true and this optimizer is done(), then the clone will use this.getBestFitParameters()as initial parameters.- Returns:
- A new LevenbergMarquardt optimizer, cloning this one except modified target values and weights.
- Throws:
java.lang.CloneNotSupportedException- Thrown if this optimizer cannot be cloned.
-
getCloneWithModifiedTargetValues
public LevenbergMarquardt getCloneWithModifiedTargetValues(java.util.List<java.lang.Number> newTargetVaues, java.util.List<java.lang.Number> newWeights, boolean isUseBestParametersAsInitialParameters) throws java.lang.CloneNotSupportedException
Create a clone of this LevenbergMarquardt optimizer with a new vector for the target values and weights. The clone will use the same objective function than this implementation, i.e., the implementation ofsetValues(double[], double[])and that ofsetDerivatives(double[], double[][])is reused. The initial values of the cloned optimizer will either be the original initial values of this object or the best parameters obtained by this optimizer, the latter is used only if this optimized signals adone().- Parameters:
newTargetVaues- New list of target values.newWeights- New list of weights.isUseBestParametersAsInitialParameters- If true and this optimizer is done(), then the clone will use this.getBestFitParameters()as initial parameters.- Returns:
- A new LevenbergMarquardt optimizer, cloning this one except modified target values and weights.
- Throws:
java.lang.CloneNotSupportedException- Thrown if this optimizer cannot be cloned.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG