ncg | R Documentation |
Attempts to minimize an unconstrained or bounds (box) and mask constrained function
of many parameters by a nonlinear conjugate gradients method using the Dai / Yuan
update and restart. Based on Nash (1979) Algorithm 22 for its main structure,
which is method "CG" of the optim()
function that has never performed well.
Bounds (or box) constraints and masks (equality constraints) can be imposed on
parameters.
This code is entirely in R to allow users to explore and understand
the method. However, ncg()
is intended to be called via optimx::optimr()
and NOT
called directly, as it has limited sanity checks on the problem provided, since
such checks are in the optimr()
code.
The earlier Rcgmin()
function
does have such checks, and was originally part of a separate package of the same
name. Rcgmin()
can also be called via optimr()
. It may give slightly
different results due to minor internal coding changes, and is kept available for
backward compatibility with other packages.
ncg(par, fn, gr, bds, control = list())
par |
A numeric vector of starting estimates. |
fn |
A function that returns the value of the objective at the
supplied set of parameters |
gr |
A function that returns the gradient of the objective at the
supplied set of parameters |
bds |
A list of information resulting from function |
control |
An optional list of control settings. |
Function fn
must return a numeric value.
Note that ncg
is to be called from optimr
and does
NOT allow dot arguments. It is intended to use the internal functions
efn
and egr
generated inside optimr()
along with
bounds information from bmchk()
available there.
The control
argument is a list. See the documentation of ctrldefault()
.
The source codes Rcgmin
and ncg
for R are still a work
in progress, so users should watch the console output. The author welcomes feedback.
A list with components:
par |
The best set of parameters found. |
value |
The value of the objective at the best set of parameters found. |
counts |
A two-element integer vector giving the number of calls to 'fn' and 'gr' respectively. This excludes those calls needed to compute the Hessian, if requested, and any calls to 'fn' to compute a finite-difference approximation to the gradient. |
convergence |
An integer code. '0' indicates successful convergence. '1' indicates that the function evaluation count 'maxfeval' was reached. '2' indicates initial point is infeasible. |
message |
A character string giving any additional information returned by the optimizer, or 'NULL'. |
bdmsk |
Returned index describing the status of bounds and masks at the proposed solution. Parameters for which bdmsk are 1 are unconstrained or "free", those with bdmsk 0 are masked i.e., fixed. For historical reasons, we indicate a parameter is at a lower bound using -3 or upper bound using -1. |
Dai, Y. H. and Y. Yuan (2001). An efficient hybrid conjugate gradient method for unconstrained optimization. Annals of Operations Research 103 (1-4), 33–47.
Nash JC (1979). Compact Numerical Methods for Computers: Linear Algebra and Function Minimisation. Adam Hilger, Bristol. Second Edition, 1990, Bristol: Institute of Physics Publications.
Nash, J. C. and M. Walker-Smith (1987). Nonlinear Parameter Estimation: An Integrated System in BASIC. New York: Marcel Dekker. See https://www.nashinfo.com/nlpe.htm for a downloadable version of this plus some extras.
optim
#####################
## Rosenbrock Banana function
fr <- function(x) {
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
grr <- function(x) { ## Gradient of 'fr'
x1 <- x[1]
x2 <- x[2]
c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
200 * (x2 - x1 * x1))
}
# Call is from optimr()
ansrosenbrock0 <- optimr(fn=fr,gr=grr, par=c(1,2), method="ncg")
print(ansrosenbrock0)
#
# Test if 1-parameter problem is possible
#
cat("test n=1 problem using simple squares of parameter\n")
sqtst<-function(xx) {
res<-sum((xx-2)*(xx-2))
}
nn<-1
startx<-rep(0,nn)
onepar<-optimr(startx,sqtst, gr="grfwd", method="ncg", control=list(trace=1))
print(onepar)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.