derivkit.derivatives.local_polynomial_derivative.local_polynomial_derivative module#
Local polynomial-regression derivative estimator.
- class derivkit.derivatives.local_polynomial_derivative.local_polynomial_derivative.LocalPolynomialDerivative(func: Callable[[float], Any], x0: float, config: LocalPolyConfig | None = None)#
Bases:
objectEstimates derivatives via trimmed local polynomial regression around x0.
Initializes the LocalPolynomialDerivative instance.
- Parameters:
func – Function to differentiate. It should take a float and return either a scalar or a NumPy array (vector or tensor); derivatives are computed componentwise with the same output shape.
x0 – The point at which to estimate the derivative.
config – An optional
derivkit.local_polynomial_derivative.local_poly_config.LocalPolyConfiginstance with configuration settings.
- differentiate(order: int = 1, degree: int | None = None, n_workers: int = 1, return_error: bool = False, diagnostics: bool = False)#
Local polynomial-regression derivative estimator.
This class estimates derivative at
x0by sampling the function in a small neighborhood around that point, fitting a polynomial to those samples, and trimming away samples whose residuals are inconsistent with the fit. Once a stable local polynomial is obtained, the k-th derivative is read off directly from the coefficient of the fitted polynomial (k! * a_k). The method works for scalar or vector/tensor-valued functions, and can optionally return a diagnostics dictionary showing which samples were used, how trimming behaved, and whether the final fit passed all internal checks.- Parameters:
order – The order of the derivative to estimate (must be >= 1).
degree – The degree of the polynomial fit. If
None, it is set tomax(order + 2, 3)but capped byself.config.max_degree.n_workers – The number of parallel workers for function evaluation (must be >= 1).
return_error – If
True, also returns a relative error estimate based on the disagreement between trimmed and least-squares fits.diagnostics – If
True, returns a diagnostics dictionary along with the derivative estimate.
- Returns:
If
return_erroris False anddiagnosticsis False: the estimated derivative (float or np.ndarray).If
return_erroris True anddiagnosticsis False:(derivative, error).If
return_erroris False anddiagnosticsis True:(derivative, diagnostics_dict).If both
return_erroranddiagnosticsare True:(derivative, error, diagnostics_dict).
- Return type:
The return type depends on
return_erroranddiagnostics- Raises:
ValueError – If order < 1, n_workers < 1, or degree < order.