Family

Family(name, likelihood, link)

A specification of model family

Parameters

name : str

The name of the family. It can be any string.

likelihood : Likelihood

A bambi.families.Likelihood instance specifying the model likelihood function.

link : str or dict of str to (str or Link)

The link function that’s used for every parameter in the likelihood function. Keys are the names of the parameters and values are the link functions. These can be a str with a name or a bambi.families.Link instance. The link function transforms the linear predictors.

Examples

import bambi as bmb

Replicate the Gaussian built-in family.

sigma_prior = bmb.Prior("HalfNormal", sigma=1)
likelihood = bmb.Likelihood("Gaussian", params=["mu", "sigma"], parent="mu")
family = bmb.Family("gaussian", likelihood, "identity")
bmb.Model("y ~ x", data, family=family, priors={"sigma": sigma_prior})

Replicate the Bernoulli built-in family.

likelihood = bmb.Likelihood("Bernoulli", parent="p")
family = bmb.Family("bernoulli", likelihood, "logit")
bmb.Model("y ~ x", data, family=family)

Attributes

Name Description
auxiliary_parameters Get names of auxiliary parameters

Methods

Name Description
log_likelihood Evaluate the model log-likelihood
posterior_predictive Get draws from the posterior predictive distribution
set_default_priors Set default priors for non-parent parameters

log_likelihood

Family.log_likelihood(model, posterior, data, **kwargs)

Evaluate the model log-likelihood

This method uses pm.logp().

Parameters

model : bambi.Model

The model

posterior : xr.Dataset

The xarray dataset that contains the draws for all the parameters in the posterior. It must contain the parameters that are needed in the distribution of the response, or the parameters that allow to derive them.

data : pd.DataFrame or None

A data frame with values for the predictors and the response on which the model’s log-likelihood function is evaluated. If omitted, the original dataset is used.

kwargs : dict = {}

Parameters that are used to get draws but do not appear in the posterior object or other configuration parameters. For instance, the ‘n’ in binomial models and multinomial models.

Returns

: xr.DataArray

A data array with the value of the log-likelihood for each chain, draw, and value of the response variable.

posterior_predictive

Family.posterior_predictive(model, posterior, random_seed, **kwargs)

Get draws from the posterior predictive distribution

This function works for almost all the families. It grabs the draws for the parameters needed in the response distribution, and then gets samples from the posterior predictive distribution using pm.draw(). It won’t work when the response distribution requires parameters that are not available in posterior.

Parameters

model : bambi.Model

The model

posterior : xr.Dataset

The xarray dataset that contains the draws for all the parameters in the posterior. It must contain the parameters that are needed in the distribution of the response, or the parameters that allow to derive them.

random_seed : (int, RandomState or Generator)

Seed for the random number generator.

kwargs : dict = {}

Parameters that are used to get draws but do not appear in the posterior object or other configuration parameters. For instance, the ‘n’ in binomial models and multinomial models.

Returns

: xr.DataArray

A data array with the draws from the posterior predictive distribution.

set_default_priors

Family.set_default_priors(priors)

Set default priors for non-parent parameters

Parameters

priors : dict

The keys are the names of non-parent parameters and the values are their default priors.