pcntoolkit.math_functions.basis_function#
Classes#
Helper class that provides a standard way to create an ABC using |
|
B-spline basis function for nonlinear covariate effects. |
|
Combine basis functions and optionally construct pairwise interactions. |
|
Fractional polynomial basis function for modelling smooth nonlinear |
|
Helper class that provides a standard way to create an ABC using |
|
Helper class that provides a standard way to create an ABC using |
Functions#
|
Module Contents#
- class BasisFunction(basis_column: int = 0, **kwargs)#
Bases:
abc.ABCHelper 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#
- transform(X: numpy.ndarray) numpy.ndarray#
- basis_column = 0#
- abstract property dimension#
- 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:
BasisFunctionB-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, default0) – Index of the covariate to which the B-spline transformation is applied.degree (
int, default3) – Degree of the B-spline basis.nknots (
int, default5) – Number of knots used to construct the basis.left_expand (
float, default0.05) – Fraction by which the lower boundary is expanded beyond the observed data range.right_expand (
float, default0.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-likeorNone, defaultNone) – Predefined knot sequence. If None, knots are estimated during fitting.include_linear (
bool, defaultFalse) – 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_methodis not"uniform"or"quantile".
- 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:
BasisFunctionCombine 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)meansparts[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 (
listofBasisFunction) – Each element is a basis function that operates on a specific covariate column.interactions (
listoftuple[int,int], optional) – Pairs of indices intoparts(not covariate columns). For each pair(i, j), the products of all columns ofparts[i]andparts[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
0tolen(parts) - 1. Also raised bytransformif called beforefit.
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:
BasisFunctionFractional 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:
- AGE_FP_POWER_PRESETS#
- DEFAULT_POWER_SET#
- basis_name = 'fractional_polynomial'#
- eps#
- order = 3#
- power_set#
- class LinearBasisFunction(basis_column: int = 0, **kwargs)#
Bases:
BasisFunctionHelper class that provides a standard way to create an ABC using inheritance.
- basis_name = 'linear'#
- property dimension#