magli¶
magli (Modelo Aditivo Generalizado Lineal) is a high-performance Python implementation of Generalized Additive Models supporting both Eilers & Marx (1996) and Wood (2011) backends.
GitHub Repository | PyPI Package | Getting-started notebook | Wood backend notebook | Eilers-Marx notebook
Contents:
What is magli?¶
magli fits smooth, additive regression models of the form
where each \(f_j\) is a flexible smooth function learned from data, penalised to avoid overfitting.
Penalised GAMs balance model flexibility against smoothness by capturing nonlinear structure without chasing random noise.¶
The package provides two complementary engines accessible through a single, unified interface
through GAM / LinGAM for single-term smooths and GAMCore for multi-term formulas:
Wood (2011) Backend (
backend='wood', default): Python implementation of Simon Wood’s GAM methods (Wood 2011, 2017). Supports multiple basis types ('tp','ts','cr','cs','cc','ps','cp','bs','re','te'), null-space constraint absorption, and automatic REML / GCV smoothing parameter optimization (no grid search required). Outputs formatted summary tables with approximate F-tests and p-values (Wood 2013).Eilers & Marx (1996) Backend (
backend='eilers'): Clean, fast B-splines with discrete GCV grid search and soft shape constraints (monotonic increasing/decreasing, convex, concave, periodic).
The package supports:
Univariate splines
s()for smooth nonlinear trendsTensor-product interactions
te()for multidimensional interactionsCategorical factors
f()with dummy or one-hot codingLinear terms
l()for parametric linear effectsShape constraints: monotonicity, convexity, concavity, periodicity
Fast evaluation: QR decomposition with SVD data-augmentation to avoid squaring condition numbers
Confidence & prediction intervals: Bayesian posterior covariance and frequentist intervals
When to use magli¶
You need more flexibility than linear regression but full interpretability without black-box complexity.
You want the exact statistical rigor and bases of Simon Wood’s GAM formulation in pure Python.
You have domain constraints (monotonic, concave, periodic) to enforce on model components.
You need to explain predictions to domain experts, where each term produces an isolated component plot.
Quick example¶
Using the Wood backend with automatic REML smoothing parameter estimation:
import numpy as np
from magli import LinGAM, GAMCore
np.random.seed(42)
x = np.linspace(0, 1, 200)
y = np.sin(2 * np.pi * x) + np.random.normal(0, 0.2, 200)
# Single-term model with cubic regression splines and REML estimation
model = LinGAM(k=10, bs="cr", backend="wood")
model.fit(x, y)
# Formatted summary table
print(model.summary())
# Predictions & Bayesian confidence intervals
y_hat = model.predict(x)
ci = model.confidence_intervals(x, width=0.95)
Multi-term formula with tensor products:
X = np.random.uniform(0, 10, (200, 3))
y_multi = np.sin(X[:, 0]) + 0.5 * X[:, 1] + 1.2 * X[:, 2] + np.random.normal(0, 0.2, 200)
gam = GAMCore("te(0, 1, k=5) + l(2)", backend="wood")
gam.fit(X, y_multi)
print(gam.summary())