View source: R/HelperFunctions.R
efficient_bfgs | R Documentation |
BFGS optimizer designed for REML optimization of correlation parameters. Combines function evaluation and gradient computation into single call to avoid redundant model refitting.
efficient_bfgs(par, fn, control = list())
par |
Numeric vector of initial parameter values. |
fn |
Function returning list(objective, gradient). Must return both objective value and gradient vector matching length(par). |
control |
List of control parameters:
|
Implements BFGS, used internally by lgspline()
for optimizing correlation parameters via REML
when argument for computing gradient VhalfInv_grad
is not NULL.
This is more efficient than native BFGS, since gradient and loss can be computed simultaneously, avoiding re-computing components in "fn" and "gr" separately.
List containing:
Parameter vector minimizing objective
Minimum objective value
Number of iterations
TRUE if converged within maxit
Description of termination status
Final approximation of inverse-Hessian, useful for inference
## Minimize Rosenbrock function
fn <- function(x) {
# Objective
f <- 100*(x[2] - x[1]^2)^2 + (1-x[1])^2
# Gradient
g <- c(-400*x[1]*(x[2] - x[1]^2) - 2*(1-x[1]),
200*(x[2] - x[1]^2))
list(f, g)
}
(res <- efficient_bfgs(c(0.5, 2.5), fn))
## Compare to
(res0 <- stats::optim(c(0.5, 2.5), function(x)fn(x)[[1]], hessian = TRUE))
solve(res0$hessian)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.