derivkit.local_polynomial_derivative.local_polynomial_derivative module

Local polynomial-regression derivative estimator.

class derivkit.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 LocalPolyConfig instance with configuration settings.

differentiate(order: int = 1, degree: int | None = None, n_workers: int = 1, 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 config.max_degree.

  • n_workers – The number of parallel workers for function evaluation (must be >= 1).

  • diagnostics – If True, returns a diagnostics dictionary along with the derivative estimate.

Returns:

The estimated derivative (float or np.ndarray). If diagnostics is True: A tuple (derivative, diagnostics_dict).

Return type:

If diagnostics is False

Raises:

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