pcntoolkit.math_functions.basis_function#

Classes#

BasisFunction

Helper class that provides a standard way to create an ABC using

BsplineBasisFunction

B-spline basis function for nonlinear covariate effects.

CompositeBasisFunction

Combine basis functions and optionally construct pairwise interactions.

FractionalPolynomialBasisFunction

Fractional polynomial basis function for modelling smooth nonlinear

LinearBasisFunction

Helper class that provides a standard way to create an ABC using

PolynomialBasisFunction

Helper class that provides a standard way to create an ABC using

Functions#

create_basis_function(→ BasisFunction)

Module Contents#

class BasisFunction(basis_column: int = 0, **kwargs)#

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

fit(X: numpy.ndarray) → None#
classmethod from_args(name: str, args: dict) → BasisFunction#
classmethod from_dict(my_dict: dict, version: str | None = None) → BasisFunction#
to_dict() → dict#
transform(X: numpy.ndarray) → numpy.ndarray#
basis_column = 0#
basis_name: str#
compute_max: bool#
compute_min: bool#
abstract property dimension#
is_fitted: bool#
max: float | None#
min: float | None#
class BsplineBasisFunction(basis_column: int = 0, degree: int = 3, nknots: int = 5, left_expand: float = 0.05, right_expand: float = 0.05, knot_method: str = 'uniform', knots: numpy.ndarray | list = None, include_linear: bool = False, **kwargs)#

Bases: BasisFunction

B-spline basis function for nonlinear covariate effects.

Transforms the selected covariate into a B-spline design matrix using the specified degree and knot configuration.

By default, only the B-spline basis functions are returned. The original covariate can optionally be included as an additional linear term by setting include_linear=True. Since linear functions are already contained in the span of the B-spline basis, including this term may introduce linear dependence and is primarily provided for backward compatibility with models fitted using earlier PCNtoolkit versions. See issue #542

Parameters:
  • basis_column (int, default 0) – Index of the covariate to which the B-spline transformation is applied.

  • degree (int, default 3) – Degree of the B-spline basis.

  • nknots (int, default 5) – Number of knots used to construct the basis.

  • left_expand (float, default 0.05) – Fraction by which the lower boundary is expanded beyond the observed data range.

  • right_expand (float, default 0.05) – Fraction by which the upper boundary is expanded beyond the observed data range.

  • knot_method ({"uniform", "quantile"}, default "uniform") – Method used to determine knot locations.

  • knots (array-like or None, default None) – Predefined knot sequence. If None, knots are estimated during fitting.

  • include_linear (bool, default False) – If True, prepend the original covariate to the B-spline design matrix. This reproduces the behavior of earlier PCNtoolkit versions and may introduce a redundant column.

Raises:

ValueError – If knot_method is not "uniform" or "quantile".

to_dict() → dict#
basis_name = 'bspline'#
degree = 3#
property dimension#
include_linear = False#
knot_method = 'uniform'#
left_expand = 0.05#
nknots = 5#
right_expand = 0.05#
class CompositeBasisFunction(parts: list[BasisFunction], interactions: list[tuple[int, int]] | None = None)#

Bases: BasisFunction

Combine basis functions and optionally construct pairwise interactions.

An interaction lets the effect of one covariate depend on another, e.g. a different age curve for each sex. (0, 1) means parts[0] x parts[1].

Output columns: first each covariate’s basis (in covariate order), then the interaction columns. A covariate with no part is copied unchanged. Example with covariates [age, sex, x3]:

parts=[BsplineBasisFunction(basis_column=0),   # age, 7 columns
       LinearBasisFunction(basis_column=1)]    # sex, 1 column
interactions=[(0, 1)]

columns 0-6   age B-spline
column  7     sex
column  8     x3, copied unchanged (no part covers it)
columns 9-15  age B-spline x sex
Parameters:
  • parts (list of BasisFunction) – Each element is a basis function that operates on a specific covariate column.

  • interactions (list of tuple[int, int], optional) – Pairs of indices into parts (not covariate columns). For each pair (i, j), the products of all columns of parts[i] and parts[j] are appended to the output. Default: no interactions.

Raises:

ValueError – If an interaction is not a pair of integers, or refers to a part index outside 0 to len(parts) - 1. Also raised by transform if called before fit.

Notes

Interaction terms are mathematical products of component basis outputs. Linear dependencies in the resulting design matrix are not detected or removed automatically.

fit(X)#
to_dict()#
transform(X)#
property dimension#
interactions = []#
parts#
class FractionalPolynomialBasisFunction(basis_column: int = 0, order: int = 3, powers: list | tuple | str | None = 'default', power_set: list | tuple | None = None, eps: float = 1e-08, **kwargs)#

Bases: BasisFunction

Fractional polynomial basis function for modelling smooth nonlinear effects.

The input must be strictly positive (do not standardize the covariates). Power convention:

p = 0 -> log(x) p != 0 -> x**p

Repeated powers:

[p, p, p] -> x**p, x**p * log(x), x**p * log(x)**2

Initialise the fractional polynomial basis function.

Parameters:
  • basis_column (int, default 0) – Column index to transform.

  • order (int, default 3) – Fractional polynomial order. Must be 1, 2, or 3.

  • powers (list, tuple, str, or None, default "default")

  • power_set (list, tuple, or None, default None) – Allowed fractional polynomial powers.

  • eps (float, default 1e-8) – Numerical stability constant.

to_dict() → dict#

Serialize the basis function configuration.

AGE_FP_POWER_PRESETS#
DEFAULT_POWER_SET#
basis_name = 'fractional_polynomial'#
property dimension: int#

Number of generated basis columns.

eps#
order = 3#
power_set#
class LinearBasisFunction(basis_column: int = 0, **kwargs)#

Bases: BasisFunction

Helper class that provides a standard way to create an ABC using inheritance.

basis_name = 'linear'#
property dimension#
class PolynomialBasisFunction(basis_column: int = 0, degree: int = 3, **kwargs)#

Bases: BasisFunction

Helper class that provides a standard way to create an ABC using inheritance.

basis_name = 'poly'#
degree = 3#
property dimension#
create_basis_function(basis_type: str | dict | None, basis_column: int = 0, **kwargs) → BasisFunction#