TransportMaps.DerivativesChecks

Module Contents

Functions

fd(f, x, dx[, params, fshape, newdim])

Compute finite difference of f

fd_gradient_check(f, gf, x, dx[, rtol, atol, params, ...])

Check the gradient grad_f using finite difference approximation of f.

action_hess_check(ghf, ahf, x, v[, fd_dx, rtol, atol, ...])

Check the correctness of the action \(\langle \nabla^2 f, v \rangle\).

TransportMaps.DerivativesChecks.fd(f, x, dx, params=None, fshape=None, newdim=None)[source]

Compute finite difference of f

The function f may be any scalar, vector, matrix, tensor etc. valued function \(f:\mathbb{R}^d\rightarrow \mathbb{R}^{n_1 \times \cdot \times n_k}\). If the function \(f\) has \(k\) output dimensions, the result of fd() will be of \(k+1\) dimensions, where the extra dimension will be appended at the position prescribed by newdim.

Parameters:
  • f (function) – the function \(f\)

  • x (ndarray [\(m,d\)]) – evaluation points. If the function is not vectorized then x should be of dimension \(d\).

  • dx (float) – finite difference perturbation

  • params (dict) – dictionary of parameters

  • fshape (tuple) – expected shape of the output of \(f\). It should correspond to \(n_1, \ldots, n_k\). If not provided, then it will be inferred by calling f.

  • newdim (int) – dimension along which to add the gradient.

Returns:

ndarray [\(m,n_1,\ldots,n_{k+1}\)] – gradient of

\(f\), where one of the \(n_i\) is \(d\).

TransportMaps.DerivativesChecks.fd_gradient_check(f, gf, x, dx, rtol=None, atol=None, params=None, fshape=None, newdim=None, verbose=True, title='')[source]

Check the gradient grad_f using finite difference approximation of f.

Parameters:
  • f (function) – the function \(f\) or \((f, \nabla f)\)

  • gf (function) – the funtion \(\nabla f\). If None, then it is assumed that f returns the tuple \((f,\nabla f)\).

  • x (ndarray [\(m,d\)]) – evaluation points. If the function is not vectorized then x should be of dimension \(d\).

  • dx (float) – finite difference perturbation

  • rtol (float) – relative tolerance (default is dx)

  • atol (float) – absolute tolerance (default is dx)

  • params (dict) – dictionary of parameters

  • fshape (tuple) – expected shape of the output of \(f\). It should correspond to \(n_1, \ldots, n_k\). If not provided, then it will be inferred by calling f.

  • newdim (int) – dimension along which to add the gradient.

  • verbose (bool) – whether to output the result of the test

  • title (str) – if verbose==True it is used to identify the gradient in the outputs

Returns:

bool – whether the gradient and its finite difference approximation

are within the specified error tolerances.

TransportMaps.DerivativesChecks.action_hess_check(ghf, ahf, x, v, fd_dx=None, rtol=None, atol=None, params=None, fshape=None, newdim=None, verbose=True, title='')[source]

Check the correctness of the action \(\langle \nabla^2 f, v \rangle\).

If the perturbation fd_dx is provided, then ghf is assumed to be the gradient \(\nabla f\) and the Hessian \(\nabla^2 f\) is approximated using finite difference. Otherwise ghf is considered to be the Hessian \(\nabla^2 f\) itself.

Parameters:
  • ghf (function) – the function \(\nabla^2 f\) or \(\nabla f\) of \((f, \nabla f)\). In the latter two cases the Hessian is approximated with finite difference and fd_dx must be provided

  • ahf (function) – the funtion \(\langle \nabla^ f, v \rangle\)

  • x (ndarray [\(m,d\)]) – evaluation points. If the function is not vectorized then x should be of dimension \(d\).

  • v (ndarray [\(m,d\)]) – vector \(v\). If the action of the Hessian is not vectorized with respect to the perturbation, then v should be of dimension :maht:`d`

  • fd_dx (float) – finite difference perturbation

  • rtol (float) – relative tolerance (default is dx)

  • atol (float) – absolute tolerance (default is dx)

  • params (dict) – dictionary of parameters

  • fshape (tuple) – expected shape of the output of \(f\). It should correspond to \(n_1, \ldots, n_k\). If not provided, then it will be inferred by calling f.

  • newdim (int) – dimension along which to add the gradient.

  • verbose (bool) – whether to output the result of the test

  • title (str) – if verbose==True it is used to identify the gradient in the outputs

Returns:

bool – whether the action of the Hessian

is within the specified error tolerances.