dubfi.linalg.generic

Generic linear algebra objects building upon types modules.

Added in version 0.1.0: (initial release)

Classes

ParametrizedVector

AbstractVector depending on array of parameters.

ParametrizedOperator

AbstractOperator depending on array of parameters.

DiagonalOperator

Generic (scalar times) identity operator.

IdentityOperator

Generic (scalar times) identity operator.

Functions

check_derivatives(→ int)

Test derivatives (gradient and Hesse matrix) of function.

check_derivatives_np(f, f_grad, f_hess, x[, eps, tol])

Test derivatives for functions returning only numpy arrays.

Module Contents

class dubfi.linalg.generic.ParametrizedVector

Bases: abc.ABC

AbstractVector depending on array of parameters.

abstractmethod __call__(s: numpy.ndarray) dubfi.linalg.types.AbstractVector

Vector at given parameter array.

abstractmethod grad(s: numpy.ndarray) dubfi.linalg.types.AbstractVector

Gradient w.r.t. parameters s.

abstractmethod hess(s: numpy.ndarray) dubfi.linalg.types.AbstractVector

Hesse matrix w.r.t. parameters s.

check_derivatives(s: numpy.ndarray, **kwargs: Any) int

Test derivatives grad() and hess().

class dubfi.linalg.generic.ParametrizedOperator

Bases: abc.ABC

AbstractOperator depending on array of parameters.

abstractmethod __call__(s: numpy.ndarray) dubfi.linalg.types.AbstractOperator

Operator at given parameter array.

abstractmethod grad(s: numpy.ndarray) dubfi.linalg.types.AbstractOperator

Gradient w.r.t. parameters s.

grad_vec(s: numpy.ndarray, vec: dubfi.linalg.types.AbstractVector) dubfi.linalg.types.AbstractVector

Equivalent to self.grad(s) @ vec.

vec_grad_vec(s: numpy.ndarray, left: dubfi.linalg.types.AbstractVector, right: dubfi.linalg.types.AbstractVector) numpy.ndarray

Equivalent to left @ (self.grad(s) @ right).

abstractmethod hess(s: numpy.ndarray) dubfi.linalg.types.AbstractOperator

Hesse matrix w.r.t. parameters s.

hess_vec(s: numpy.ndarray, vec: dubfi.linalg.types.AbstractVector) dubfi.linalg.types.AbstractVector

Equivalent to self.hess(s) @ vec.

vec_hess_vec(s: numpy.ndarray, left: dubfi.linalg.types.AbstractVector, right: dubfi.linalg.types.AbstractVector) numpy.ndarray | numpy.float64

Equivalent to left @ (self.hess(s) @ right).

check_derivatives(s: numpy.ndarray, **kwargs: Any) int

Test derivatives grad() and hess().

check_derivatives_applied(s: numpy.ndarray, vec: dubfi.linalg.types.AbstractVector, **kwargs: Any) int

Test derivatives grad() and hess(), applying results on vec.

check_derivatives_sandwich(s: numpy.ndarray, vec: dubfi.linalg.types.AbstractVector, **kwargs: Any) int

Test derivatives grad() and hess(), with results sandwiched by vec.

check_derivatives_trace(s: numpy.ndarray, **kwargs: Any) int

Test derivatives grad() and hess(), with trace applied to results.

class dubfi.linalg.generic.DiagonalOperator(diagonal: numpy.ndarray)

Bases: dubfi.linalg.types.AbstractOperator

Generic (scalar times) identity operator.

Linear operator represented by a diagonal matrix.

Parameters:

diagonal (np.ndarray) – last dimension represents the vector space dimension along which this is the diagonal of a linear operator

diagonal()

Return copy of the diagonal of self as vector.

property shape

Shape of array (matrix) representation of self.

apply(vec)

Apply operator from left.

rapply(vec)

Apply operator from right.

chain(other)

Combine operators: self @ other, may return other or a copy.

tonumpy()

Numpy array (matrix) representation of self.

solve(vec)

Solve self @ x = vec for x.

inv()

Compute (multiplicative) inverse.

__mul__(other)

Multiply element-wise.

__add__(other)

Add other operator.

__sub__(other)

Add other operator.

__iadd__(other)

Add other operator.

__isub__(other)

Add other operator.

trace()

Compute trace of self.

trace_product(other)

Compute trace of (self @ other).

logdet()

Compute log(det(self)), assuming that all entries are positive.

dot(other)

Inner product along non-vector dimensions.

validate_uncertainty_statistics(vec) tuple[numpy.ndarray, numpy.ndarray]

Indicate whether vec is a likely realization of a Gaussian random variable with variance self.

Assume that self is the error covariance matrix of a Gaussian random variable X with mean 0. Provide indications whether X is a likely realization of X in the following form:

Diagonalize self: self = V @ diag(D) @ V.T where D is the vector of eigenvalues of self and V is an orthogonal matrix. Return (V.T @ vec, D). One can expect that (V.T @ vec) / D is a Gaussian random variable with mean 0 and standard deviation 1.

Added in version 0.1.3.

class dubfi.linalg.generic.IdentityOperator(size: int, prefactor=None)

Bases: DiagonalOperator

Generic (scalar times) identity operator.

Linear operator represented by a diagonal matrix.

Parameters:

diagonal (np.ndarray) – last dimension represents the vector space dimension along which this is the diagonal of a linear operator

property shape

Shape of array (matrix) representation of self.

apply(vec)

Apply identity operator, may return vec or a copy.

rapply(vec)

Apply identity operator, may return the vector itself or a copy.

tonumpy()

Numpy array (matrix) representation of self.

solve(vec)

Solve self @ x = vec for x, may return vec or a copy.

inv()

Compute (multiplicative) inverse, again an identity operator.

__mul__(other)

Multiply element-wise.

chain(other)

Combine operators: self @ other, may return other or a copy.

trace()

Compute trace of self.

trace_product(other)

Compute trace of (self @ other).

logdet()

Compute log(det(self)) assuming that all prefactors are positive.

dubfi.linalg.generic.check_derivatives(f, f_grad, f_hess, x, eps=1e-05, tol=20, count=10) int

Test derivatives (gradient and Hesse matrix) of function.

This function prints statistics and warning messages if the numerical computed derivatives deviate significantly from the provided gradient and/or Hessian functions.

Parameters:
  • f (callable) – function mapping a 1d array to a scalar or vector.

  • f_grad (callable) – gradient of f (maps a 1d array to a 1d array or vector of 1d arrays)

  • f_hess (callable) – Hesse matrix of f (maps a 1d array to a 2d array or vector of 2d arrays)

  • x (np.ndarray) – parameter array at which the derivatives should be checked

  • eps (float) – scale of variations of x to compute derivatives numerically

  • tol (float) – tolerance scale relative to expected orders of eps.

  • count (int) – number of directions in which derivatives should be checked.

Returns:

errors – number of errors

Return type:

int

dubfi.linalg.generic.check_derivatives_np(f, f_grad, f_hess, x, eps=1e-05, tol=20)

Test derivatives for functions returning only numpy arrays.