step.SW {pnd} | R Documentation |
Stepleman–Winarsky automatic step selection
Description
Stepleman–Winarsky automatic step selection
Usage
step.SW(
FUN,
x,
h0 = 1e-05 * (abs(x) + (x == 0)),
shrink.factor = 0.5,
range = h0/c(1e+12, 1e-08),
seq.tol = 1e-04,
max.rel.error = .Machine$double.eps/2,
maxit = 40L,
cores = 1,
preschedule = getOption("pnd.preschedule", TRUE),
cl = NULL,
...
)
Arguments
FUN |
Function for which the optimal numerical derivative step size is needed. |
x |
Numeric scalar: the point at which the derivative is computed and the optimal step size is estimated. |
h0 |
Numeric scalar: initial step size, defaulting to a relative step of
slightly greater than .Machine$double.eps^(1/3) (or absolute step if |
shrink.factor |
A scalar less than 1 that is used to multiply the step size during the search. The authors recommend 0.25, but this may be result in earlier termination at slightly sub-optimal steps. Change to 0.5 for a more thorough search. |
range |
Numeric vector of length 2 defining the valid search range for the step size. |
seq.tol |
Numeric scalar: maximum relative difference between old and new step sizes for declaring convergence. |
max.rel.error |
Positive numeric scalar > 0 indicating the maximum relative error of function evaluation. For highly accurate functions with all accurate bits is equal to half of machine epsilon. For noisy functions (derivatives, integrals, output of optimisation routines etc.), it is higher. |
maxit |
Maximum number of algorithm iterations to avoid infinite loops.
Consider trying some smaller or larger initial step size |
cores |
Integer specifying the number of CPU cores used for parallel computation. Recommended to be set to the number of physical cores on the machine minus one. |
preschedule |
Logical: if |
cl |
An optional user-supplied |
... |
Passed to FUN. |
Details
This function computes the optimal step size for central differences using the (Stepleman and Winarsky 1979) algorithm.
Value
A list similar to the one returned by optim()
:
-
par
– the optimal step size found. -
value
– the estimated numerical first derivative (using central differences). -
counts
– the number of iterations (each iteration includes four function evaluations). -
abs.error
– an estimate of the truncation and rounding errors. -
exitcode
– an integer code indicating the termination status:-
0
– Optimal termination within tolerance. -
2
– No change in step size within tolerance. -
3
– Solution lies at the boundary of the allowed value range. -
4
– Maximum number of iterations reached.
-
-
message
– A summary message of the exit status. -
iterations
– A list including the full step size search path, argument grids, function values on those grids, estimated derivative values, estimated error values, and monotonicity check results.
References
Stepleman RS, Winarsky ND (1979). “Adaptive numerical differentiation.” Mathematics of Computation, 33(148), 1257–1264. doi:10.1090/s0025-5718-1979-0537969-8.
Examples
f <- function(x) x^4 # The derivative at 1 is 4
step.SW(x = 1, f)
step.SW(x = 1, f, h0 = 1e-9) # Starting too low
# Starting somewhat high leads to too many preliminary iterations
step.SW(x = 1, f, h0 = 10)
step.SW(x = 1, f, h0 = 1000) # Starting absurdly high
f <- sin # The derivative at pi/4 is sqrt(2)/2
step.SW(x = pi/4, f)
step.SW(x = pi/4, f, h0 = 1e-9) # Starting too low
step.SW(x = pi/4, f, h0 = 0.1) # Starting slightly high
# The following two example fail because the truncation error estimate is invalid
step.SW(x = pi/4, f, h0 = 10) # Warning
step.SW(x = pi/4, f, h0 = 1000) # Warning