derivkit.local_polynomial_derivative.fit module

Local polynomial fitting with outlier trimming.

derivkit.local_polynomial_derivative.fit.centered_polyfit_least_squares(x0: float, xs: ndarray, ys: ndarray, degree: int) tuple[ndarray, ndarray]

Calculates a plain least-squares polynomial fit in powers of (x - x0).

Parameters:
  • x0 – Expansion point.

  • xs – Sample locations (1D array-like).

  • ys – Sample values (shape (n_samples,) or (n_samples, n_comp)).

  • degree – Polynomial degree.

Returns:

A tuple containing
  • An array of shape (degree+1, n_comp) with coefficients

    for sum_k a_k (x - x0)^k.

  • A boolean mask of length n_samples (all True here).

derivkit.local_polynomial_derivative.fit.design_matrix(x0: float, config: LocalPolyConfig, sample_points: ndarray, degree: int) ndarray

Builds a Vandermonde design matrix.

This method constructs the Vandermonde matrix for polynomial fitting based on whether centering around x0 is specified in the config.

Parameters:
  • x0 – The center point for polynomial fitting.

  • config – LocalPolyConfig instance with fitting settings.

  • sample_points – An array of sample points (shape (n_samples,)).

  • degree – The degree of the polynomial to fit.

Returns:

A Vandermonde matrix (shape (n_samples, degree + 1)).

derivkit.local_polynomial_derivative.fit.trimmed_polyfit(x0: float, config: LocalPolyConfig, xs: ndarray, ys: ndarray, degree: int) tuple[ndarray, ndarray, bool]

Returns a polynomial fit with trimmed outliers.

This method fits a polynomial of the specified degree to the provided samples and iteratively removes sample points whose residuals exceed the configured tolerances. Trimming continues until either all residuals are within tolerance or the maximum number of trims is reached. If trimming can no longer proceed without violating the minimum sample requirement, the last valid fit is returned.

Parameters:
  • x0 – The center point for polynomial fitting.

  • config – LocalPolyConfig instance with fitting settings.

  • xs – An array of sample points (shape (n_samples,)).

  • ys – An array of function evaluations (shape (n_samples, n_components)).

  • degree – The degree of the polynomial to fit.

Returns:

The fitted polynomial coefficients. Each column corresponds to

one output component of ys, and row k contains the coefficient of the x^k term (or (x−x0)^k if centering is enabled).

used_mask:

A boolean array indicating which sample points were kept after trimming. True means the point was used in the final fit.

ok:

True if, after trimming, all remaining sample points satisfied the residual tolerances defined in config. False means the loop stopped due to hitting trimming limits or minimum-sample constraints.

Return type:

coeffs