Optimization.Marquardt Method

Overload List

#SignatureDescription
1function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; IHess: TMtx; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tol: Double; GradTol: Double; Lambda0: Double): Integer;Minimizes the function of several variables by using the Marquardt optimization method with no log.
2function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; IHess: TMtx; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tol: Double; GradTol: Double; Lambda0: Double; Verbose: TStrings): Integer;Minimizes the function of several variables by using the Marquardt optimization algorithm.
3function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; const FloatPrecision: TMtxFloatPrecision): Integer;Minimizes the function of several variables by using the Marquardt optimization method with no log.
4function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision): Integer;Minimizes the function of several variables by using the Marquardt optimization method with no log.

Overload 1: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; IHess: TMtx; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tol: Double; GradTol: Double; Lambda0: Double): Integer;

Minimizes the function of several variables by using the Marquardt optimization method with no log.

#NameTypeDescription
1FunTRealFunction
2GradHessTGradHess
3ParsTDoubleArray
4ConstsTDoubleArray
5ObjConstTObjectArray
6FMinDouble
7IHessTMtxsource TMtx
8StopReasonTOptStopReason
9FloatPrecisionTMtxFloatPrecision
10MaxIterInteger
11TolDoublescalar
12GradTolDoublescalar
13Lambda0Doublescalar

Returns: Int32

Overload 2: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; IHess: TMtx; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tol: Double; GradTol: Double; Lambda0: Double; Verbose: TStrings): Integer;

Minimizes the function of several variables by using the Marquardt optimization algorithm.

#NameDescription
1FunReal function (must be of TRealFunction type) to be minimized.
2GradHessThe gradient and Hessian procedure (must be of TGradHess type), used for calculating the gradient and Hessian matrix.
3ParsStores the initial estimates for parameters (minimum estimate). After the call to routine returns adjusted calculated values (minimum position).
4ConstsAdditional Fun constant parameteres (can be/is usually nil).
5ObjConstAdditional Fun constant parameteres (can be/is usually nil).
6FloatPrecisionSpecifies the floating point precision to be used by the routine.
7FMinReturns function value at minimum.
8IHessReturns inverse Hessian matrix.
9StopReasonReturns reason why minimum search stopped (see TOptStopReason).
10MaxIterMaximum allowed numer of minimum search iterations.
11TolDesired Pars - minimum position tolerance.
12GradTolMinimum allowed gradient C-Norm.
13Lambda0Initial lambda step, used in Marquardt algorithm.
14VerboseIf assigned, stores Fun, evaluated at each iteration step. Optionally, you can also pass TOptControl object to the Verbose parameter. This allows the optimization procedure to be interrupted from another thread and optionally also allows logging and iteration count monitoring.

Returns: Int32 - the number of iterations required to reach the solution(minimum) within given tolerance.

Remarks:

What it computes. Minimizes a smooth real function of several
variables with the Levenberg-Marquardt method, solving a damped Newton step (H+lambda I) delta=-g and adapting the damping lambda (started from Lambda0) between gradient-descent and Newton behaviour. It needs both the gradient and the Hessian via GradHess. The standard test objective is

f(x_0,x_1) = 100 (x_1 - x_0^2)^2 + (1 - x_0)^2

with global minimum f=0 at (1,1)(1,1), gradient

(d f)/(d x_0) = -400 (x_1 - x_0^2) x_0 - 2 (1 - x_0), (d f)/(d x_1) = 200 (x_1 - x_0^2)

and Hessian

H(f) = [ 1200 x_0^2 - 400 x_1 + 2  -400 x_0 ]
       [ -400 x_0                  200      ]

Domain. Pars holds the n starting coordinates;
GradHess returns the gradient and (symmetric) Hessian; MaxIter > 0; Tol,GradTol ≥ 0; Lambda0 > 0.

Defined behaviour. Returns the iteration count; Pars
holds the minimizer, FMin the minimum and IHess the inverse Hessian. StopReason reports the cause - TOptStopReason.OptResConverged/TOptStopReason.OptResSmallGrad on success, TOptStopReason.optNANValue if the objective returned NaN, or TOptStopReason.OptResBigLambda/TOptStopReason.OptResSmallLambda/ TOptStopReason.OptResMaxIter otherwise.

Examples
Uses MtxVec, Math387, Optimization;

function Banana(const Pars: TVec; const Consts: TVec; const PConsts: array of TObject): double;
begin
    Banana := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]);
end;

procedure GradHessBanana(Fun: TRealFunction; const Pars: TVec; const Consts: TVec; const PConsts: array of TObject; const Grad: TVec; const Hess: TMtx);
begin
    Grad[0] := -400*(Pars[1] - Sqr(Pars[0]))*Pars[0] - 2*(1 - Pars[0]);
    Grad[1] :=  200*(Pars[1] - Sqr(Pars[0]));
    Hess[0,0] := -400*Pars[1] + 1200*Sqr(Pars[0])+2;
    Hess[0,1] := -400*Pars[0];
    Hess[1,0] := Hess.Values[0,1]; // symmetric !
    Hess[1,1] := 200;
end;

procedure Example;
var Iters : integer;
Pars : Array [0..1] of double;
StopReason : TOptStopReason;
begin
    // initial estimates for x1 and x2
    Pars[0] := 0;
    Pars[1] := 0;
    Iters := Marquardt(Banana,GradHessBanana,Pars,[],[],FMin,StopReason,mvDouble,IHess);
    //stop if Iters > 500 or Tolerance < 1e-8
    // Returns Pars = [1,1] and FMin = 0, meaning x1=1, x2=1 and minimum value is 0
end;
See Also: TGradHess, TRealFunction, MtxIntDiff.NumericGradHess

Overload 3: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; const FloatPrecision: TMtxFloatPrecision): Integer;

Minimizes the function of several variables by using the Marquardt optimization method with no log.

#NameTypeDescription
1FunTRealFunction
2GradHessTGradHess
3ParsTDoubleArray
4ConstsTDoubleArray
5ObjConstTObjectArray
6FMinDouble
7FloatPrecisionTMtxFloatPrecision

Returns: Int32

Overload 4: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision): Integer;

Minimizes the function of several variables by using the Marquardt optimization method with no log.

#NameTypeDescription
1FunTRealFunction
2GradHessTGradHess
3ParsTDoubleArray
4ConstsTDoubleArray
5ObjConstTObjectArray
6FMinDouble
7StopReasonTOptStopReason
8FloatPrecisionTMtxFloatPrecision

Returns: Int32