Description
General-purpose optimization wrapper function that calls other R tools for optimization, including the existing optim() function. optim
also tries to unify the calling sequence to allow a number of tools to use the same front-end. Note that optim() itself allows Nelder--Mead, quasi-Newton and conjugate-gradient algorithms as well as box-constrained optimization via L-BFGS-B. Because SANN does not return a meaningful convergence code (conv), optimz::optim() does not call the SANN method.
Usage
optimr(par, fn, gr=NULL, lower=-Inf, upper=Inf, method=NULL, hessian=FALSE, control=list(), ...)
Arguments
par
a vector of initial values for the parameters for which optimal values are to be found. Names on the elements of this vector are preserved and used in the results data frame.
fn
A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result.
gr
A function to return (as a vector) the gradient for those methods that can use this information. If 'gr' is NULL
, a finite-difference approximation will be used. An open question concerns whether the SAME approximation code used for all methods, or whether there are differences that could/should be examined?
lower, upper
Bounds on the variables for methods such as "L-BFGS-B"
that can handle box (or bounds) constraints.
method
A list of the methods to be used. Note that this is an important change from optim() that allows just one method to be specified. See ‘Details’. The default of NULL causes an appropriate set of methods to be supplied depending on the presence or absence of bounds on the parameters. The default unconstrained set is Rvmminu, Rcgminu, lbfgsb3, newuoa and nmkb. The default bounds constrained set is Rvmminb, Rcgminb, lbfgsb3, bobyqa and nmkb.
hessian
A logical control that if TRUE forces the computation of an approximation to the Hessian at the final set of parameters. If FALSE (default), the hessian is calculated if needed to provide the KKT optimality tests (see kkt
in ‘Details’ for the control
list). This setting is provided primarily for compatibility with optim().
control
A list of control parameters. See ‘Details’.
…
For optimx
further arguments to be passed to fn
and gr
; otherwise, further arguments are not used.
Value
For <U+2018>optim<U+2019>, a list with components:
The best set of parameters found.
The value of <U+2018>fn<U+2019> corresponding to <U+2018>par<U+2019>.
A two-element integer vector giving the number of calls to <U+2018>fn<U+2019> and <U+2018>gr<U+2019> respectively. This excludes those calls needed to compute the Hessian, if requested, and any calls to <U+2018>fn<U+2019> to compute a finite-difference approximation to the gradient.
An integer code. <U+2018>0<U+2019> indicates successful completion
A character string giving any additional information returned by the optimizer, or <U+2018>NULL<U+2019>.
Always NULL for this routine.
Details
Note that arguments after …
must be matched exactly.
This routine is essentially the same as that in package optimrx
which is NOT in CRAN. This version permits the selection of fewer optimizers in the method
argument. This reduced selection is intended to avoid failures if dependencies are not available. The available methods are listed in the variable allmeth
in the file ctrldefault.R
.
By default this function performs minimization, but it will maximize if control$maximize
is TRUE. The original optim() function allows control$fnscale
to be set negative to accomplish this. DO NOT use both methods.
Possible method codes are 'Nelder-Mead', 'BFGS', 'CG', 'L-BFGS-B', 'nlm', 'nlminb', 'Rcgmin', 'Rvmmin' and 'hjn'. These are in base R or in CRAN repositories or part of this package.
The default methods for unconstrained problems (no lower
or upper
specified) are an implementation of the Nelder and Mead (1965) and a Variable Metric method based on the ideas of Fletcher (1970) as modified by him in conversation with Nash (1979). Nelder-Mead uses only function values and is robust but relatively slow. It will work reasonably well for non-differentiable functions. The Variable Metric method, "BFGS"
updates an approximation to the inverse Hessian using the BFGS update formulas, along with an acceptable point line search strategy. This method appears to work best with analytic gradients. ("Rvmmmin"
provides a box-constrained version of this algorithm.
If no method
is given, and there are bounds constraints provided, the method is set to "L-BFGS-B"
.
Method "CG"
is a conjugate gradients method based on that by Fletcher and Reeves (1964) (but with the option of Polak--Ribiere or Beale--Sorenson updates). The particular implementation is now dated, and improved yet simpler codes have been implemented. Furthermore, "Rcgmin"
allows box constraints as well as fixed (masked) parameters. Conjugate gradient methods will generally be more fragile than the BFGS method, but as they do not store a matrix they may be successful in optimization problems with a large number of parameters.
Method "L-BFGS-B"
is that of Byrd et. al. (1995) which allows box constraints, that is each variable can be given a lower and/or upper bound. The initial value must satisfy the constraints. This uses a limited-memory modification of the BFGS quasi-Newton method. If non-trivial bounds are supplied, this method is selected by the original optim()
function, with a warning. Unfortunately, the authors of the original Fortran version of this method released a correction for bugs in 2011, but these have not been incorporated into the distributed R codes, which are a C translation of a version that appears to be from the mid-1990s. Conversations with Jorge Nocedal suggest that the bug should NOT affect L-BFGS-B. However, CRAN does have a direct translation of the 2001 Fortran in package lbfgsb3
.
Nocedal and Wright (1999) is a comprehensive reference for such methods.
Function fn
can return NA
or Inf
if the function cannot be evaluated at the supplied value, but the initial value must have a computable finite value of fn
. However, some methods, of which "L-BFGS-B"
is known to be a case, require that the values returned should always be finite.
While optim
can be used recursively, and for a single parameter as well as many, this may not be true for optimr
. optim
also accepts a zero-length par
, and just evaluates the function with that argument, but such an input is not recommended.
Method "nlm"
is from the package of the same name that implements ideas of Dennis and Schnabel (1983) and Schnabel et al. (1985). See nlm() for more details.
Method "nlminb"
is the package of the same name that uses the minimization tools of the PORT library. The PORT documentation is at <URL: http://netlib.bell-labs.com/cm/cs/cstr/153.pdf>. See nlminb() for details. (Though there is very little information about the methods.)
Method "Rcgmin"
is from the package of that name. It implements a conjugate gradient algorithm with the Dai and Yuan update (2001) and also allows bounds constraints on the parameters. (Rcgmin also allows mask constraints -- fixing individual parameters -- but there is as yet no interface from "optimr"
.)
Method "Rvmmin"
is from the package of that name. It implements the same variable metric method as the base optim() function with method "BFGS"
but allows bounds constraints on the parameters. (Rvmmin also allows mask constraints -- fixing individual parameters -- but there is as yet no interface from "optimr"
.)
Method "hjn"
is a conservative implementation of a Hooke and Jeeves (1961)
The control
argument is a list that can supply any of the following components:
trace
Non-negative integer. If positive, tracing information on the progress of the optimization is produced. Higher values may produce more tracing information: for method
"L-BFGS-B"
there are six levels of tracing. trace = 0 gives no output (To understand exactly what these do see the source code: higher levels give more detail.)follow.on
= TRUE or FALSE. If TRUE, and there are multiple methods, then the last set of parameters from one method is used as the starting set for the next.
save.failures
= TRUE if we wish to keep "answers" from runs where the method does not return convcode==0. FALSE otherwise (default).
maximize
= TRUE if we want to maximize rather than minimize a function. (Default FALSE). Methods nlm, nlminb, ucminf cannot maximize a function, so the user must explicitly minimize and carry out the adjustment externally. However, there is a check to avoid usage of these codes when maximize is TRUE. See
fnscale
below for the method used inoptim
that we deprecate.all.methods
= TRUE if we want to use all available (and suitable) methods.
kkt
=FALSE if we do NOT want to test the Kuhn, Karush, Tucker optimality conditions. The default is TRUE. However, because the Hessian computation may be very slow, we set
kkt
to be FALSE if there are more than than 50 parameters when the gradient functiongr
is not provided, and more than 500 parameters when such a function is specified. We return logical valuesKKT1
andKKT2
TRUE if first and second order conditions are satisfied approximately. Note, however, that the tests are sensitive to scaling, and users may need to perform additional verification. Ifkkt
is FALSE buthessian
is TRUE, thenKKT1
is generated, butKKT2
is not.all.methods
= TRUE if we want to use all available (and suitable) methods.
kkttol
= value to use to check for small gradient and negative Hessian eigenvalues. Default = .Machine$double.eps^(1/3)
kkt2tol
= Tolerance for eigenvalue ratio in KKT test of positive definite Hessian. Default same as for kkttol
starttests
= TRUE if we want to run tests of the function and parameters: feasibility relative to bounds, analytic vs numerical gradient, scaling tests, before we try optimization methods. Default is TRUE.
dowarn
= TRUE if we want warnings generated by optimx. Default is TRUE.
badval
= The value to set for the function value when try(fn()) fails. Default is (0.5)*.Machine$double.xmax
usenumDeriv
= TRUE if the
numDeriv
functiongrad()
is to be used to compute gradients when the argumentgr
is NULL or not supplied.
The following control
elements apply only to some of the methods. The list may be incomplete. See individual packages for details.
fnscale
An overall scaling to be applied to the value of
fn
andgr
during optimization. If negative, turns the problem into a maximization problem. Optimization is performed onfn(par)/fnscale
. For methods from the set inoptim()
. Note potential conflicts with the controlmaximize
.parscale
A vector of scaling values for the parameters.Optimization is performed on
par/parscale
and these should becomparable in the sense that a unit change in any element producesabout a unit change in the scaled value.Foroptim
.ndeps
A vector of step sizes for the finite-difference approximation to the gradient, on
par/parscale
scale. Defaults to1e-3
. Foroptim
.maxit
The maximum number of iterations. Defaults to
100
for the derivative-based methods, and500
for"Nelder-Mead"
.abstol
The absolute convergence tolerance. Only useful for non-negative functions, as a tolerance for reaching zero.
reltol
Relative convergence tolerance. The algorithm stops if it is unable to reduce the value by a factor of
reltol * (abs(val) + reltol)
at a step. Defaults tosqrt(.Machine$double.eps)
, typically about1e-8
. Foroptim
.alpha
,beta
,gamma
Scaling parameters for the
"Nelder-Mead"
method.alpha
is the reflection factor (default 1.0),beta
the contraction factor (0.5) andgamma
the expansion factor (2.0).REPORT
The frequency of reports for the
"BFGS"
and"L-BFGS-B"
methods ifcontrol$trace
is positive. Defaults to every 10 iterations for"BFGS"
and"L-BFGS-B"
.type
for the conjugate-gradients method. Takes value
1
for the Fletcher--Reeves update,2
for Polak--Ribiere and3
for Beale--Sorenson.lmm
is an integer giving the number of BFGS updates retained in the
"L-BFGS-B"
method, It defaults to5
.factr
controls the convergence of the
"L-BFGS-B"
method. Convergence occurs when the reduction in the objective is within this factor of the machine tolerance. Default is1e7
, that is a tolerance of about1e-8
.pgtol
helps control the convergence of the
"L-BFGS-B"
method. It is a tolerance on the projected gradient in the current search direction. This defaults to zero, when the check is suppressed.
Any names given to par
will be copied to the vectors passed to fn
and gr
. Note that no other attributes of par
are copied over. (We have not verified this as at 2009-07-29.)
References
See the manual pages for optim()
and the packages the DESCRIPTION suggests
.
Dai YH, and Yuan Y (2001). An efficient hybrid conjugate gradient method for unconstrained optimization. Annals of Operations Research 103 (1-4), 33<U+2013>47.
Hooke R. and Jeeves, TA (1961). Direct search solution of numerical and statistical problems. Journal of the Association for Computing Machinery (ACM). 8 (2): 212<U+2013>229.
Nash JC, and Varadhan R (2011). Unifying Optimization Algorithms to Aid Software System Users: optimx for R., Journal of Statistical Software, 43(9), 1-14., URL http://www.jstatsoft.org/v43/i09/.
Nocedal J, and Wright SJ (1999). Numerical optimization. New York: Springer. 2nd Edition 2006.