Taweret.mix namespace

Submodules

class Taweret.mix.bivariate_linear.BivariateLinear(models_dic: Dict[str, Type[BaseModel]], method: str = 'sigmoid', nargs_model_dic: Optional[Dict[str, int]] = 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_dic (dictionary {'name1' : model1, 'name2' : model2}) -- Two models to mix, each must be derived from the base_model.

  • method (str) -- Mixing weight function form. This is a function of input parameters.

  • nargs_model_dic (dictionary {'name1' : N_model1, 'name2' : N_model2}) -- Only used in calibration. Number of free parameters in each model

  • same_parameters (bool) -- Only used in BMM with calibration. If set, two models are assumed to have same parameters.

  • full_cov (bool) -- This option is only used in BMMcor method. For BMMC full covariance is not needed and mean_mix must have full covariance.

  • BMMcor (bool) -- If set use BMMcor method for Bayesian model mixing.

  • mean_mix (bool) -- If set use mean mixing method for Bayesian model mixing.

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

Evaluate the mixed model for given parameters at input values x

Parameters
  • mixture_params (np.1darray) -- parameter values that fix the shape of mixing function

  • x (np.1daray) -- input parameter values array

  • model_params (list[model_1_params, mode_2_params]) -- list of model parameter values for each model

Returns

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

Return type

np.2darray

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

return the mixing function values at the input parameter values x

Parameters
  • mixture_params (np.1darray) -- parameter values that fix the shape of mixing function

  • x (np.1darray) -- input parameter values

Returns

weights -- weights for model 1 and model 2 at input values x

Return type

list[np.1darray, np.1darray]

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 :param mixture_params: parameter values that fix the shape of mixing function :type mixture_params: np.1darray :param model_params: list of model parameter values for each model :type model_params: list[model_1_params, mode_2_params] :param x_exp: Experimentally measured input values :type x_exp: np.1darray :param y_exp: Experimentally measured observable values.

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

Parameters

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

_posteriornp.ndarray

posterior from learning the weights

predict(x: ~numpy.ndarray, CI: ~typing.List = [5, 95], samples: ~typing.Optional[~numpy.ndarray] = None, nthin: int = 1) -> (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>)[source]

Evaluate posterior to make prediction at test points x.

Parameters
  • x (np.1darray) -- input parameter values

  • CI (list) -- confidence intervals as percentages

  • samples (np.ndarray) -- If samples are given use that instead of posterior for predictions.

  • Returns --

  • -------- --

  • evaluated_posterior (np.ndarray) -- array of posterior predictive distribution evaluated at provided test points

  • mean (np.ndarray) -- average mixed model value at each provided test points

  • credible_intervals (np.ndarray) -- intervals corresponding for 60%, 90% credible intervals

  • std_dev (np.ndarray) -- sample standard deviation of mixed model output at provided test points

predict_weights(x: ~numpy.ndarray, CI: ~typing.List = [5, 95], samples: ~typing.Optional[~numpy.ndarray] = None) -> (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>)[source]

Calculate posterior predictive distribution for first model weights

Parameters
  • x (np.1darray) -- input parameter values

  • CI (list) -- confidence intervals

  • samples (np.ndarray) -- If samples are given use that instead of posterior for predictions.

  • Returns --

  • -------- --

  • posterior_weights (np.ndarray) -- array of posterior predictive distribution of weights

  • mean (np.ndarray) -- average mixed model value at each provided test points

  • credible_intervals (np.ndarray) -- intervals corresponding for 60%, 90% credible intervals

  • std_dev (np.ndarray) -- sample standard deviation of mixed model output at provided test points

property prior

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

_priorDict[str, Any]

Underlying prior object(s)

Please consult BaseMixer.set_prior for an example

prior_predict(x: ~numpy.ndarray, CI: ~typing.List = [5, 95], n_sample: int = 10000) -> (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>)[source]

Evaluate prior to make prediction at test points x.

Parameters
  • x (np.1darray) -- input parameter values

  • CI (list) -- confidence intervals

  • n_samples (int) -- number of samples to evaluate prior_prediction

  • Returns --

  • -------- --

  • evaluated_prior (np.ndarray) -- array of prior predictive distribution evaluated at provided test points

  • mean (np.ndarray) -- average mixed model value at each provided test points

  • credible_intervals (np.ndarray) -- intervals corresponding for 60%, 90% credible intervals

  • std_dev (np.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.

bilby_prior_dicbilby.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: Optional[Dict[str, int]] = None, load_previous: 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 should also calculate and set as member variable of class 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.

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)\]
m = Multivariate(x=np.linspace(), models=dict(), n_models=0)
m.predict(ci=68)
m.evaluate_weights()
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.

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.

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.

ciint, list

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

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 Assitive 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)\]
# 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
  • model_dict (dict) -- Dictionary of models where each item is an instance of BaseModel.

  • kwargs (dict) -- Additional arguments to pass to the constructor.

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.

plot_prediction(xdim: int = 0)[source]

Plot the predictions of the mixed-model. The x-axis of this plot can be any column of the design matrix X, which is passed into the predict function.

Parameters

xdim (int) -- index of the column to plot against the predictions.

Returns

None.

plot_sigma()[source]

Plot the posterior distribution of the observational error standard deviation.

plot_weights(xdim: int = 0)[source]

Plot the weight functions. The x-axis of this plot can be any column of the design matrix X, which is passed into the predict_weights function.

Parameters

xdim (int) -- index of the column to plot against the predictions.

Returns

None.

property posterior

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

Param

None.

Returns

The posterior of the error standard deviation .

Return type

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
  • X (np.ndarray) -- design matrix of testing inputs.

  • ci (float) -- credible interval width, must be a value within the interval (0,1).

Returns

The posterior prediction draws and summaries.

Return type

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
  • X (np.ndarray) -- design matrix of testing inputs.

  • ci (float) -- credible interval width, must be a value within the interval (0,1).

Returns

The posterior weight function draws and summaries.

Return type

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.

Param

None.

Returns

A dictionary of the hyperparameters used in the model.

Return type

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, overallsd: Optional[float] = None, overallnu: int = 10, inform_prior: bool = True, tauvec: Optional[bool] = None, betavec: Optional[bool] = None)[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
  • ntree (int) -- The number of trees used in the sum-of-trees model for the weights.

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

  • k (float) -- The tuning parameter in the prior variance of the terminal node parameter prior. This is a value greater than zero.

  • power (float) -- The power parameter in the tree prior.

  • base (float) -- The base parameter in the tree prior.

  • overallsd (float) -- An initial estimate of the erorr standard deviation. This value is used to calibrate the scale parameter in variance prior.

  • overallnu (float) -- The shape parameter in the error variance prior.

  • inform_prior (bool) -- Controls if the informative or non-informative prior is used. Specify true for the informative prior.

  • tauvec (np.ndarray) -- 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.

  • betavec (np.ndarray) -- 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

None.

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

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

Parameters
  • X (np.ndarray) -- input parameter values of dimension (n x p).

  • y (np.ndarray) -- observed data at inputs X of dimension (n x 1).

  • kwargs (dict) -- dictionary of arguments

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.

Return type

dict