AdditivePredictor

AdditivePredictor#

class liesel_gam.AdditivePredictor(name, inv_link=None, intercept=True, intercept_name='$\\\\beta{subscript}$')[source]#

Bases: UserVar

A Liesel Var that represents an additive predictor.

This is a special variable that allows you to add other Liesel varibales using the += syntax (see examples).

Parameters:
  • name (str) – Name of the predictor variable.

  • inv_link (Callable[[Any], Any] | None, default: None) – Inverse link function. If supplied, variables are added on the link level, and the predictor variable’s value will be inv_link(sum(*inputs)), where *inputs refers to the additive terms in this predictor.

  • intercept (bool | Var, default: True) – Whether this predictor should be initialized with an intercept. You can supply booleans, or a liesel.model.Var. In the latter case, this var is taken as the intercept. The default intercept has a constant prior.

  • intercept_name (str, default: '$\\\\beta{subscript}$') – Name of the automatically created intercept variable (if intercept=True). If this name contains the placeholder {subscript}, it will be filled with the predictor name to create a unique intercept name for this predictor.

Examples

Basic example:

>>> import liesel_gam as gam
>>> import liesel.model as lsl
>>> loc = gam.AdditivePredictor("loc")

Now we add a variable using the + syntax. The value of the predictor is the sum of the values of its inputs. >>> loc += lsl.Var.new_value(1.0, name=”s(x)”) >>> loc.value 1.0

The input terms can be accessed:

>>> loc.terms
{'s(x)': Var(name="s(x)")}

This term got initialized with a default intercept, which has no distribution node (corresponding to a constant prior).

>>> loc.intercept
Var(name="$\beta_{0,loc}$")
>>> print(loc.intercept.dist_node)
None

After adding a second term, the value of the predictor is updated:

>>> loc += lsl.Var.new_value(2.5, name="s(x2)")
>>> loc.terms
{'s(x)': Var(name="s(x)"), 's(x2)': Var(name="s(x2)")}
>>> loc.value
3.5

Using an inverse link function:

>>> import jax.numpy as jnp
>>> import liesel.model as lsl
>>> import liesel_gam as gam
>>> scale = gam.AdditivePredictor("scale", inv_link=jnp.exp)
>>> scale += lsl.Var.new_value(1.0, name="s(x)")
>>> scale.terms
{'s(x)': Var(name="s(x)")}
>>> scale.intercept
Var(name="$\beta_{0,scale}$")
>>> scale.value
Array(2.7182817, dtype=float32, weak_type=True)

Using a custom intercept:

>>> import liesel.model as lsl
>>> import tensorflow_probability.substrates.jax.distributions as tfd
>>> import liesel_gam as gam
>>> intercept_var = lsl.Var.new_param(
...     value=3.0,
...     distribution=lsl.Dist(tfd.Normal, loc=0.0, scale=10.0),
...     name="b0",
... )
>>> loc = gam.AdditivePredictor("loc", intercept=intercept_var)
>>> loc.intercept
Var(name="b0")
>>> loc.value
3.0
>>> loc.intercept.dist_node
Dist(name="b0_log_prob")

Multiple terms can be added at the same time:

>>> import liesel.model as lsl
>>> import liesel_gam as gam
>>> loc = gam.AdditivePredictor("loc")
>>> sx1 = lsl.Var.new_value(1.0, name="s(x1)")
>>> sx2 = lsl.Var.new_value(1.0, name="s(x2)")
>>> loc += sx1, sx2
>>> loc.terms
{'s(x1)': Var(name="s(x1)"), 's(x2)': Var(name="s(x2)")}

Methods

append

Appends a single term to this predictor's sum inputs.

extend

Appends a sequence of terms to this predictor's sum inputs.

update

Updates the variable.

Attributes

intercept

This term's intercept.

terms

Dictionary of terms in this predictor.