term#
- class liesel_ptm.term(basis, penalty, scale, name, inference=None, coef_name=None, _update_on_init=True)[source]#
Bases:
UserVar
General structured additive term.
A structured additive term represents a smooth or structured effect in a generalized additive model. The term wraps a design/basis matrix together with a prior/penalty and a set of coefficients. The object exposes the coefficient variable and evaluates the term as the matrix-vector product of the basis and the coefficients. The term evaluates to
basis @ coef
.- Parameters:
basis (
Basis
) – ABasis
instance that produces the design matrix for the term. The basis must evaluate to a 2-D array with shape(n_obs, n_bases)
.penalty (
Var
|Value
|Any
) – Penalty matrix or a variable/value wrapping the penalty used to construct the multivariate normal prior for the coefficients.scale (
Var
|Any
) – Scale parameter for the prior on the coefficients. This is typically either a scalar or a per-coefficient scale variable.name (
str
) – Human-readable name for the term. Used for labelling variables and building sensible default names for internal nodes.inference (
Any
) –liesel.goose.MCMCSpec
inference specification forwarded to coefficient creation. (default:None
)coef_name (
str
|None
) – Name for the coefficient variable. IfNone
, a default name based onname
will be used. (default:None
)_update_on_init (
bool
) – IfTrue
(default) the internal calculation/graph nodes are evaluated during initialization. Set toFalse
to delay initial evaluation. (default:True
)
- Raises:
ValueError – If
basis.value
does not have two dimensions.
- scale#
The scale variable used by the prior on the coefficients.
- nbases#
Number of basis functions (number of columns in the basis matrix).
- basis#
The basis object provided to the constructor.
- coef#
The coefficient variable created for this term. It holds the prior (multivariate normal singular) and is used in the evaluation of the term.
- is_noncentered#
Whether the term has been reparameterized to the non-centered form.
Examples
Create a P-spline basis and wrap it into a term:
# assume x to be a 1d array. b = ps(x, nbases=10) t = term(basis=b, penalty=b.penalty, name="f(x)")
Methods
Construct a smooth term from a
Basis
.Construct a smooth term with an inverse-gamma prior on the variance.
Turns this term into noncentered form, which means the prior for the coefficient will be turned from
coef ~ N(0, scale^2 * inv(penalty))
intolatent_coef ~ N(0, inv(penalty)); coef = scale * latent_coef
.Attributes