StructuredAdditiveTerm#
- class liesel_ptm.StructuredAdditiveTerm(x, basis_fn, K, tau2, name, mcmc_kernel=<class 'liesel.goose.nuts.NUTSKernel'>)[source]#
Bases:
Term
Term in a structured additive predictor.
This term has the form
basis_matrix @ coef_vector
, wherecoef_vector
is equipped with a potentially singular multivariate normal prior, constructed using a penalty matrixK
and a variance parametertau2
.The user supplies a callable for the basis matrix, because this allows the class to easily evaluate the basis with manually chosen covariate values for predictions.
- Parameters:
x (
Any
) – Covariate array.basis_fn (
Callable
[[Any
],Any
]) – A function that takesx
and returns an array of basis function evaluations.K (
Any
) – Penalty matrix.tau2 (
Var
) – Variance parameter for the singular normal prior.name (
str
) – Unique name of the term.mcmc_kernel (
Kernel
) – Kernel class to use by default. (default:<class 'liesel.goose.nuts.NUTSKernel'>
)
See also
bspline_basis
Evaluates B-spline basis functions.
kn
Creates equidistant knots.
diffpen
Evaluates a differences-penalty matrix.
sumzero_term
Reparameterization matrix for sum-to-zero constraint.
sumzero_coef
Reparameterization matrix for sum-to-zero constraint.
nullspace_remover
Reparameterization matrix for sum-to-zero constraint.
Examples
In this example, we define a P-spline term using B-spline bases and a second-order random walk penalty matrix. We reparameterize both the basis and the penalty matrix
K
to enforce a sum-to-zero constraint on the term:import numpy as np import tensorflow_probability.substrates.jax.distributions as tfd import liesel.model as lsl import liesel_ptm as ptm np.random.seed(2407) x = np.random.uniform(low=-1.0, high=1.0, size=300) nparam = 10 knots = ptm.kn(x, n_params=nparam) Z = ptm.sumzero_term(ptm.bspline_basis(x, knots, 3)) K = Z.T @ ptm.diffpen(nparam, diff=2) @ Z def basis_fn(x): return ptm.bspline_basis(x, knots, 3) @ Z tau2 = ptm.VarWeibull(10.0, scale=0.05, name="tau2") term = ptm.StructuredAdditiveTerm( x=x, basis_fn=basis_fn, K=K, tau2=tau2, name="my_term" )
Methods
Computes predicted values given an array of posterior samples.
Alternative constructor for quickly setting up a P-spline.
Computes a posterior summary for this term based on quantiles.
Computes a posterior summary for this term based on a subsample from the posterior.
Attributes
List of hyperparameter names associated with this term.
Array of observed
x
values.List of parameter names associated with this term.
Evaluation of this term.
Covariate node.
Basis function.
Basis matrix node.
Penalty matrix node.
Variance parameter node.
Coefficient node.
Smooth node.
List of
liesel.goose.kernel.Kernel
MCMC kernel classes.