step.M | R Documentation |
Mathur's AutoDX-like automatic step selection
step.M(
FUN,
x,
h0 = NULL,
deriv.order = 1,
acc.order = 2,
range = NULL,
shrink.factor = 0.5,
min.valid.slopes = 5L,
seq.tol = 0.1,
correction = TRUE,
max.rel.error = .Machine$double.eps^(7/8),
cores = 1,
preschedule = getOption("pnd.preschedule", TRUE),
cl = NULL,
...
)
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 |
deriv.order |
Integer or vector of integers indicating the desired derivative order,
|
acc.order |
Integer or vector of integers specifying the desired accuracy order
for each element of |
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 |
max.rel.error |
Error bound for the relative function-evaluation error
( |
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. |
This function computes the optimal step size for central differences using the \insertCitemathur2012analyticalpnd algorithm. It consists of the following steps.
Choose a reasonable large (but not too large) initial step size h_0
and a reduction
factor (1/2 for fast, 1/4 for slow functions is a reasonable choice).
Compute a series of truncation error estimates via third derivatives or Richardson extrapolation.
Find the leftmost range of consecituve step sizes for which the slope of the trunctation error it approximately equal (within 10% tolerance) to the accuracy order and which is long enough (e.g. at least length 5).
Use the leftmost point of this range as the uncorrected optimal step size, or correct it by shrinking it by a small amount given in the article.
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.
4
– Step trimmed to 0.1|x| when |x| is not tiny and within range.
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.
f <- function(x) x^4 # The derivative at 1 is 4
step.M(x = 1, f)
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
plot(step.M(x = pi/2, f))
plot(step.M(x = pi/4, f))
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.