derivkit package

Submodules

Module contents

Provides all derivkit methods.

class derivkit.AdaptiveFitDerivative(function, central_value)

Bases: object

Computes derivatives using an adaptive polynomial fitting approach.

This method evaluates a function at symmetrically spaced points around a central value, fits a polynomial of specified degree (equal to the derivative order), and computes the derivative by evaluating the derivative of the polynomial at the central point.

If the polynomial fit fails to meet a specified residual tolerance, the method adaptively removes points with the largest relative residuals (optionally in symmetric pairs) and refits until the tolerance is met or the minimum sample count is reached.

function

The function to be differentiated. Must return scalar or vector output.

Type:

callable

central_value

The point at which the derivative is evaluated.

Type:

float

diagnostics_data

The diagnostic data collected during the fitting process, if requested. Defaults to None.

Type:

dict, optional

min_used_points

The default minimum number of samples required for fitting. This is used to ensure that the polynomial fit has enough data points to be meaningful.

Type:

int

Initialises the class based on the function and central value.

Parameters:
  • function (callable) – The function to be differentiated. Must return scalar or vector output.

  • central_value (float) – The point at which the derivative is evaluated.

compute(include_zero=True, derivative_order=1, min_samples=7, diagnostics=False, fallback_mode: str = 'finite_difference', fit_tolerance=0.05, floor_accept_multiplier: float = 2.0, n_workers: int = 1)

Computes class derivative using adaptive polynomial fitting.

This method evaluates the target function at symmetric points around a central value, fits a polynomial of the specified order, and computes the derivative from that fit. If the fit quality does not meet the specified tolerance, it adaptively prunes the worst residual points and refits. If all attempts fail, it falls back to a finite difference estimate or accepts a fit based on a looser criterion.

Parameters:
  • derivative_order (int, optional) – The order of the derivative to compute (default is 1). Must be 1, 2, 3, or 4.

  • min_samples (int, optional) – Minimum number of total samples to start with. Must be at least equal to the larger value of derivative_order + 2, self.min_used_points, to have an effect. Smaller values are ignored. Default is 7.

  • fit_tolerance (float, optional) – Maximum acceptable relative residual for the polynomial fit. Default is 0.05, i.e 5%.

  • include_zero (bool, optional) – Whether to include the central point in the sampling grid. Default is True.

  • fallback_mode

    Strategy if the fit fails to meet the tolerance at the minimum sample count. Options are:

    • ”finite_difference”: always reject and use finite difference fallback.

    • ”poly_at_floor”: always accept the polynomial fit at floor regardless of residuals.

    • ”auto”: accept the polynomial at floor if both of the following conditions hold:

      • (max_residual < floor_accept_multiplier × fit_tolerance)

      • (median_residual < fit_tolerance);

      otherwise fall back to finite differences.

    Default is “finite_difference”.

  • floor_accept_multiplier – Tolerance multiplier used in “auto” fallback mode. Default is 2.0.

  • diagnostics (bool, optional) – Whether to return diagnostic information such as residuals, fit values, and pruning status. Default is False.

  • n_workers – Number of worker to use in multiprocessing. Default is 1 (no multiprocessing).

Returns:

The estimated

derivative(s). If diagnostics is True, returns a tuple with the derivative array and a dictionary of diagnostic data.

Return type:

np.ndarray, (np.ndarray, dict)

Raises:

ValueError – An error occurred attempting to comput derivatives of order higher than 4.

get_adaptive_offsets(central_value=None, base_rel=0.01, base_abs=1e-06, factor=1.5, num_offsets=10, max_rel=0.05, max_abs=0.01, step_mode='auto', x_small_threshold=0.001)

Returns an array of absolute step sizes to use for adaptive sampling.

Parameters:
  • central_value (float_optional) – The central value around which to generate offsets. If None, uses self.central_value.

  • base_rel (float, optional) – Base relative step size as a fraction of the central value. Default is 1%.

  • base_abs (float, optional) – Base absolute step size for small central values. Default is 1e-6.

  • factor (float, optional) – Factor by which to increase the step size for each offset. Default is 1.5.

  • num_offsets (int, optional) – Number of offsets to generate. Default is 10.

  • max_rel (float, optional) – Maximum relative step size as a fraction of the central value. Default is 5%.

  • max_abs (float, optional) – Maximum absolute step size. Default is 1e-2.

  • step_mode (str, optional) – Mode for determining step sizes: “auto”, “relative”, or “absolute”. Defaults to “auto”.

  • x_small_threshold (float, optional) – Threshold below which the central value is considered small. Default is 1e-3.

Returns:

An array with absolute step sizes (positive

only), not including the central 0 step.

Return type:

np.ndarray

recommend_fit_tolerance(dx=0.001, verbose=True)

Suggests a fit_tolerance value based on local function variation.

Parameters:
  • dx (float, optional) – Offset used to probe nearby values. Default value is 1e-3.

  • verbose (bool, optional) – Whether to print out the suggested value. Default value is True.

Returns:

Recommended fit_tolerance.

Return type:

float

class derivkit.DerivativeKit(function: Callable[[float], float], central_value: float)

Bases: object

Provides access to adaptive and finite difference derivative calculators.

adaptive

Adaptive polynomial fit-based derivative method.

Type:

:class:AdaptiveFitDerivative

finite

High-order finite difference stencil-based method.

Type:

:class:`` FiniteDifferenceDerivative``

Initialises the class based on function and central value.

Parameters:
  • function – The scalar or vector-valued function to differentiate.

  • central_value – The point at which the derivative is evaluated.

get_used_points(derivative_order: int = 1, n_workers=1)

Returns x and y points used in the adaptive fit (for component 0).

Parameters:
  • derivative_order – Order of the derivative to compute diagnostics for (default is 1).

  • n_workers (int, optional) – Number of worker to use in multiprocessing. Default is 1 (no multiprocessing).

Returns:

A tuple of np.ndarray.

class derivkit.FiniteDifferenceDerivative(function, central_value, log_file=None, debug=False)

Bases: object

Computes numerical derivatives using central finite difference stencils.

This class supports the calculation of first to fourth-order derivatives for scalar or vector-valued functions. It uses high-accuracy central difference formulas with configurable stencil sizes (3-, 5-, 7-, or 9-point).

For scalar-valued functions, a single float is returned. For vector-valued functions, the derivative is computed component-wise and returned as a NumPy array.

function

callable The function to differentiate. Must accept a single float and return either a float or a 1D array-like object.

central_value

float The point at which the derivative is evaluated.

log_file

str, optional Path to a file where debug information may be logged.

debug

bool, optional If True, debug information will be printed or logged.

Supported Stencil and Derivative Combinations

  • 3-point: first-order only

  • 5-point: first to fourth-order

  • 7-point: first and second-order

  • 9-point: first and second-order

Examples:

>>> f = lambda x: x**3
>>> d = FiniteDifferenceDerivative(function=f, central_value=2.0)
>>> d.compute(derivative_order=2)

Initialises the class based on function and central value.

param function:

The function to differentiate. Must accept a single float and return either a float or a 1D array-like object.

type function:

callable

param central_value:

The point at which the derivative is evaluated.

type central_value:

float

param log_file:

Path to a file where debug information may be logged.

type log_file:

str, optional

param debug:

If True, debug information will be printed or logged.

type debug:

bool, optional

compute(derivative_order=1, stepsize=0.01, num_points=5, n_workers=1)

Computes the derivative using a central finite difference scheme.

Supports 3-, 5-, 7-, or 9-point central difference stencils for derivative orders 1 through 4 (depending on the stencil size). Derivatives are computed for scalar or vector-valued functions.

Parameters:
  • derivative_order (int, optional) – The order of the derivative to compute. Must be supported by the chosen stencil size. Default is 1.

  • stepsize (float, optional) – Step size (h) used to evaluate the function around the central value. Default is 0.01.

  • num_points (int, optional) – Number of points in the finite difference stencil. Must be one of [3, 5, 7, 9]. Default is 5.

  • n_workers (int, optional) – Number of worker to use in multiprocessing. Default is 1 (no multiprocessing).

Returns:

The estimated derivative. Returns a float for

scalar-valued functions, or a NumPy array for vector-valued functions.

Return type:

float or np.ndarray

Raises:

ValueError – If the combination of num_points and derivative_order is not supported.

Notes

The available (num_points, derivative_order) combinations are:
  • 3: order 1

  • 5: orders 1, 2, 3, 4

  • 7: orders 1, 2

  • 9: orders 1, 2

get_finite_difference_tables(stepsize)

Returns offset patterns and coefficient tables.

Parameters:

stepsize (float) – Stepsize for finite difference calculation.

Returns:

A tuple of two dictionaries. The first maps from

stencil size to symmetric offsets. The second mapps from (stencil_size, derivative_order) to coefficient arrays.

Return type:

(dict, dict)

class derivkit.ForecastKit(function, central_values, covariance_matrix)

Bases: object

Provides tools for facilitating experimental forecasts.

function

The scalar or vector-valued function to differentiate. It should accept a list or array of parameter values as input and return either a scalar or a np.ndarray of observable values.

Type:

callable

central_values (class

np.ndarray): The point(s) at which the derivative is evaluated. A 1D array or list of parameter values matching the expected input of the function.

covariance_matrix (class

np.ndarray): The covariance matrix of the observables. Should be a square matrix with shape (n_observables, n_observables), where n_observables is the number of observables returned by the function.

n_parameters

The number of elements of central_values.

Type:

int

n_observables

The number of cosmic observables. Determined from the dimension of covariance_matrix.

Type:

int

Initialises the class.

Parameters:
  • function (callable) – The scalar or vector-valued function to differentiate. It should accept a list or array of parameter values as input and return either a scalar or a np.ndarray of observable values.

  • (class (covariance_matrix) – np.ndarray): The points at which the derivative is evaluated. A 1D array or list of parameter values matching the expected input of the function.

  • (classnp.ndarray): The covariance matrix of the observables. Should be a square matrix with shape (n_observables, n_observables), where n_observables is the number of observables returned by the function.

Raises:

ValueError – raised if covariance_matrix is not a square numpy array.

get_derivatives(derivative_order, n_workers=1)

Returns derivatives of the observables of the requested order.

Parameters:
  • derivative_order (int) –

    The requested order d of the derivatives:

    • d = 1 returns first-order derivatives.

    • d = 2 returns second-order derivatives.

    Currently only d = 1, 2 are supported.

  • n_workers (int, optional) – Number of worker to use in multiprocessing. Default is 1 (no multiprocessing).

Returns:

An array of derivative values:

  • d = 1 returns an array with shape (n_parameters, n_observables) containing first-order derivatives.

  • d = 2 returns an array with shape n_parameters, n_parameters, n_observables) containing second-order derivatives.

Return type:

np.ndarray

Raises:
  • ValueError – An error occurred if a derivative was requested of higher order than 2.

  • RuntimeError – An error occurred if a ValueError was not raised after calling the function.

get_forecast_tensors(forecast_order=1, n_workers=1)

Returns a set of tensors according to the requested order of the forecast.

Parameters:
  • forecast_order (int) –

    The requested order D of the forecast:

    • D = 1 returns a Fisher matrix.

    • D = 2 returns the 3-d and 4-d tensors required for the doublet-DALI approximation.

    • D = 3 would be the triplet-DALI approximation.

    Currently only D = 1, 2 are supported.

  • n_workers (int, optional) – Number of worker to use in multiprocessing. Default is 1 (no multiprocessing).

Returns:

A list of numpy arrays:

  • D = 1 returns a square matrix of size n_parameters, where n_parameters is the number of parameters included in the forecast.

  • D = 2 returns one array of shapes (n_parameters, n_parameters, n_parameters) and one array of shape (n_parameters, n_parameters, n_parameters, n_parameters), where n_parameters is the number of parameters included in the forecast.

Return type:

np.ndarray

Raises:
  • ValueError – A ValueError occurs when a forecase order greater than 2 is requested.

  • Exception – An exception occurs if the covariance matrix cannot be inverted.

  • RunTimeError – A RunTimeError occurs if the ValueError was not raised when calling this function.

class derivkit.PlotHelpers(function, central_value, fit_tolerance: float = 0.05, true_derivative_fn=None, plot_dir: str = 'plots')

Bases: object

Utility class to support plotting and evaluation of numerical derivatives under noise.

Provides helper functions for: - Creating noisy function versions - Running derivative estimation trials - Comparing different derivative methods - Computing or approximating reference derivatives - Saving consistent figures to disk

Initialises the class.

Parameters:
  • function – callable Original function to be differentiated.

  • central_value – float Central evaluation point.

  • fit_tolerance – float, optional Tolerance used for adaptive fitting (default is 0.05).

  • true_derivative_fn – callable, optional Ground-truth derivative function, if available.

  • plot_dir – str, optional Directory to save output plots (default is “plots”).

adaptive_fit_with_outlier_removal(x_vals, y_vals, central_value=None, y_center=None, return_inliers=False)

Fit a line to data after removing outliers.

The removal is done using a 2.5σ residual filter. Ensures the central point (central_value, y_center) is always included in the fit.

Parameters:
  • x_vals – array-like X coordinates.

  • y_vals – array-like Y coordinates.

  • central_value – float, optional The central x value to enforce inclusion.

  • y_center – float, optional The corresponding f(central_value) value.

  • return_inliers – bool Whether to return a mask of inlier points.

Returns:

float

Slope of the best-fit line.

interceptfloat

Intercept of the best-fit line.

inlier_masknp.ndarray or None

Boolean array indicating which points were kept (if return_inliers=True).

Return type:

slope

get_noisy_derivatives(derivative_order, noise_std=0.01, trials=100)

Compute derivative estimates across multiple noisy trials using finite, adaptive, and Numdifftools methods.

Parameters:
  • derivative_order – int The order of the derivative to compute.

  • noise_std – float Standard deviation of the noise to add to the function.

  • trials – int Number of noisy trials to run.

Returns:

tuple of lists

(finite_differences, adaptive_fits, numdifftools_estimates)

make_additive_noise_function(noise_std=0.01, seed=None)

Add Gaussian noise to the function directly, no interpolation.

Parameters:
  • noise_std – float Standard deviation of added noise.

  • seed – int, optional Random seed for reproducibility.

Returns:

callable

Noisy function.

make_less_noisy(function, sigma=0.01, seed=42)

Add signal-scaled noise to a function.

The noise is propportional to the absolute value of the function.

Parameters:
  • function – callable Base function.

  • sigma – float Noise scaling factor. Default is 0.01.

  • seed – int Seed for the noise RNG. Default is 42.

Returns:

callable

Noisy version of the input function.

make_noisy_interpolated_function(function, central_value, width=0.2, resolution=100, noise_std=0.01, seed=None)

Create a noisy interpolated version of a function on a local interval around central_value.

Parameters:
  • function – callable Function to be sampled.

  • central_value – float Central point of the interval.

  • width – float Half-width of the interval.

  • resolution – int Number of sample points.

  • noise_std – float Standard deviation of added Gaussian noise.

  • seed – int, optional Random seed for reproducibility.

Returns:

callable

Interpolated noisy function.

make_shared_noisy_func(sigma, *, cover_width=None, resolution=401, seed=None)

Generate a shared noisy interpolated function with reproducible randomness.

Parameters:
  • sigma – float Standard deviation of the added noise.

  • cover_width – float, optional Width of the interpolation domain.

  • resolution – int, optional Number of points in interpolation grid.

  • seed – int, optional Random seed to use.

Returns:

callable

Noisy interpolated function.

nd_derivative(x, derivative_order)

Compute derivative using numdifftools.

Parameters:
  • derivative_order – int Order of the derivative to compute.

  • x – float Point at which to evaluate the derivative.

Returns:

float

Derivative estimate from numdifftools.

reference_derivative(x=None, *, degree=None, derivative_order=1, half_width=None, num=21)

Estimate the true derivative via polynomial fitting if no analytical derivative is given.

Parameters:
  • x – float, optional Point at which to evaluate (defaults to central_value).

  • degree – int, optional Degree of the fitting polynomial.

  • derivative_order – int, optional Order of the derivative to compute (default is 1).

  • half_width – float, optional Half-width of the fitting interval.

  • num – int, optional Number of points to use in the fit.

Returns:

float

Estimated reference derivative.

run_derivative_trials_with_noise(method='finite', order=1, noise_std=0.01, trials=100)

Run repeated derivative estimation trials with a specified method and added noise.

Parameters:
  • method – str One of {“finite”, “adaptive”, “numdifftools”}.

  • order – int Derivative order to compute.

  • noise_std – float Standard deviation of the noise.

  • trials – int Number of trials to run.

Returns:

list

Derivative estimates for each trial.

save_fig(filename)

Save the current matplotlib figure to the configured directory.

Parameters:

filename – str File name (with extension) for saving the figure.

class derivkit.PlotKit(function, central_value: float, derivative_order: int = 1, fit_tolerance=0.05, plot_dir: str = 'plots', linewidth: float | None = None, colors: dict | None = None, gradient_colors: dict | None = None, true_derivative_fn=None)

Bases: object

A high-level plotting utility for evaluating and comparing numerical derivative methods.

This class provides multiple plotting routines to assess the accuracy and behavior of finite difference and adaptive polynomial fit derivative estimates under noisy conditions.

derivs

DerivativeKit Internal instance used to compute derivatives using both finite and adaptive methods.

h

PlotHelpers Helper instance used for diagnostics, noise injection, and figure saving.

seed

int Random seed for reproducibility in plots involving noise.

Initialises the class.

Parameters:
  • function – callable The target function for which derivatives will be evaluated.

  • central_value – float The point at which the derivative is computed.

  • derivative_order – int, optional The order of the derivative to compute (default is 1).

  • fit_tolerance – float, optional Residual tolerance used in adaptive fitting (default is 0.05).

  • plot_dir – str, optional Directory in which to save generated plots (default is “plots”).

  • linewidth – float, optional Line width to use for plots. Uses default style if not specified.

  • colors – dict, optional A dictionary of color overrides keyed by method name (e.g., ‘finite’, ‘adaptive’).

  • gradient_colors – dict, optional Optional color map or overrides for gradients or tolerance bands.

  • true_derivative_fn – callable, optional Optional function to compute the true derivative for reference.

adaptive_fit_demo(derivative_order=1, noise_std=0.01, width=0.2, title=None, extra_info=None)

Visualize the adaptive polynomial fit on a noisy function segment.

This method creates a reproducible noisy realization of the input function over a local region around central_value and shows which points are used or excluded by the adaptive fitting method. It also overlays the fitted polynomial and its tolerance band.

Parameters:
  • derivative_order – int The order of the derivative to compute (e.g., 1 for first derivative). Defaults to 1.

  • noise_std – float, optional Standard deviation of the Gaussian noise added to the function (default is 0.01).

  • width – float, optional Width of the interval around central_value for generating the noisy segment (default is 0.2).

  • title – str, optional Optional title for the plot.

  • extra_info – str, optional Additional string to append to the saved filename.

Notes:

  • Uses a fixed random seed for reproducibility.

  • Highlights the internal logic of the adaptive derivative fitting mechanism visually.

color(key: str) str

Retrieve the color assigned to a specific plot element key.

Parameters:

key – str Identifier for the color key (e.g., ‘finite’, ‘adaptive’, ‘central’, etc.).

Returns:

str

The hex code or named color string associated with the key.

Notes:

  • Falls back to the default color mapping if the key was not explicitly overridden.

  • Used internally for consistent styling across plots.

plot_ecdf_errors(noise_std=0.01, trials=200, title=None, extra_info=None)

Plot empirical cumulative distribution functions (ECDFs) of squared errors.

This method compares the distribution of squared errors from finite difference and adaptive derivative estimators by plotting their ECDFs under repeated noisy evaluations.

Parameters:
  • noise_std – float, optional Standard deviation of the Gaussian noise added to the function (default is 0.01).

  • trials – int, optional Number of Monte Carlo trials to generate error samples (default is 200).

  • title – str, optional Optional title for the plot.

  • extra_info – str, optional Additional string to append to the saved filename.

Notes:

  • ECDF provides a full view of the error distribution, not just summary statistics.

  • The reference derivative value is used to compute true squared error per trial.

  • Useful for visualizing which method tends to produce smaller errors more often.

plot_error_vs_noise(derivative_order, noise_levels, trials=50, title=None, extra_info=None)

Plot mean squared error (MSE) of derivative estimates vs. noise level.

This method evaluates both finite difference and adaptive derivative estimators across a range of Gaussian noise standard deviations. It plots: - MSE vs. noise level (log scale) - Relative difference between adaptive and finite MSEs

Parameters:
  • derivative_order – int The order of the derivative to compute (e.g., 1 for first derivative).

  • noise_levels – array-like List or array of noise standard deviation values to test.

  • trials – int, optional Number of Monte Carlo trials per noise level (default is 50).

  • title – str, optional Optional title for the plot.

  • extra_info – str, optional Additional string to append to the saved filename.

Notes:

  • Finite difference uses a fixed step size relative to central_value.

  • Adaptive fit uses polynomial regression with tolerance-controlled filtering.

  • Useful for comparing robustness of methods under varying noise.

plot_overlaid_histograms(derivative_order, noise_std=0.01, trials=100, bins=20, title=None, extra_info=None)

Plot overlaid histograms of derivative estimates.

The estimates are determined from stencil and adaptive method under repeated noisy evaluations.

This method runs multiple trials of noisy function evaluations, computes the derivative using both the finite difference and adaptive fitting methods, and plots their empiricala distributions overlaid as histograms.

Parameters:
  • derivative_order – int Order of the derivative to compute (e.g., 1 for first derivative).

  • noise_std – float, optional Standard deviation of the Gaussian noise added to the function (default is 0.01).

  • trials – int, optional Number of repeated noisy evaluations to perform (default is 100).

  • bins – int, optional Number of bins to use in the histogram (default is 20).

  • title – str, optional Optional title for the plot.

  • extra_info – str, optional Additional string to append to the saved filename.

Notes:

  • Excludes outliers based on 1st and 99th percentiles to improve plot readability.

  • Displays method-specific variance in the legend for comparison.

plot_paired_error_differences(noise_std=0.01, trials=200, title=None, extra_info=None)

Plot paired squared error differences between adaptive and finite methods.

This method runs repeated noisy derivative trials and computes the squared error for both adaptive and finite difference methods. It then plots the pairwise differences: Δ = error²_adaptive − error²_finite.

Points are jittered horizontally to show density and grouped into: - Adaptive better (Δ < 0) - Finite better (Δ > 0) - Tie (Δ = 0)

Parameters:
  • noise_std – float, optional Standard deviation of the Gaussian noise added to the function (default is 0.01).

  • trials – int, optional Number of paired Monte Carlo trials to run (default is 200).

  • title – str, optional Optional title for the plot.

  • extra_info – str, optional Additional string to append to the saved filename.

Notes:

  • A horizontal line at Δ = 0 indicates equal performance.

  • The estimated win rate (P(Δ < 0)) is shown in the legend.

  • This visualization provides intuitive insight into method-wise reliability.

derivkit.central_difference_error_estimate(step_size, order=1)

Provides a rough truncation error estimate for central differences.

Parameters:
  • step_size (float) – Finite difference step size.

  • order (int) – Order of the derivative (1 to 4).

Returns:

Estimated error magnitude.

Return type:

float

derivkit.generate_test_function(name='sin')

Returns a known test function and its first/second derivatives.

Parameters:

name (str) – One of ‘sin’, ‘exp’, ‘polynomial’, ‘gaussian’.

Returns:

(f(x), df/dx, d2f/dx2)

Return type:

tuple

derivkit.is_finite_and_differentiable(function, x, delta=1e-05, tol=0.01)

Checks if a function is finite and numerically differentiable at a point.

Parameters:
  • function (callable) – The function to test.

  • x (float or np.ndarray) – The input value(s).

  • delta (float) – Step size for finite difference.

  • tol (float) – Tolerance for differentiability check.

Returns:

True if function is finite and differentiable at x, False

otherwise.

Return type:

bool

derivkit.is_symmetric_grid(x_vals)

Checks if evaluation grid is symmetric around 0.

Parameters:

x_vals (np.ndarray) – Evaluation points (1D).

Returns:

True if grid is symmetric, False otherwise.

Return type:

bool

derivkit.log_debug_message(message, debug=False, log_file=None, log_to_file=None)

Logs a debug message to stdout and optionally to a file.

Parameters:
  • message (str) – The debug message to print/log.

  • debug (bool) – Whether to print the message.

  • log_file (str or None) – Path to the log file.

  • log_to_file (bool or None) – Whether to write the message to the file.

derivkit.normalize_derivative(derivative, reference)

Computes the relative error between estimated and reference derivative.

Parameters:
  • derivative (float or np.ndarray) – Estimated derivative.

  • reference (float or np.ndarray) – True/reference derivative.

Returns:

Normalized relative error.

Return type:

float or np.ndarray