BSplineApprox

Contents

BSplineApprox#

class liesel_gam.experimental.BSplineApprox(knots, degree=3, ngrid=1000, Z=None, subscripts='...ij,...j->...i')[source]#

Bases: object

Approximate B-spline evaluations on a fixed grid.

Parameters:
  • knots (Array) – Knot positions.

  • degree (int, default: 3) – Degree of the spline, with degree=3 indicating a cubic spline.

  • ngrid (int, default: 1000) – Number of grid points used to precompute the basis (default 1000).

  • Z (Array | None, default: None) – Optional matrix to post-multiply the basis. In B(x) @ Z, B(x) is the basis matrix and Z is the postmultiplication matrix. Can be used to apply linear constraints via reparameterization matrices such as those returned by LinearConstraintEVD.

See also

LinearConstraintEVD

Compute reparameterization matrices for linear constraints.

Examples

Basic example.

>>> import jax
>>> from liesel.contrib.splines import equidistant_knots
>>> import liesel_gam as gam
>>> nbases = 20
>>> x = jax.random.uniform(jax.random.key(1234), shape=(40,))
>>> coef = jax.random.normal(jax.random.key(4321), shape=(nbases,))
>>> knots = equidistant_knots(x, n_param=nbases)
>>> bspline = gam.experimental.BSplineApprox(knots)
>>> bspline.dot(x, coef).shape
(40,)

Applying a linear constraint matrix.

>>> import jax
>>> from liesel.contrib.splines import basis_matrix, equidistant_knots
>>> import liesel_gam as gam
>>> nbases = 20
>>> x = jax.random.uniform(jax.random.key(1234), shape=(40,))
>>> coef = jax.random.normal(jax.random.key(4321), shape=((nbases - 1),))
>>> knots = equidistant_knots(x, n_param=nbases)
>>> basis = basis_matrix(x, knots)
>>> Z = gam.LinearConstraintEVD.sumzero_term(basis)
>>> bspline = gam.experimental.BSplineApprox(knots, Z=Z)
>>> bspline.dot(x, coef).shape
(40,)

Methods

approx_basis

Approximates B-spline basis for input data.

approx_basis_and_deriv

Approximates basis and matrix of first derivatives.

approx_basis_and_deriv2

Approximates basis matrix and matrices of first and second derivatives.

compute_basis

Computes the basis matrix.

compute_deriv

Computes the matrix of basis function derivatives with respect to x.

compute_deriv2

Computes the matrix of basis function second derivatives with respect to x.

dot

Evaluate spline at given points.

dot_and_deriv

Evaluate spline and its derivative.