optiWrap | R Documentation |
Wrapper for calling stats::optim
with a gradient
computed by automatic differentiation
optiWrap(
par,
fn,
...,
method = c("BFGS", "L-BFGS-B", "CG"),
lower = -Inf,
upper = Inf,
control = list(),
hessian = FALSE,
trace = FALSE
)
par |
Initial value |
fn |
Function to be minimized |
... |
Further argument to be passed to 'fn' |
method |
Optimization method |
lower , upper |
Bounds on the variables for 'L-BFGS-B' |
control |
A list of control parameters passed to 'optim' |
hessian |
If 'TRUE' a *numerically* differentiated matrix is returned. |
trace |
If 'TRUE', keep trace of the visited points |
The gradient of fn
is computed using unlist(d(fn(x)))
. It is
computed at the same time as fn(x)
' and stored for when optim
calls
the gradient. In most cases this should be more efficient than defining
gr = \(x) unlist(d(f(dual(x))))
.
Parameters 'method' 'lower' 'upper' 'control' and 'hessian' are passed directly to
optim
.
optim
f <- function(x) (x[1] - x[2])**4 + (x[1] + 2*x[2])**2 + x[1] + x[2]
X <- seq(-1, 0.5, by = 0.01)
Y <- seq(-1, 0.5, by = 0.01)
Z <- matrix(NA_real_, nrow = length(X), ncol = length(Y))
for(i in seq_along(X)) for(j in seq_along(Y)) Z[i,j] <- f(c(X[i],Y[j]))
contour(X,Y,Z, levels = c(-0.2, 0, 0.3, 2**(0:6)), main = "BFGS")
opt <- optiWrap(c(0,0), f, method = "BFGS", trace = TRUE)
lines(t(opt$trace), type = "o", col = "red")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.