# Formula

Model formula

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/formula.py#L8-L169)

``` python
Formula(
    formula,
    *additionals,
    nlpars=None,
)
```

Allows to describe a model with multiple formulas. The first formula describes the response variable and its predictors. The following formulas describe predictors for other parameters of the response distribution, allowing distributional models.

## Parameters

`formula: str`  
A model description written using the formula syntax from the `formulae` library.

`*additionals: tuple of str`  
Additional formulas that describe model parameters rather than a response variable.

`nlpars: list or tuple of str = None`  
Names of coefficients used in nonlinear expressions. Terms that reference modeled parameters use arithmetic syntax; other terms retain ordinary Formulae syntax. Use `nl(expression)` to add an arithmetic expression directly to the predictor, without an estimated multiplier. For example, `y ~ nl(exp(b * x) + x) + treatment` estimates `b` and the usual intercept and treatment effects, but uses `x` directly inside the wrapper. Use `0 + nl(expression)` to omit the additive intercept. Parentheses outside `nl` retain ordinary formula grouping semantics. A formula consisting entirely of unwrapped arithmetic terms and numeric constants retains its expression-only interpretation, with no implicit intercept. An additional formula can describe how a nonlinear coefficient varies. Coefficients without an additional formula have a direct prior and are sampled under their own names. Coefficients with a formula use priors for its terms, including intercept-only formulas. Additional formulas can also describe ordinary auxiliary likelihood parameters, such as `sigma ~ z`. Each expression is on its parameter's link scale. The family's inverse link is applied once to the complete predictor, including auxiliary parameters. An additional formula that references a modeled parameter is evaluated after its dependencies, using their values on the response scale. For example, with the log link for Gaussian `sigma`, write `sigma ~ 0 + nl(0.5 * log(variance))` for a known variance column. The inverse link produces its square root. Separately modeled nonlinear coefficients use identity links.

## Notes

Nonlinear coefficients are scalar by default and do not inherit dimensions from the likelihood parameters that use them. Additive coefficient formulas produce one value per observation, shared across response categories. Nonlinear coefficient expressions can reference vector-valued modeled parameters and retain those dimensions.

Nonlinear formulas support categorical, count-vector, ordinal, and compatible custom numeric vector families. Weighted, censored, truncated, and constrained scalar responses are supported where the ordinary family supports the wrapper. New-data weighted predictions use unit weights unless a weight column is supplied. See the nonlinear expressions guide for supported dimensions, competing risks, and remaining restrictions.

## Examples

Model an exponential decay with three separately modeled parameters:

``` python
>>> Formula("y ~ a + b * exp(-k * x)", "a ~ 1 + z", nlpars=("a", "b", "k"))
Formula('y ~ a + b * exp(-k * x)', 'a ~ 1 + z', nlpars=('a', 'b', 'k'))
```

Combine a nonlinear contribution with ordinary additive effects:

``` python
>>> Formula("y ~ nl(exp(b * x) + x) + treatment", nlpars=("b",))
Formula('y ~ nl(exp(b * x) + x) + treatment', nlpars=('b',))
```

## Attributes

| Name | Description |
|----|----|
| [is_nonlinear](#bambi.Formula.is_nonlinear) | Whether the formulas declare nonlinear coefficients or explicit contributions. |

### is_nonlinear

Whether the formulas declare nonlinear coefficients or explicit contributions.

`is_nonlinear`

## Methods

| Name | Description |
|----|----|
| [check_additional()](#bambi.Formula.check_additional) | Check if an additional formula matches the expected format |
| [get_all_formulas()](#bambi.Formula.get_all_formulas) | Get all the model formulas |

### check_additional()

Check if an additional formula matches the expected format

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/formula.py#L113-L145)

``` python
check_additional(additional)
```

#### Parameters

`additional: str`  
A model formula that describes a model parameter.

#### Returns

`str`  
The parameter name on the left-hand side.

#### Raises

`ValueError`  
If the formula does not contain a response term.

`ValueError`  
If the response term is not a plain name.

### get_all_formulas()

Get all the model formulas

Usage

[Source](https://github.com/bambinos/bambi/blob/main/bambi/formula.py#L147-L155)

``` python
get_all_formulas()
```

#### Returns

`list of str`  
All the formulas in the instance.
