factory-optimFactory: Function Factory for Optimization function with Unified...

optimFactoryR Documentation

Function Factory for Optimization function with Unified Arguments

Description

A function factory which returns a function with unified input arguments, and provides compatibility with the package optimParallel.

Usage

optimFactory(optim.args = list(), hessian = FALSE, optimParallel.args = list())

Arguments

optim.args

A named list of optional settings for optim. See optim for documentation of valid arguments.

hessian

A logical which specifies whether the hessian matrix is to be returned with the output.

optimParallel.args

A named list which is passed to optimParallel from the optimParallel package. See details.

Details

The function factory optimFactory() returns a function of the form function(par, fn, gr = NULL, ..., lower, upper). It is intended to replace calls to optim or optimParallel by wrapping both functions. By default, it returns a function that corresponds to optim with default arguments.

To use optimParallel, users may specify the following arguments in optimParallel.args:

num_cores (default = NULL)

The number of cores to use during numerical optimization. Is NULL by default, which corresponds to using stats::optim. When num_cores is a numeric value, the actual number of cores is set to min(detectCores()-1, num_cores) to avoid accidentally overloading the user's system.

forward (default = FALSE)

A logical value which controls whether optimParallel should use central difference approximation of the gradient (FALSE) or forward difference approximation (TRUE).

loginfo (default = FALSE)

A logical value which controls whether optimParallel should return additional information about the optimization process. See optimParallel.

Value

A function of the form function(par, fn, gr = NULL, ..., lower, upper) which returns the output obtained from calls to optim or optimParallel

Author(s)

Thomas Caspar Fischer

References

Hadley Wickham (2015) Advanced R, CRC Press. Florian Gerber and Reinhard Furrer (2019) optimParallel: An R package providing a parallel version of the L-BFGS-B optimization method, The R Journal, 11(1), 352–358

See Also

optim and optimParallel

Examples

library(GeneralizedWendland)
library(optimParallel)

set.seed(43)
n <- 50
range <- 0.4
dist_max <- 2
theta  <- c(range, 1, 1, 0, 0)

locs <- data.frame(x = runif(n, 0, sqrt(dist_max)),
                   y = runif(n, 0, sqrt(dist_max)))
dmat  <- spam::nearest.dist(locs, locs, delta = dist_max)
Sigma <- cov.wendland(h = dmat, theta = theta)
y <- c(spam::rmvnorm(1, Sigma = Sigma))

init_parameters   <- c(0.7, 2, 0, 2, 2)
lower_constraints <- c(0.1, 0.1, 0, 0, 0)
upper_constraints <- c(sqrt(2), 2, 2, 2, 2)

mleFunction <- mleFactory(covariance = cov.wendland)
(mle_result <- mleFunction(y = y, distmat = dmat, init_parameters = init_parameters,
                       theta_llim = lower_constraints, theta_ulim = upper_constraints))

mleFunctionPar <- mleFactory(covariance = cov.wendland, optimParallel.args = list(num_cores = 2))
(mle_result_par <- mleFunctionPar(y = y, distmat = dmat, init_parameters = init_parameters,
                       theta_llim = lower_constraints, theta_ulim = upper_constraints))


GeneralizedWendland documentation built on June 22, 2022, 9:06 a.m.