optim_share: Use splitfngr with optim

Description Usage Arguments Value Examples

Description

Use R's optim function but pass in a single function that returns both the function and gradient together in a list. Useful when the function and gradient are expensive to calculate and can be calculated faster together than separate.

Usage

1

Arguments

par

Initial values for the parameters to be optimized over. Will be passed to optim as par argument.

fngr

A function that returns a list of two elements: the function value and the gradient value.

...

Arguments passed to optim, such as method, lower, upper, control, and hessian.

Value

Results from running optim

Examples

1
2
quad_share <- function(x){list(sum(x^4), 4*x^3)}
optim_share(par=c(3, -5), quad_share, method="BFGS")

Example output

$par
[1] -0.0003599207  0.0006973696

$value
[1] 2.532927e-13

$counts
function gradient 
      42       36 

$convergence
[1] 0

$message
NULL

splitfngr documentation built on May 2, 2019, 8:54 a.m.