StructuredAdditiveTerm

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, where coef_vector is equipped with a potentially singular multivariate normal prior, constructed using a penalty matrix K and a variance parameter tau2.

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 takes x 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

predict

Computes predicted values given an array of posterior samples.

pspline

Alternative constructor for quickly setting up a P-spline.

summarise_by_quantiles

Computes a posterior summary for this term based on quantiles.

summarise_by_samples

Computes a posterior summary for this term based on a subsample from the posterior.

Attributes

hyper_parameters

List of hyperparameter names associated with this term.

observed_value

Array of observed x values.

parameters

List of parameter names associated with this term.

value

Evaluation of this term.

x

Covariate node.

basis_fn

Basis function.

basis

Basis matrix node.

K

Penalty matrix node.

tau2

Variance parameter node.

coef

Coefficient node.

smooth

Smooth node.

mcmc_kernels

List of liesel.goose.kernel.Kernel MCMC kernel classes.