Taweret.core namespace

Submodules

class Taweret.core.base_mixer.BaseMixer[source]

Bases: ABC

abstractmethod evaluate()[source]

Calculate a point estimate for mixing model

Returns:

evaluationfloat

point estimate from mixing model

Example:

Global linear mixing:

\[f_\mathrm{mixed} = \sum_{i=1}^N w_i \mathcal M_i(\theta_i)\]
class MyMixer(BaseMixer):
    @property
    def models(self):
        return self._model_list
    @models.setter
    def models(self, model_list):
        self._model_list = model_list
    def evaluate(self, weights, model_parameters):
        value = 0
        for i in range(len(self._model_list)):
            value += self._model_list[i](
                x, model_parameters[i]
            ) * weight[i]
        return value
abstractmethod evaluate_weights()[source]

Calculate or sample a point estimate for model weights

Returns:

weightsnp.ndarray

array of the evaluations or samples of weights

Example:

Global linear mixing:

import scipy
class MyMixer(BaseMixer):
    # . . .
    def evaluate_weights(self, dirichlet_params):
        return scipy.stats.dirichlet.rvs(dirichlet_params)
    # . . .
abstract property map

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

abstract property posterior

Stores the most recent posteriors from running self.train function

Returns:

_posteriornp.ndarray

posterior from learning the weights

abstractmethod predict()[source]

Evaluate posterior to make prediction at test points.

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

Example:

Global liner mixing:

class MyMixer(BaseMixer):
    # . . .
    def predict(self, x_test):
        # work to calculate everything
        # . . .
        return posterior, means, credible_intervals, std_dev
abstractmethod predict_weights()[source]

Calculate posterior predictive distribution for model weights

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

Example:

Global liner mixing:

class MyMixer(BaseMixer):
    # . . .
    def predict_weights(self, x_test):
        # work to calculate everything
        # . . .
        return posterior, means, credible_intervals, std_dev
abstract 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

abstractmethod prior_predict()[source]

Get prior predictive distribution and prior distribution samples

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

Example:

Global liner mixing:

class MyMixer(BaseMixer):
    # . . .
    def prior_predict(self, x_test):
        # work to calculate everything
        # . . .
        return prior, means, credible_intervals, std_dev
abstractmethod set_prior()[source]

User must provide function that sets a member variable called _prior. Dictionary of prior distributions. Format should be compatible with sampler.

Example:

class MyMixer(BaseMixer):
    # . . .
    def set_prior(self, prior_dict):
        self._prior = prior_dict
    # . . .
# creating a bilby prior dict
priors = dict()
priors['a'] = bilby.core.prior.MultivariateGaussian(mvg, 'a')
priors['b'] = bilby.core.prior.MultivariateGaussian(mvg, 'b')
m = MyMixer()
m.set_prior(prior_dict=priors)
abstractmethod train()[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.

Returns:

_posteriornp.ndarray

the mcmc chain return from sampler

class Taweret.core.base_model.BaseModel[source]

Bases: ABC

abstractmethod evaluate()[source]

Calculate a point estimate for mixing model

Returns:

evaluationfloat

point estimate for model

Example

class MyModel(BaseModel):
    @property
    def model(self):
        return self._model

    @models.setter
    def model(self, the_model):
        self._model = the_model

    def evaluate(self, model_parameters):
        return self._model(model_parameters)
    # . . .
abstractmethod log_likelihood_elementwise()[source]

Calculate log_likelihood for array of points given

Returns:

log_likelisnp.ndarray

an array of length as shape[0] of the input evaluation points

Example:

class MyModel(BaseModel):
    def log_likelihood_elementwise(
        self, y_exp, y_err, model_params
    ):
        # Assuming a normal distribution for error
        y = self.evaluate(model_params)
        # If y_exp, y_err, y are numpy arrays of same length
        return np.exp(-(y - y_exp) **2 / (2 * y_err ** 2)) \
            / np.sqrt(2 * np.pi * y_err ** 2))
abstractmethod set_prior()[source]

User must provide function that sets a member variable called _prior. Dictionary of prior distributions. Format should be compatible with sampler.

Example:

class MyModel(BaseMixer):
    # . . .
    def set_prior(self, prior_dict):
        self._prior = prior_dict
    # . . .