step.M {pnd} | R Documentation |
Mathur's AutoDX-like automatic step selection
Description
Mathur's AutoDX-like automatic step selection
Usage
step.M(
FUN,
x,
h0 = NULL,
max.rel.error = .Machine$double.eps^(7/8),
range = NULL,
shrink.factor = 0.5,
min.valid.slopes = 5L,
seq.tol = 0.01,
correction = TRUE,
plot = FALSE,
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 |
max.rel.error |
Error bound for the relative function-evaluation error
( |
range |
Numeric vector of length 2 defining the valid search range for the step size. |
shrink.factor |
A scalar less than 1 that is used to create a sequence of step sizes. The recommended value is 0.5. Change to 0.25 for a faster search. This number should be a negative power of 2 for the most accurate representation. |
min.valid.slopes |
Positive integer: how many points must form a sequence
with the correct slope with relative difference from 2 less than |
seq.tol |
Numeric scalar: maximum relative difference between old and new step sizes for declaring convergence. |
correction |
Logical: if |
plot |
Logical: if |
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 (Mathur 2012) 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 two function evaluations). -
abs.error
– an estimate of the truncation and rounding errors. -
exitcode
– an integer code indicating the termination status:-
0
– Optimal termination due to a sequence of correct reductions. -
1
– Reductions are slightly outside the tolerance. -
2
– Tolerances are significantly violated; an approximate minimum is returned. -
3
– Not enough finite function values; a rule-of-thumb value is returned.
-
-
message
– A summary message of the exit status. -
iterations
– A list including the step and argument grids, function values on those grids, estimated derivative values, and estimated error values.
References
Mathur R (2012). An Analytical Approach to Computing Step Sizes for Finite-Difference Derivatives. Ph.D. thesis, University of Texas at Austin. http://hdl.handle.net/2152/ETD-UT-2012-05-5275.
Examples
f <- function(x) x^4 # The derivative at 1 is 4
step.M(x = 1, f, plot = TRUE)
step.M(x = 1, f, h0 = 1e-9) # Starting low
step.M(x = 1, f, h0 = 1000) # Starting high
f <- sin # The derivative at pi/4 is sqrt(2)/2
step.M(x = pi/2, f, plot = TRUE) # Bad case -- TODO a fix
step.M(x = pi/4, f, plot = TRUE)
step.M(x = pi/4, f, h0 = 1e-9) # Starting low
step.M(x = pi/4, f, h0 = 1000) # Starting high
# where the truncation error estimate is invalid