Skip to main content

Simple numerical derivative checker with randomized input sampling.

Project description

Numerical Derivative Checker

This package contains a numericcal derivative checker that can be used to verify first or second order derivatives by comparing them to the central finite difference approximation.

The following combinations of input and output types are currently supported

Derivative order Output type
scalar vector
scalar first / second first
Input type vector first / second first
matrix first ---

Sampling types

In helper_functions.py, there are different sampling routines defined to randomize the function inputs. The following samplers are currently implemented:

  • ScalarSampler
  • VectorSampler
  • MatrixSampler
  • PosDefMatrixSampler

Each sampling routine has various parameters such as minimum/maximum values or dimensionality.

Examples

Scalar input and output: f(x) = x**2

from numerical_derivative_checker import check_derivative, ScalarSampler
check_derivative(sampler=ScalarSampler(n_samples=200),
                 fun=lambda x: np.sin(x),
                 grad=lambda x: np.cos(x),
                 hess=lambda x: -np.sin(x))

Vector input and scalar output: f(x) = x^T A x

from numerical_derivative_checker import check_derivative, VectorSampler
A = np.random.rand(5, 5)
check_derivative(sampler=VectorSampler(n=5),
                 fun=lambda x: x @ A @ x,
                 grad=lambda x: (A + A.T) @ x)

Pos. def. matrix input and scalar output: f(X) = log(det(X))

from numerical_derivative_checker import check_derivative, PosDefMatrixSampler
check_derivative(sampler=PosDefMatrixSampler(n=3),
                 fun=lambda x: np.linalg.det(x),
                 grad=lambda x: np.linalg.det(x) * np.linalg.inv(x),
                 tol=1e-3)

see examples.py for more examples.

Project details


Release history Release notifications | RSS feed

This version

0.8

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numerical_derivative_checker-0.8.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

numerical_derivative_checker-0.8-py3-none-any.whl (10.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page