Helper Functions¶
Sparse Hessians¶
-
class
vittles.sparse_hessian_lib.
SparseBlockHessian
(objective_function, sparsity_array)[source]¶ Efficiently calculate block-sparse Hessians.
The objective function is expected to be of the form
\[ \begin{align}\begin{aligned}x = (x_1 , ... , x_G) \textrm{ (or some permutation thereof)}\\f(x) = \sum_{g=1}^{G} f_g(x_g)\end{aligned}\end{align} \]Each \(x_g\) is expected to have the same dimension. Consequently, the Hessian matrix of
f
with respect tox
, is block diagonal withG
blocks, up to a permutation of the order ofx
. The purpose of this class is to efficiently calculate this Hessian when the block structure (i.e., the partition ofx
) is known.-
__init__
(self, objective_function, sparsity_array)[source]¶ In terms of the class description,
objective_function = f
,opt_par = x
, andsparsity_array
describes the partition ofx
into \((x_1, ..., x_G)\).Parameters: - objective_function : callable
An objective function of which to calculate a Hessian. The argument should be
opt_par
: numpy.ndarray (N,) The optimization parameter.
- sparsity_array : numpy.ndarray (G, M)
An array containing the indices of rows and columns of each block. The Hessian should contain
G
dense blocks, each of which isM
byM
. Each row ofsparsity_array
should contain the indices of the corresponding block. There must be no repeated indices, and each block must be the same size.
-
System solvers¶
-
vittles.solver_lib.
get_cg_solver
(mat_times_vec, dim, cg_opts={})[source]¶ Return a function solving \(h^{-1} v\) for a matrix h using conjugate gradient.
Parameters: - mat_times_vec : callable
A function of a single vector argument, v that returns the product h v for some invertible matrix h.
- dim : int
The dimension of the vector v.
- cg_opts : dict
Optional, a dictionary of named arguments for the solver sp.sparse.linalg.cg.
Returns: - solve : callable
A function of a single vector argument, v that returns the conjugate gradient approximation to \(h^{-1} v\).
-
vittles.solver_lib.
get_cholesky_solver
(h)[source]¶ Return a function solving \(h^{-1} v\) for a matrix h.
Parameters: - h : A dense or sparse matrix.
Returns: - solve : callable
A function of a single vector argument, v that returns \(h^{-1} v\).
-
vittles.solver_lib.
get_dense_cholesky_solver
(h, h_chol=None)[source]¶ Return a function solving \(h^{-1} v\) with a dense Cholesky factorization.
Parameters: - h : numpy.ndarray
A dense symmetric positive definite matrix.
- h_chol : A Cholesky factorization
Optional, a Cholesky factorization created with sp.linalg.cho_factor. If this is specified, the argument h is ignored. If None (the default), the factoriztion of h is calculated.
Returns: - solve : callable
A function of a single vector argument, v that returns \(h^{-1} v\).