- java.lang.Object
-
- net.finmath.optimizer.StochasticPathwiseLevenbergMarquardt
-
- net.finmath.optimizer.StochasticPathwiseLevenbergMarquardtAD
-
- All Implemented Interfaces:
Serializable
,Cloneable
,StochasticOptimizer
public abstract class StochasticPathwiseLevenbergMarquardtAD extends StochasticPathwiseLevenbergMarquardt
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(RandomVariable[] parameters, RandomVariable[][] 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(RandomVariable[] parameters, RandomVariable[] 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(RandomVariable[] parameters, RandomVariable[][] 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 equationvalues
toDouble.NaN
in the implementation ofsetValues(RandomVariable[] parameters, RandomVariable[] values)
. Put differently: The solver handles NaN values invalues
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.
Sample linear system of equations. 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(RandomVariable[] parameters, RandomVariable[] values) { values[0] = parameters[0] * 0.0 + parameters[1]; values[1] = parameters[0] * 2.0 + parameters[1]; } }; // Set solver parameters optimizer.setInitialParameters(new RandomVariable[] { 0, 0 }); optimizer.setWeights(new RandomVariable[] { 1, 1 }); optimizer.setMaxIteration(100); optimizer.setTargetValues(new RandomVariable[] { 5, 10 }); optimizer.run(); RandomVariable[] 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.FINEsetValues
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.- Version:
- 1.6
- Author:
- Christian Fries
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.finmath.optimizer.StochasticOptimizer
StochasticOptimizer.ObjectiveFunction
-
-
Constructor Summary
Constructors Constructor Description StochasticPathwiseLevenbergMarquardtAD(List<RandomVariable> initialParameters, List<RandomVariable> targetValues, int maxIteration, int numberOfThreads)
StochasticPathwiseLevenbergMarquardtAD(List<RandomVariable> initialParameters, List<RandomVariable> targetValues, int maxIteration, ExecutorService executorService)
StochasticPathwiseLevenbergMarquardtAD(RandomVariable[] initialParameters, RandomVariable[] targetValues, int maxIteration, int numberOfThreads)
StochasticPathwiseLevenbergMarquardtAD(RandomVariable[] initialParameters, RandomVariable[] targetValues, RandomVariable[] weights, RandomVariable[] parameterSteps, int maxIteration, RandomVariable errorTolerance, ExecutorService executorService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
prepareAndSetDerivatives(RandomVariable[] parameters, RandomVariable[] values, RandomVariable[][] derivatives)
protected void
prepareAndSetValues(RandomVariable[] parameters, RandomVariable[] values)
-
Methods inherited from class net.finmath.optimizer.StochasticPathwiseLevenbergMarquardt
clone, getBestFitParameters, getCloneWithModifiedTargetValues, getCloneWithModifiedTargetValues, getIterations, getLambda, getLambdaDivisor, getLambdaMultiplicator, getMeanSquaredError, getRootMeanSquaredError, main, run, setDerivatives, setErrorMeanSquaredCurrent, setLambda, setLambdaDivisor, setLambdaMultiplicator, setValues
-
-
-
-
Constructor Detail
-
StochasticPathwiseLevenbergMarquardtAD
public StochasticPathwiseLevenbergMarquardtAD(List<RandomVariable> initialParameters, List<RandomVariable> targetValues, int maxIteration, ExecutorService executorService)
-
StochasticPathwiseLevenbergMarquardtAD
public StochasticPathwiseLevenbergMarquardtAD(List<RandomVariable> initialParameters, List<RandomVariable> targetValues, int maxIteration, int numberOfThreads)
-
StochasticPathwiseLevenbergMarquardtAD
public StochasticPathwiseLevenbergMarquardtAD(RandomVariable[] initialParameters, RandomVariable[] targetValues, int maxIteration, int numberOfThreads)
-
StochasticPathwiseLevenbergMarquardtAD
public StochasticPathwiseLevenbergMarquardtAD(RandomVariable[] initialParameters, RandomVariable[] targetValues, RandomVariable[] weights, RandomVariable[] parameterSteps, int maxIteration, RandomVariable errorTolerance, ExecutorService executorService)
-
-
Method Detail
-
prepareAndSetValues
protected void prepareAndSetValues(RandomVariable[] parameters, RandomVariable[] values) throws SolverException
- Overrides:
prepareAndSetValues
in classStochasticPathwiseLevenbergMarquardt
- Throws:
SolverException
-
prepareAndSetDerivatives
protected void prepareAndSetDerivatives(RandomVariable[] parameters, RandomVariable[] values, RandomVariable[][] derivatives) throws SolverException
- Overrides:
prepareAndSetDerivatives
in classStochasticPathwiseLevenbergMarquardt
- Throws:
SolverException
-
-