Language: English
Last updated: 2026-05-28
This page: Model documentation
Switch: Chinese
Language switch: Chinese
GAM fits a Generalized Additive Model using penalized B-splines with automatic smoothing parameter selection via Generalized Cross-Validation (GCV). The model is:
where each
For the underlying B-spline basis utilities, see Spline Basis Functions.
statgpu.semiparametric.GAM
GAM fits a penalized least-squares model:
where
The first-order condition of the penalized objective yields the system
solved via Cholesky factorization.
GCV for lambda selection (when lam=None):
where the effective degrees of freedom is
Lambda is selected by minimizing GCV over a log-spaced grid.
edf_: effective degrees of freedom of the fitted model.gcv_score_: GCV score (available when lambda is auto-selected).lam_: smoothing parameter used for the final fit.- No coefficient-level standard errors or p-values are produced; the GAM is a smoother, not a parametric inference tool.
| Parameter | Default | Description |
|---|---|---|
n_splines |
20 |
Number of spline basis functions per feature |
degree |
3 |
Spline degree |
lam |
None |
Smoothing parameter; auto-selected via GCV if None |
penalty_order |
2 |
Order of the difference penalty matrix |
device |
"auto" |
cpu / cuda / auto |
from statgpu.semiparametric import GAM
import numpy as np
X = np.random.randn(500, 3)
y = np.sin(X[:, 0] * 3) + 0.1 * np.random.randn(500)
# GAM (CPU)
gam = GAM(n_splines=20, device='cpu')
gam.fit(X, y)
print(f"EDF: {gam.edf_:.1f}, GCV: {gam.gcv_score_:.6f}")
y_pred = gam.predict(X)
# GAM (GPU)
gam_gpu = GAM(n_splines=20, device='cuda')
gam_gpu.fit(X, y)
y_pred_gpu = gam_gpu.predict(X)- When
lam=None(default), GAM uses GCV over a log-spaced grid (1e-10 to 1e10, 100 points) to select the smoothing parameter. This is the approximate path; the grid is coarse and may miss the optimal lambda in narrow valleys. - When
lamis specified manually, the exact penalized least-squares solution is computed for that single value. This is the exact path. - For fine-tuning beyond the default grid, pass a custom
lamvalue obtained from a narrower search or domain knowledge.
GAM fitted attributes:
| Attribute | Type | Description |
|---|---|---|
coef_ |
array, shape (1 + sum(n_basis_j),) |
Concatenated spline coefficients (including intercept) |
intercept_ |
float | Intercept term |
edf_ |
float | Effective degrees of freedom |
gcv_score_ |
float | GCV score at the selected lambda |
lam_ |
float | Smoothing parameter used |
knots_ |
list of arrays | Knot locations per feature |
n_features_ |
int | Number of input features |
Methods: fit(X, y), predict(X), summary().
- How many knots should I use?
n_splines=20is a good default. More knots give more flexibility but increase effective degrees of freedom and risk overfitting. - What penalty order should I use?
penalty_order=2(second differences) is standard for smooth functions. Usepenalty_order=1for piecewise-linear fits. - GPU speedup? The GAM solve is dominated by the Cholesky factorization, which benefits from GPU acceleration for large basis dimensions.
- GAM predictions validated against pyGAM on standard test datasets.
- Hastie, T., & Tibshirani, R. (1990). Generalized Additive Models. Chapman & Hall.
- Wood, S. N. (2017). Generalized Additive Models: An Introduction with R (2nd ed.). Chapman & Hall/CRC.