Taweret.mix.gp_bmm

class Taweret.mix.gp_bmm.GPPriors(kernel, prior_choice='rbfnorm', prior_type=None, prior_params=None, switch=None)[source]

Bases: object

The GP prior class for housing the hyperpriors of the kernels.

Parameters:

kernelobj

The kernel used in the GP mixing step.

prior_choicestr

The choice of which type of prior to use on the length scale. Options are 'rbfnorm', 'matern32', 'matern52', and 'ratquad'.

prior_typedict

Maps the hyperparameters supplied to the names in the priors code ('truncnorm' vs. 'free').

prior_paramsdict

The prior parameters we want to use as starting guesses for the optimization.

switchstr

If using a changepoint kernel, specify which switching function you are using.

Returns:

None.

deriv_cp(cp, mean_cp, var_cp)[source]

The derivative helper function for the changepoint.

Parameters:

cpfloat

The value of the changepoint.

mean_cpfloat

The starting guess of the changepoint mean.

var_cpfloat

The starting guess of the changepoint standard deviation.

Returns:

The value of the derivative at the parameter values.

deriv_w_sigmoid(w, mean_w, var_w)[source]

The derivative helper function for the changepoint width in the sigmoid mixing function.

Parameters:

wfloat

The value of the changepoint width.

mean_wfloat

The starting guess for the changepoint width mean.

var_wfloat

The starting guess for the changepoint width standard deviation.

Returns:

The value of the derivative at the parameters.

deriv_w_tanh(w, mean_w, var_w)[source]

The derivative helper function for the changepoint width in the tanh mixing function.

Parameters:

wfloat

The value of the changepoint width parameter.

mean_wfloat

The starting guess for the changepoint width mean.

var_wfloat

The starting guess for the changepoint width standard deviation.

Returns:

The value of the derivative at the parameters.

log_priors(theta, **kwargs)[source]

The log prior function for optimization.

Parameters:

thetanumpy.ndarray

The initial theta values of the parameters.

Returns:

log_priorfloat

The value of the log prior.

log_gradientfloat

The value of the log gradient.

luniform_ls(ls, a, b)[source]

Helper function to calculate log uniform prior.

Parameters:

lsfloat

The lengthscale parameter.

afloat

The lower cutoff of the uniform prior.

bfloat

The upper cutoff of the uniform prior.

Returns:

Either 0 or -inf depending on the value of the lengthscale.

luniform_sig(sig, a, b)[source]

Helper function to calculate the log uniform prior of the marginal variance.

Parameters:

sigfloat

The value of the marginal variance.

afloat

The lower cutoff of the uniform prior.

bfloat

The upper cutoff of the uniform prior.

Returns:

Either 0 or -inf depending on the value of the marginal variance.

trunc_deriv(sig)[source]

Derivative helper function for the truncated normal distribution for the marginal variance.

Parameters:

sigfloat

The value of the marginal variance.

Returns:

truncfloat

The value of the derivative function.

class Taweret.mix.gp_bmm.GPRwrapper(kernel=None, *, alpha=1e-10, optimizer='fmin_l_bfgs_b', n_restarts_optimizer=0, normalize_y=False, copy_X_train=True, n_targets=None, random_state=None)[source]

Bases: GaussianProcessRegressor

Note: There is no __init__() function for this class, but if a user wishes to define a frozen kernel using it, they may do so by passing the parameters below.

Parameters:

alphascalar or array

The covariance from the data added to the inference. The full covariance matrix from the correlated data is given to the GP here and used to train the GP and predict at new points.

kernel: obj

The kernel chosen for this mixing procedure. If 'None', the code will use an RBF kernel with a length scale of 1 multiplied with the ConstantKernel with a variance of 1.

fit(X, y, priors=True, prior_choice='rbfnorm', prior_type=None, prior_params=None, switch=None, max_iter=None)[source]

Meat of the GP training, where we use the GPR class in scikit-learn and wrap it with this treatment of the extracted model means, variances, and covariances.

Parameters:

Xarray-like of shape (n_samples, n_features) or list of object

Feature vectors or other representations of training data.

yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values.

priorsbool

Whether or not to use hyperpriors on the GP kernel hyperparameters.

prior_choicestr

The choice of which type of prior to use on the length scale. Options are 'rbfnorm', 'matern32', 'matern52', and 'ratquad'.

prior_typedict

Maps the hyperparameters supplied to the names in the priors code ('truncnorm' vs. 'free').

prior_params: dict

If using hyperpriors, specify the dict of starting values for the hyperprior parameters. 'None' will use the default starting values.

switchstr

If using a changepoint kernel, specify which switching function you are using.

max_iterint

The maximum number of iterations of the optimizer.

Returns:

selfobject

The fitted GP object.

class Taweret.mix.gp_bmm.GPmixing(x, models, alpha=1e-10, kernel=None, priors=True, prior_params=None, prior_choice='rbfnorm', prior_type=None, switch=None, max_iter=None, nopt=1000)[source]

Bases: BaseMixer

Parameters:

xnumpy.linspace

Input space variable in which mixing is occurring.

modelsdict

Dict of models with BaseModel methods.

alphascalar or array

The covariance from the data added to the inference. The full covariance matrix from the correlated data is given to the GP here and used to train the GP and predict at new points.

kernel: obj

The kernel chosen for this mixing procedure. If 'None', the code will use an RBF kernel with a length scale of 1 multiplied with the ConstantKernel with a variance of 1.

priors: bool

Whether to use hyperpriors for the selected kernel. If False, code will perform maximum likelihood estimation; if True, code will perform maximum a posteriori (MAP) calculation.

prior_params: dict

Dict of prior hyperparameter starting values if using priors. 'None' will run the default starting values if 'priors' is not None.

prior_choice: str

The choice of which type of prior to use on the length scale of a stationary kernel choice. Options are 'rbfnorm', 'matern32', 'matern52', and 'ratquad'. This argument cannot be 'None'.

prior_typedict

Maps the hyperparameters supplied to the names in the priors code ('truncnorm' vs. 'free').

switch: str

The type of mixing function to use if prior_choice = 'changepoint'. Can only be 'None' when not using the changepoint kernel. Options are 'sigmoid' and 'tanh'.

max_iter: int

The maximum iterations of the optimizer. 'None' uses maximum iterations set by scikit-learn.

nopt: int

The number of times to restart the optimizer to ensure it does not get stuck in a local minimum.

Returns:

None.

evaluate(x)[source]

Evaluation of the trained model at the set of training points. If the model was not yet fit, this function will return the result of the prior at the training points.

Parameters:

xnumpy.linspace

The array of training points used.

Returns:

eval_resultsdict

The evaluated results including the training points, mean, standard deviation, and covariances.

evaluate_weights()[source]

Evaluation of a point estimate of the weights of the models used. Not able to be done for this mixing method since GP is implicitly weighted.

property map

Return the MAP values of the parameters, exponentiated for readability as the true values of the parameters.

Parameters:

None.

Returns:

The MAP values of the parameters.

property posterior

Return the posterior of the parameters. Not needed for this mixing method; we only return the MAP currently.

predict()[source]

Here the GP needed to perform the mixing is predicted at the requested points.

Returns:

gp_resultsdict

A dict of the prediction points in the input space, and the means, variances, and covariances at each of these specified locations.

predict_weights()[source]

Predict the weights of the mixed model. Returns mean and intervals from the posterior of the weights. Cannot predict weights for GP implicitly. Not needed for this method.

property prior

Return the prior of the parameters in the mixing. Not needed for this method, since hyperpriors are really what we are using.

prior_predict(sample=False, n_samples=None)[source]

Find the predicted prior distribution using the unconstrained GP (e.g., no hyperparameter optimization has been performed yet).

Parameters:

samplebool

Whether or not the user would like draws returned from the GP distribution.

n_samplesint

The number of draws to return.

Returns:

prior_resultsdict

The dictionary of prior results, including the input space array, mean, standard deviation, and covariance matrix.

samplesnumpy.ndarray

If samples is True, return the values of the GP for each draw from the distribution.

sample_prior()[source]

Returns samples from the prior distributions for the various weight parameters. Not needed for this mixing method.

set_prior()[source]

Set the priors on the parameters. Not used for this method.

train(X, y)[source]

Train the GP chosen in the __init__() function to optimize its hyperparameters given chosen priors and models.

Parameters:

Xarray-like of shape (n_samples, n_features) or list of object

Feature vectors or other representations of training data.

yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values.

Returns:

None.