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: object

Estimates 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.LocalPolyConfig instance 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 x0 by 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 to max(order + 2, 3) but capped by self.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_error is False and diagnostics is False: the estimated derivative (float or np.ndarray).

  • If return_error is True and diagnostics is False: (derivative, error).

  • If return_error is False and diagnostics is True: (derivative, diagnostics_dict).

  • If both return_error and diagnostics are True: (derivative, error, diagnostics_dict).

Return type:

The return type depends on return_error and diagnostics

Raises:

ValueError – If order < 1, n_workers < 1, or degree < order.