Description Usage Arguments Details Value Author(s) References Examples
Evaluate a function for a given list of possible arguments.
1 2 3 4 5 
fun 
a function of the form 
levels 
a list of levels for the arguments (see Examples) 
... 
objects passed to 
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 
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 
mc.control 
a list containing settings that will be passed to 
cl 
default is 
keepNames 

asList 
does 
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
is multicore
, then function mclapply
from package parallel is used. Further settings for
mclapply
can be passed through the list mc.control
. If
multicore
is chosen but the functionality is not available,
then method
will be set to loop
and a warning is
issued. If method == "snow"
, the function clusterApply
from package parallel 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.
A list.
minfun 
the minimum of 
minlevels 
the levels that give this minimum. 
values 
a list. All the function values of 
levels 
a list. All the levels for which 
Enrico Schumann
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. (2016) Financial Optimisation with R (NMOF Manual). http://enricoschumann.net/NMOF.htm#NMOFmanual
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  testFun < function(x)
x[1L] + x[2L]^2
sol < gridSearch(fun = testFun, levels = list(1:2, c(2, 3, 5)))
sol$minfun
sol$minlevels
## specify all levels
levels < list(a = 1:2, b = 1:3)
res < gridSearch(testFun, levels)
res$minfun
sol$minlevels
## specify lower, upper and npar
lower < 1; upper < 3; npar < 2
res < gridSearch(testFun, lower = lower, upper = upper, npar = npar)
res$minfun
sol$minlevels
## specify lower, upper, npar and n
lower < 1; upper < 3; npar < 2; n < 4
res < gridSearch(testFun, lower = lower, upper = upper, npar = npar, n = n)
res$minfun
sol$minlevels
## specify lower, upper and n
lower < c(1,1); upper < c(3,3); n < 4
res < gridSearch(testFun, lower = lower, upper = upper, n = n)
res$minfun
sol$minlevels
## specify lower, upper (autoexpanded) and n
lower < c(1,1); upper < 3; n < 4
res < gridSearch(testFun, lower = lower, upper = upper, n = n)
res$minfun
sol$minlevels

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.