ps()

Contents

ps()#

liesel_ptm.ps(x, nbases, xname=None, knots=None, order=3, outer_ok=False, penalty=None, diagonalize_penalty=True, scale_penalty=True, constraint='sumzero_term', constraint_matrix=None)[source]#

Create a P-spline basis.

Convenience function to build a B-spline/P-spline basis for a numeric predictor. It constructs knots (if not provided), builds a basis function that evaluates the B-spline basis, attaches a difference-penalty (by default a second-order random-walk penalty), and returns a Basis instance. Optional post-processing steps like scaling the penalty, diagonalization and constraint application are supported and provided with sensible defaults.

Parameters:
  • x (Union[Array, ndarray, bool, number, bool, int, float, complex, Series, Var]) – Predictor values or an observed variable. If an array or Series is provided, xname should be set to produce a named observation variable.

  • nbases (int) – Number of basis functions (parameters) to use for the P-spline.

  • xname (str | None) – Name for the created observation variable when x is not already a liesel.model.Var. (default: None)

  • knots (Union[Array, ndarray, bool, number, bool, int, float, complex, None]) – Knot locations for the B-spline basis. If None equidistant knots are generated based on the data and nbases. (default: None)

  • order (int) – Order of the B-spline (degree + 1). Default is 3 (cubic splines). (default: 3)

  • outer_ok (bool) – Passed to the underlying basis generator to control behavior for observed values outside the internal knot range. (default: False)

  • penalty (Union[Array, ndarray, bool, number, bool, int, float, complex, None]) – Penalty matrix to use. If None a default P-spline penalty is created using a second-order difference. (default: None)

  • diagonalize_penalty (bool) – If True (default), diagonalize the penalty. (default: True)

  • scale_penalty (bool) – If True (default), scale the penalty matrix to improve numeric conditioning. (default: True)

  • constraint (Optional[Literal['sumzero_term', 'sumzero_coef', 'constant_and_linear', 'custom']]) – Optional constraint to apply to the basis (see Basis.reparam_constraint()). If None no constraint is applied. (default: 'sumzero_term')

  • constraint_matrix (Union[Array, ndarray, bool, number, bool, int, float, complex, None]) – Custom constraint matrix used when constraint=='custom'. (default: None)

Return type:

Basis

Returns:

A configured Basis instance representing the P-spline basis.