Generalized Additive Models

Michael BrenndoerferOctober 25, 20263 min read
Machine Learning from ScratchAll chapters
Part 5 of 15Linear and Generalized Linear Models
Chapter 39Generalized Additive Models
Reading section 1 of 10 · Generalized Additive Models · 3 minSection 1 of 10 · 3 min

Section 1 of 10 · 3 min read

Generalized Additive ModelsSection view · Sections in this chapter1 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

A regression can miss a pattern even when every predictor has a clear relationship with the response: a straight-line term cannot follow a sine-shaped association or a U-shaped one. Spline Regression gave us curves for an individual predictor. Combining several curves raises two further questions: how do we keep their contributions identifiable, and how do we choose their flexibility without judging them on the same observations used for fitting?

Generalized additive models (GAMs) fill that gap. They keep the additive structure of a linear model, replace each linear term with a smooth function, and let us pair that structure with the link functions familiar from generalized linear models. Formally, a GAM has the form

g(E[y∣x])=β0+f1(x1)+f2(x2)+⋯+fp(xp),g(\mathbb{E}[y \mid \mathbf{x}]) = \beta_0 + f_1(x_1) + f_2(x_2) + \cdots + f_p(x_p),

where:

  • gg: the link function connecting the conditional mean to the additive predictor
  • E[y∣x]\mathbb{E}[y \mid \mathbf{x}]: the conditional mean of the response given the predictors
  • β0\beta_0: the intercept
  • fjf_j: a smooth function of predictor xjx_j
  • xjx_j: the jj-th predictor value, with j=1,…,pj=1,\ldots,p and pp predictors

Two questions guide the implementation:

  • how to estimate the amount of flexibility (the smoothing parameter λ\lambda) without leaking information from validation into training,
  • how to interpret a fitted additive model, including what it cannot capture (interactions).

This chapter develops those ideas from first principles. We will build a GAM by hand using spline bases and a difference penalty, show how the fit reduces to a closed-form ridge-style solve, extend the framework to non-Gaussian responses via a link function, and demonstrate the whole pipeline on a synthetic dataset where the true component functions are known. Implementation is done in NumPy for the mechanism and compared against statsmodels' GLMGam for a reference. We will close with practical guidance on when GAMs make sense and what to watch out for.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy