nlminb_share: Use splitfngr with nlminb

Description Usage Arguments Value Examples

Description

Use nlminb 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

start

Initial values for the parameters to be optimized over. Will be passed to nlminb as start argument.

fngr

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

...

Other arguments passed to nlminb

Value

Result from running nlminb on the given function

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
quad_share <- function(x){list(sum(x^4), 4*x^3)}
nlminb_share(start=c(3, -5), fngr=quad_share)

## Not run: 
# Add a sleep amount to show when it can be faster

# Using share
quad_fngr <- function(x){Sys.sleep(.01); list(sum(x^4), 4*x^3)}
system.time(nlminb_share(start=c(3, -5), fngr=quad_fngr))

# Without share
quad_fn <- function(x) {Sys.sleep(.01); sum(x^4)}
quad_gr <- function(x) {Sys.sleep(.01); 4*x^3}
system.time(nlminb(c(3,-5), quad_fn, quad_gr))

## End(Not run)

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