Taweret.mix namespace

Submodules

class Taweret.mix.bivariate_linear.BivariateLinear(models_dic: Dict[str, Type[BaseModel]], method: str = 'sigmoid', nargs_model_dic: Dict[str, int] | None = None, same_parameters: bool = False, full_cov: bool = False, BMMcor: bool = False, mean_mix: bool = False)[source]

Bases: BaseMixer

Local linear Bayesian mixing of two models. This is a general class of mixing that offer both density (likelihood) and mean mixing methods. The default mixing method is linear mixing of two likelihoods.

Parameters:

models_dicdictionary {'name1'model1, 'name2'model2}

Two models to mix, each must be derived from the base_model.

methodstr

Mixing weight function form. This is a function of input parameters.

nargs_model_dicdictionary {'name1'N_model1, 'name2'N_model2}

Only used in calibration. Number of free parameters in each model

same_parametersbool

Only used in BMM with calibration. If set, two models are assumed to have same parameters.

full_covbool

This option is only used in BMMcor method. For BMMC full covariance is not needed and mean_mix must have full covariance.

BMMcorbool

If set use BMMcor method for Bayesian model mixing.

mean_mixbool

If set use mean mixing method for Bayesian model mixing.

evaluate(mixture_params: ndarray, x: ndarray, model_params: List[ndarray] | None = []) ndarray[source]

Evaluate the mixed model for given parameters at input values x

Parameters:

mixture_paramsnp.1darray

parameter values that fix the shape of mixing function

xnp.1daray

input parameter values array

model_params: list[model_1_params, mode_2_params]

list of model parameter values for each model

Returns:

evaluationnp.2darray

the evaluation of the mixed model at input values x Has the shape of len(x) x Number of observables in the model

evaluate_weights(mixture_params: ndarray, x: ndarray) ndarray[source]

return the mixing function values at the input parameter values x

Parameters:

mixture_paramsnp.1darray

parameter values that fix the shape of mixing function

xnp.1darray

input parameter values

Returns:

weightslist[np.1darray, np.1darray]

weights for model 1 and model 2 at input values x

property map

Stores the MAP values for the posterior distributions and is set during the self.train step

mix_loglikelihood(mixture_params: ndarray, model_param: ndarray, x_exp: ndarray, y_exp: ndarray, y_err: ndarray) float[source]

log likelihood of the mixed model given the mixing function parameters

Parameters:

mixture_paramsnp.1darray

parameter values that fix the shape of mixing function

model_params: list[model_1_params, mode_2_params]

list of model parameter values for each model

x_exp: np.1darray

Experimentally measured input values

y_exp: np.2darray

Experimentally measured observable values. Takes the shape len(x_exp) x number of observable types measured

y_err: np.2darray

Experimentally measured observable errors. Takes the shape len(x_exp) x number of observable types measured

property posterior

Stores the most recent posteriors from running self.train function

Returns:

_posteriornp.ndarray

posterior from learning the weights

predict(x: ndarray, CI: List = [5, 95], samples: ndarray | None = None, nthin: int = 1)[source]

Evaluate posterior to make prediction at test points x.

Parameters:

xnp.1darray

input parameter values

CIlist

confidence intervals as percentages

samples: np.ndarray

If samples are given use that instead of posterior for predictions.

Returns:

evaluated_posteriornp.ndarray

array of posterior predictive distribution evaluated at provided test points

meannp.ndarray

average mixed model value at each provided test points

credible_intervalsnp.ndarray

intervals corresponding for 60%, 90% credible intervals

std_devnp.ndarray

sample standard deviation of mixed model output at provided test points

predict_weights(x: ndarray, CI: List = [5, 95], samples: ndarray | None = None)[source]

Calculate posterior predictive distribution for first model weights

Parameters:

xnp.1darray

input parameter values

CIlist

confidence intervals

samples: np.ndarray

If samples are given use that instead of posterior for predictions.

Returns:

posterior_weightsnp.ndarray

array of posterior predictive distribution of weights

meannp.ndarray

average mixed model value at each provided test points

credible_intervalsnp.ndarray

intervals corresponding for 60%, 90% credible intervals

std_devnp.ndarray

sample standard deviation of mixed model output at provided test points

property prior

Dictionary of prior distributions. Format should be compatible with sampler.

Returns:

_priorDict[str, Any]

Underlying prior object(s)

Example:

Please consult BaseMixer.set_prior for an example

prior_predict(x: ndarray, CI: List = [5, 95], n_sample: int = 10000)[source]

Evaluate prior to make prediction at test points x.

Parameters:

xnp.1darray

input parameter values

CIlist

confidence intervals

n_samplesint

number of samples to evaluate prior_prediction

Returns:

evaluated_priornp.ndarray

array of prior predictive distribution evaluated at provided test points

meannp.ndarray

average mixed model value at each provided test points

credible_intervalsnp.ndarray

intervals corresponding for 60%, 90% credible intervals

std_devnp.ndarray

sample standard deviation of mixed model output at provided test points

set_prior(bilby_prior_dic)[source]

Set prior for the mixing function parameters. Prior for the model parameters should be defined in each model.

Parameters:

bilby_prior_dic (bilby.core.prior.PriorDict) --

The keys should be named as following :

'<mix_func_name>_1', '<mix_func_name>_2', ...

Returns:

  • A full Bilby prior object for the mixed model.

  • Including the mixing function parameters and model parameters.

  • The Bilby prior dictionary has following keys. --

    Prior for mixture function parameter :

    '<mix_func_name>_1', '<mix_func_name>_2', ...

    Prior parameters for model 1 :

    '<name_of_the_model>_1', '<name_of_the_model>_2' , ...

    Prior parameters for model 2 :

    '<name_of_the_model>_1', '<name_of_the_model>_2' , ...

train(x_exp: ndarray, y_exp: ndarray, y_err: ndarray, label: str = 'bivariate_mix', outdir: str = 'outdir', kwargs_for_sampler: Dict[str, int] | None = None, load_previous: bool = False, plot: bool = False)[source]

Run sampler to learn parameters. Method should also create class members that store the posterior and other diagnostic quantities important for plotting MAP values, and finds the MAP values for each parameter, and sets them equal to a class variable for easy access.

Parameters:

x_exp: np.1darray

Experimentally measured input values

y_exp: np.2darray

Experimentally measured observable values. Takes the shape len(x_exp) x number of observable types measured

y_err: np.2darray

Experimentally measured observable errors. Takes the shape len(x_exp) x number of observable types measured

label: str

Name of the chain to be stored after sampling

outdir: str

Where to save the MCMC chain and output of bilby samplers

kwargs_for_sampler: Dict

Optional arguments to be used instead of default Bibly sampler settings

load_previous: bool

If a previous training has been done, load that chain instead of retraining.

Returns:

resultbilby posterior object

object returned by the bilby sampler

class Taweret.mix.gaussian.Multivariate(x, models, n_models=0)[source]

Bases: BaseMixer

The multivariate BMM class originally introduced in the BAND SAMBA package. Combines individual models using a Gaussian form.

\[f_{\dagger} = \mathcal{N} \left( \sum_i \frac{f_i/v_i}{1/v_i}, \sum_i \frac{1}{v_i} \right)\]

Example:

m = Multivariate(x=np.linspace(), models=dict(), n_models=0)
m.predict(ci=68)
m.evaluate_weights()

Parameters:

xnumpy.linspace

Input space variable in which mixing is occurring.

modelsdict

Dict of models with BaseModel methods.

n_modelsint

Number of free parameters per model.

Returns:

None.

evaluate()[source]

Evaluate the mixed model at one set of parameters. Not needed for this mixing method.

evaluate_weights()[source]

Calculate the weights for each model in the mixed model over the input space.

Returns:

weightsnumpy.ndarray

Array of model weights calculated in the Multivariate.predict function.

property map

Return the MAP values of the parameters. Not needed for this method.

property posterior

Return the posterior of the parameters. Not needed for this mixing method.

predict(ci=68)[source]

The f_dagger function responsible for mixing the models together in a Gaussian way. Based on the first two moments of the distribution: mean and variance.

Parameters:

ciint, list

The desired credibility interval(s) (1-sigma, 2-sigma)

Returns:

mean, intervals, std_devnumpy.ndarray

The mean, credible intervals, and std_dev of the predicted mixed model

predict_weights()[source]

Predict the weights of the mixed model. Returns mean and intervals from the posterior of the weights. Not needed for this mixing method.

property prior

Return the prior of the parameters in the mixing. Not needed for this method.

prior_predict()[source]

Find the predicted prior distribution. Not needed for this mixing method.

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 needed for this mixing method.

train()[source]

Train the mixed model by optimizing the weights. Not needed in this mixing method.

class Taweret.mix.trees.Trees(model_dict: dict, **kwargs)[source]

Bases: BaseMixer

Constructor for the Trees mixing class, which implements a mean-mixing strategy. The weight functions are modeled using Bayesian Additive Regression Trees (BART). Please read the installation page of the documentation to ensure the BART-BMM Ubuntu package is downloaded and installed.

\[f_\dagger(x) = \sum_{k = 1}^K w_k(x)\;f_k(x)\]

Example:

# Initialize trees class
mix = Trees(model_dict = model_dict)

# Set prior information
mix.set_prior(k=2.5,ntree=30,overallnu=5,
overallsd=0.01,inform_prior=False)

# Train the model
fit = mix.train(X=x_train, y=y_train, ndpost = 10000,
nadapt = 2000, nskip = 2000, adaptevery = 500, minnumbot = 4)

# Get predictions and posterior weight functions.
ppost, pmean, pci, pstd = mix.predict(X = x_test, ci = 0.95)
wpost, wmean, wci, wstd = mix.predict_weights(X=x_test,ci = 0.95)

Parameters:

param dict model_dict:

Dictionary of models where each item is an instance of BaseModel.

param dict kwargs:

Additional arguments to pass to the constructor.

Returns:

returns:

None.

evaluate()[source]

Evaluate the mixed-model to get a point prediction. This method is not applicable to BART-based mixing.

evaluate_weights()[source]

Evaluate the weight functions to get a point prediction. This method is not applicable to BART-based mixing.

property map

Return the map values for parameters in the model. This method is not applicable to BART-based mixing.

property posterior

Returns the posterior distribution of the error standard deviation, which is learned during the training process.

Parameters:

param:

None.

Returns:

returns:

The posterior of the error standard deviation .

rtype:

np.ndarray

predict(X: ndarray, ci: float = 0.95)[source]

Obtain the posterior predictive distribution of the mixed-model at a set of inputs X.

Parameters:

param np.ndarray X:

design matrix of testing inputs.

param float ci:

credible interval width, must be a value within the interval (0,1).

Returns:

returns:

The posterior prediction draws and summaries.

rtype:

np.ndarray, np.ndarray, np.ndarray, np.ndarray

return value:

the posterior predictive distribution evaluated at the specified test points

return value:

the posterior mean of the mixed-model at each input in X.

return value:

the pointwise credible intervals at each input in X.

return value:

the posterior standard deviation of the mixed-model at each input in X.

predict_weights(X: ndarray, ci: float = 0.95)[source]

Obtain posterior distribution of the weight functions at a set of inputs X.

Parameters:

param np.ndarray X:

design matrix of testing inputs.

param float ci:

credible interval width, must be a value within the interval (0,1).

Returns:

returns:

The posterior weight function draws and summaries.

rtype:

np.ndarray, np.ndarray, np.ndarray, np.ndarray

return value:

the posterior draws of the weight functions at each input in X.

return value:

posterior mean of the weight functions at each input in X.

return value:

pointwise credible intervals for the weight functions.

return value:

posterior standard deviation of the weight functions at each input in X.

property prior

Returns a dictionary of the hyperparameter settings used in the various prior distributions.

Parameters:

param:

None.

Returns:

returns:

A dictionary of the hyperparameters used in the model.

rtype:

dict

prior_predict()[source]

Return the prior predictive distribution of the mixed-model. This method is not applicable to BART-based mixing.

set_prior(ntree: int = 1, ntreeh: int = 1, k: float = 2, power: float = 2.0, base: float = 0.95, sighat: float = 1, nu: int = 10, inform_prior: bool = True)[source]

Sets the hyperparameters in the tree and terminal node priors. Also specifies if an informative or non-informative prior will be used when mixing EFTs.

Parameters:

param int ntree:

The number of trees used in the sum-of-trees model for the weights.

param int ntreeh:

The number of trees used in the product-of-trees model for the error standard deviation. Set to 1 for homoscedastic variance assumption.

param float k:

The tuning parameter in the prior variance of the terminal node parameter prior. This is a value greater than zero.

param float power:

The power parameter in the tree prior.

param float base:

The base parameter in the tree prior.

param float overallsd:

An initial estimate of the error standard deviation. This value is used to calibrate the scale parameter in variance prior.

param float overallnu:

The shape parameter in the error variance prior.

param bool inform_prior:

Controls if the informative or non-informative prior is used. Specify true for the informative prior.

param np.ndarray tauvec:

A K-dimensional array (where K is the number of models) that contains the prior standard deviation of the terminal node parameter priors. This is used when specifying different priors for the different model weights.

param np.ndarray betavec:

A K-dimensional array (where K is the number of models) that contains the prior mean of the terminal node parameter priors. This is used when specifying different priors for the different model weights.

Returns:

returns:

None.

train(X: ndarray, y: ndarray, **kwargs)[source]

Train the mixed-model using a set of observations y at inputs x.

Parameters:

param np.ndarray X:

input parameter values of dimension (n x p).

param np.ndarray y:

observed data at inputs X of dimension (n x 1).

param dict kwargs:

dictionary of arguments

Returns:

returns:

A dictionary which contains relevant information to the model such as values of tuning parameters. The MCMC results are written to a text file and stored in a temporary directory as defined by the fpath key in the results dictionary.

rtype:

dict