Learn R Programming

NMOF (version 0.20-0)

gridSearch: Grid Search

Description

Evaluate a function for a given list of possible arguments.

Usage

gridSearch(fun, levels, ..., lower, upper, npar = 1L, n = 5L,
           printDetail = TRUE,
           method = c("loop", "multicore", "snow"),
           mc.control = list(), cl = NULL,
           keepNames = FALSE, asList = FALSE)

Arguments

fun
a function of the form fun(x, ...), with x being a numeric vector or a list
levels
a list of levels for the arguments (see Examples)
...
objects passed to fun
lower
a numeric vector. Ignored if levels are explicitly specified.
upper
a numeric vector. Ignored if levels are explicitly specified.
npar
the number of parameters. Must be supplied if lower and upper are to be expanded; see Details. Ignored if levels are explicitly specified.
n
the number of levels. Default is 5. Ignored if levels are explicitly specified.
printDetail
print information on the number of objective function evaluations
method
can be loop (the default), multicore or snow. See Details.
mc.control
a list containing settings that will be passed to mclapply if method is multicore. Must be a list of named elements; see the documentation of mclapply in pkg{multicore}.
cl
default is NULL. If method snow is used, this must be a cluster object or an integer (the number of cores).
keepNames
should the names of levels be kept? Not implemented.
asList
does fun expect a list? Default is FALSE.

Value

  • A list.
  • minfunthe minimum of fun.
  • minlevelsthe levels that give this minimum.
  • valuesa list. All the function values of fun.
  • levelsa list. All the levels for which fun was evaluated.

Details

A grid search can be used to find good parameter values for a function. In principle, a grid search has an obvious deficiency: as the length of x (the first argument to fun) increases, the number of necessary function evaluations grows exponentially. Note that gridSearch will not warn about an unreasonable number of function evaluations, but if printDetail is TRUE it will print the required number of function evaluations. In practice, grid search is often better than its reputation. If a function takes only a few parameters, it is often a reasonable approach to find good parameter values. The function uses the mechanism of expand.grid to create the list of parameter combinations for which fun is evaluated; it calls lapply to evaluate fun if method == "loop" (the default). If method == "multicore", the function mclapply from package multicore is used. Further settings for mclapply can be passed through the list mc.control. If multicore is chosen but not available, then method will be set to loop and a warning is issued. If method == "snow", the function clusterApply from package snow is used. In this case, the argument cl must either be a cluster object (see the documentation of clusterApply) or an integer. If an integer, a cluster will be set up via makeCluster(c(rep("localhost", cl)), type = "SOCK") (and stopCluster is called when the function is exited). If snow is chosen but not available or cl is not specified, then method will be set to loop and a warning is issued.

References

Gilli, M., Maringer, D. and Schumann, E. (2011) Numerical Methods and Optimization in Finance. Elsevier. http://www.elsevierdirect.com/product.jsp?isbn=9780123756626 Schumann, E. (2011) Examples and Extensions for the NMOF Package. http://ssrn.com/abstract=1886522

Examples

Run this code
testFun <- function(x) x[1L] + x[2L]^2
sol <- gridSearch(fun = testFun, levels = list(1:2, c(2, 3, 5)))
sol$minfun
sol$minlevels

testFun2 <- function(x) sum(x^2)
sol <- gridSearch(fun = testFun2, lower = rep(1, 3), upper = c(2, 8, 4), n = 5L)
sol$minfun
sol$minlevels

Run the code above in your browser using DataLab