# Family

A specification of model family

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/families/family.py#L8-L212)

``` python
Family(
    name,
    likelihood,
    link,
)
```

## 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

``` python
import bambi as bmb
```

Replicate the Gaussian built-in family.

``` python
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.

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

## Attributes

| Name | Description |
|----|----|
| [auxiliary_parameters](#bambi.Family.auxiliary_parameters) | Get names of auxiliary parameters |

### auxiliary_parameters

Get names of auxiliary parameters

`auxiliary_parameters`

Obtains the difference between all the parameters and the parent parameter of a family. These parameters are known as auxiliary or nuisance parameters.

## Methods

| Name | Description |
|----|----|
| [conditional_moments()](#bambi.Family.conditional_moments) | Get the conditional mean and variance of the response |
| [get_param_spec()](#bambi.Family.get_param_spec) | Return metadata for a likelihood parameter. |
| [moments()](#bambi.Family.moments) | Compute the conditional mean and variance of the response |
| [set_default_priors()](#bambi.Family.set_default_priors) | Set default priors for non-parent parameters |

### conditional_moments()

Get the conditional mean and variance of the response

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/families/family.py#L127-L174)

``` python
conditional_moments(
    model,
    posterior,
)
```

Computes the mean and the variance of the response conditional on the parameters, $`E(Y \mid \theta)`$ and $`\mathrm{Var}(Y \mid \theta)`$, for every posterior draw and observation. This is used, for example, to compute the Bayesian $`R^2`$.

#### 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 of the response distribution for the original data.

#### Returns

`tuple of xr.DataArray`  
The conditional mean and the conditional variance of the response.

#### Raises

`NotImplementedError`  
If the conditional moments are not implemented or not defined for the family.

### get_param_spec()

Return metadata for a likelihood parameter.

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/families/family.py#L101-L114)

``` python
get_param_spec(param_name)
```

Declared metadata is returned unchanged. When no metadata is declared, parameters are scalar, have no coefficient dimension, and accept all registered string links.

### moments()

Compute the conditional mean and variance of the response

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/families/family.py#L176-L205)

``` python
moments(params)
```

Families that support the Bayesian $`R^2`$ override this method.

#### Parameters

`params: dict of str to xr.DataArray`  
Maps the names of the parameters of the likelihood to their values on the response scale (i.e. after applying the inverse link functions). Binomial-type families also get the number of trials as `"n"`.

#### Returns

`tuple of xr.DataArray`  
The conditional mean and the conditional variance of the response. Families with a circular response (`CIRCULAR = True`) return the circular variance instead.

#### Raises

`NotImplementedError`  
When the moments are not defined or not implemented for the family.

### set_default_priors()

Set default priors for non-parent parameters

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/families/family.py#L116-L125)

``` python
set_default_priors(priors)
```

#### Parameters

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