Taweret.mix.bivariate_linear
- 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:
BaseMixerLocal 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_priorfor 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