cofi.utils.GaussianPrior#

class cofi.utils.GaussianPrior(model_covariance_inv: ndarray | Tuple[Tuple, float], mean_model: ndarray)[source]#

CoFI’s utility class to calculate the Gaussian prior, given the inverse of model covariance matrix and the mean model

\(GaussianPrior(C_m^{-1}, \mu) = (m-\mu)^TC_m^{-1}(m-\mu)\)

With gradient of:

\(2\times C_m^{-1} (m-\mu)\)

And hessian of:

\(2\times C_m^{-1}\)

Optionally, the inverse model covariance matrix can be constructed given the correlation lengths and sigma.

Parameters:
  • model_covariance_inv (np.ndarray or (tuple, float)) – the inverse model covariance matrix, either provided by users or specified so can be generated, corresponding to \(C_m^{-1}\) in the formula above. When this is a tuple (i.e. CoFI will construct the matrix for you), it should be in the form of (corr_lengths, sigma)

  • mean_model (np.ndarray) – the prior model, corresponding to \(\mu\) in the formula above

Raises:
  • TypeError – if arguments aren’t in accepted types as described above in “Parameters” section

  • ValueError – if shape of model_covariance_inv and mean_model doesn’t match, or if shape of corr_lengths and mean_model doesn’t match

Examples

Generate a Gaussian Prior term for models of size (4,4), with correlation lengths of (2,2), and sigma of 0.5:

>>> from cofi.utils import GaussianPrior
>>> import numpy as np
>>> my_prior_model = np.array([[1,2],[3,4]])
>>> my_reg = GaussianPrior(model_covariance_inv=((2,2), 0.5), mean_model=my_prior_model)
>>> my_reg(np.array([[0,2],[1,0]]))
101.53804950804782

To use together with cofi.BaseProblem:

>>> from cofi import BaseProblem
>>> my_problem = BaseProblem()
>>> my_problem.set_regularization(my_reg)

Reference Details

__call__(model: ndarray) Number#

a class instance itself can also be called as a function

It works exactly the same as reg.

In other words, the following two usages are exactly the same:

>>> my_reg = QuadraticReg(factor=1, model_size=3)
>>> my_reg_value = my_reg(np.array([1,2,3]))            # usage 1
>>> my_reg_value = my_reg.reg(np.array([1,2,3]))        # usage 2
gradient(model: ndarray) ndarray[source]#

the gradient of regularization function with respect to model given a model

The usual size for the gradient is \((M,)\) where \(M\) is the number of model parameters

hessian(model: ndarray) ndarray[source]#

the hessian of regularization function with respect to model given a model

The usual size for the Hessian is \((M,M)\) where \(M\) is the number of model parameters

reg(model: ndarray) Number[source]#

the regularization function value given a model to evaluate

gaussian_model_covariance_inv#
model_shape#
model_size#

the number of unknowns that current regularization function accepts

back to top