derivkit.utils.extrapolation module#
Extrapolation methods for numerical approximations.
- derivkit.utils.extrapolation.gauss_richardson_extrapolate(base_values: Sequence[ndarray[tuple[Any, ...], dtype[float64]] | float], h_values: Sequence[float], p: int, jitter: float = 1e-10) tuple[ndarray[tuple[Any, ...], dtype[float64]] | float, ndarray[tuple[Any, ...], dtype[float64]] | float]#
Gauss–Richardson extrapolation for a sequence of approximations f(h_i).
This method uses a Gaussian-process model with a radial-basis-function (RBF) kernel to perform Richardson extrapolation, providing both an improved estimate of the true value at h=0 and an uncertainty estimate. For more details, see arXiv:2401.07562.
- Parameters:
base_values – Sequence of approximations at different step sizes h_i.
h_values – Corresponding step sizes (must be positive and same length as base_values).
p – The order of the leading error term in the approximations.
jitter – Small positive value added to the diagonal of the kernel matrix for numerical stability. Defaults to
1e-10.
- Returns:
extrapolated_value is the Gauss–Richardson extrapolated estimate at h=0.
error_estimate is a heuristic uncertainty estimate for the extrapolated value.
- Return type:
A tuple (extrapolated_value, error_estimate) where
- Raises:
ValueError – If h_values and base_values have different lengths or if any h_value is non-positive.
- derivkit.utils.extrapolation.richardson_extrapolate(base_values: Sequence[ndarray[tuple[Any, ...], dtype[float64]] | float], p: int, r: float = 2.0) ndarray[tuple[Any, ...], dtype[float64]] | float#
Computes Richardson extrapolation on a sequence of approximations.
Richardson extrapolation improves the accuracy of a sequence of numerical approximations that converge with a known leading-order error term. Given a sequence of approximations computed with decreasing step sizes, this method combines them to eliminate the leading error term, yielding a more accurate estimate of the true value.
- Parameters:
base_values – Sequence of approximations at different step sizes. The step sizes are assumed to decrease by a factor of
rbetween successive entries.p – The order of the leading error term in the approximations.
r – The step-size reduction factor between successive entries (default is
2.0).
- Returns:
The extrapolated value with improved accuracy.
- Raises:
ValueError – If
base_valueshas fewer than two entries.
- derivkit.utils.extrapolation.ridders_extrapolate(base_values: ~typing.Sequence[~numpy.ndarray[tuple[~typing.Any, ...], ~numpy.dtype[~numpy.float64]] | float], r: float = 2.0, *, extrapolator=<function richardson_extrapolate>, p: int = 2) tuple[ndarray[tuple[Any, ...], dtype[float64]] | float, float]#
Computes a Ridders-style extrapolation on a sequence of approximations.
This builds the usual Ridders diagonal assuming a central finite-difference scheme (leading error is approximately O(h^2)) by repeatedly extrapolating prefixes of
base_values. By default it usesderivkit.utils.extrapolation.richardson_extrapolate()withp=2, but a different extrapolator can be passed if needed.- Parameters:
base_values – Sequence of derivative approximations at step sizes h, h/r, h/r^2, … (all same shape: scalar, vector, or tensor).
r – Step-size reduction factor (default
2.0).extrapolator – Function implementing the extrapolation step. Must have the signature
extrapolator(base_values, p, r) -> array_like. Defaults toderivkit.utils.extrapolation.richardson_extrapolate().p – Leading error order passed to
extrapolator(default2).
- Returns:
best_valueis the extrapolated estimate chosen from the diagonal entries.error_estimateis a heuristic scalar error scale given by the minimum difference between consecutive diagonal elements.
- Return type:
A tuple
(best_value, error_estimate)where- Raises:
ValueError – If fewer than two base values are provided.