Fitting

class lsqfitgp.empbayes_fit(hyperprior, gpfactory, data, *, raises=True, minkw={}, gpfactorykw={}, jit=True, method='gradient', initial='priormean', verbosity=0, covariance='auto', fix=None, mlkw={}, forward=False, additional_loss=None)[source]

Maximum a posteriori fit.

Maximizes the marginal likelihood of the data with a Gaussian process model that depends on hyperparameters, multiplied by a prior on the hyperparameters.

Parameters:
hyperpriorscalar, array or dictionary of scalars/arrays

A collection of gvars representing the prior for the hyperparameters.

gpfactorycallable

A function with signature gpfactory(hyperparams) -> GP object. The argument hyperparams has the same structure of the empbayes_fit argument hyperprior. gpfactory must be JAX-friendly, i.e., use jax.numpy and jax.scipy instead of plain numpy/scipy and avoid assignments to arrays.

datadict, tuple or callable

Dictionary of data that is passed to GP.marginal_likelihood on the GP object returned by gpfactory. If a tuple, it contains the first two arguments to GP.marginal_likelihood. If a callable, it is called with the same arguments of gpfactory and must return the argument(s) for GP.marginal_likelihood.

raisesbool, optional

If True (default), raise an error when the minimization fails. Otherwise, use the last point of the minimization as result.

minkwdict, optional

Keyword arguments passed to scipy.optimize.minimize, overwrites values specified by empbayes_fit.

gpfactorykwdict, optional

Keyword arguments passed to gpfactory, and also to data if it is a callable. If jit, gpfactorykw crosses a jax.jit boundary, so it must contain objects understandable by jax.

jitbool

If True (default), use jax.jit to compile the minimization target.

methodstr

Minimization strategy. Options:

‘nograd’

Use a gradient-free method.

‘gradient’ (default)

Use a gradient-only method.

‘fisher’

Use a Newton method with the Fisher information matrix plus the hyperprior precision matrix.

initialstr, scalar, array, dictionary of scalars/arrays

Starting point for the minimization, matching the format of hyperprior, or one of the following options:

‘priormean’ (default)

Start from the hyperprior mean.

‘priorsample’

Take a random sample from the hyperprior.

verbosityint

An integer indicating how much information is printed on the terminal:

0 (default)

No logging.

1

Minimal report.

2

Detailed report.

3

Log each iteration.

4

More detailed iteration log.

5

Print the current parameter values at each iteration.

covariancestr

Method to estimate the posterior covariance matrix of the hyperparameters:

‘fisher’

Use the Fisher information in the MAP, plus the prior precision, as precision matrix.

‘minhess’

Use the hessian estimate of the minimizer as precision matrix.

‘none’

Do not estimate the covariance matrix.

‘auto’ (default)

'minhess' if applicable, 'none' otherwise.

fixscalar, array or dictionary of scalars/arrays

A set of booleans, with the same format as hyperprior, indicating which hyperparameters are kept fixed to their initial value. Scalars and arrays are broadcasted to the shape of hyperprior. If a dictionary, missing keys are treated as False.

mlkwdict

Additional arguments passed to GP.marginal_likelihood.

forwardbool, default False

Use forward instead of backward derivatives. Typically, forward is faster with a small number of parameters.

additional_losscallable, optional

A function with signature additional_loss(hyperparams) -> float which is added to the minus log marginal posterior of the hyperparameters.

Raises:
RuntimeError

The minimization failed and raises is True.

Attributes:
pscalar, array or dictionary of scalars/arrays

A collection of gvars representing the hyperparameters that maximize their posterior. These gvars do not track correlations with the hyperprior or the data.

priorscalar, array or dictionary of scalars/arrays

A copy of the hyperprior.

initialscalar, array or dictionary of scalars/arrays

Starting point of the minimization, with the same format as p.

fixscalar, array or dictionary of scalars/arrays

A set of booleans, with the same format as p, indicating which parameters were kept fixed to the values in initial.

pmeanscalar, array or dictionary of scalars/arrays

Mean of p.

pcovscalar, array or dictionary of scalars/arrays

Covariance matrix of p.

minresultscipy.optimize.OptimizeResult

The result object returned by scipy.optimize.minimize.

minargsdict

The arguments passed to scipy.optimize.minimize.

gpfactorycallable

The gpfactory argument.

gpfactorykwdict

The gpfactorykw argument.

datadict, tuple or callable

The data argument.