Skip to content

fix: incorrect q=2 coefficient in PiecewisePolynomialKernel#2756

Merged
gpleiss merged 2 commits into
cornellius-gp:mainfrom
devteamaegis:fix/piecewise-polynomial-q2-coeff
Jul 10, 2026
Merged

fix: incorrect q=2 coefficient in PiecewisePolynomialKernel#2756
gpleiss merged 2 commits into
cornellius-gp:mainfrom
devteamaegis:fix/piecewise-polynomial-q2-coeff

Conversation

@devteamaegis

Copy link
Copy Markdown
Contributor

What's broken

PiecewisePolynomialKernel(q=2) (q=2 is the default) returns incorrect covariance values. The q=2 covariance polynomial uses the coefficient (j + 4*j + 3)/3 = (5j+3)/3, but it should be (j**2 + 4*j + 3)/3.

Why it happens

In _get_cov, the q=2 branch dropped the square on the j term: (j + 4 * j + 3) instead of (j**2 + 4 * j + 3). This contradicts the kernel's own docstring, Rasmussen & Williams Eq. 4.21, and the q=3 branch in the same function (which correctly uses j**2). Existing tests only exercised q=0 with point pairs whose distances fall outside the compact support, so (1 - r)_+ = 0 zeroed the polynomial and the bug went undetected.

Fix

Restore the square: (j**2 + 4 * j + 3) / 3.0.

Test

Added test_computes_piecewise_polynomial_kernel_q2 with closely-spaced inputs (r < 1, inside the support), comparing kernel output against the R&W closed form. Fails on the old coefficient (norm diff ~0.105), passes after.

The q=2 branch of _get_cov used (j + 4*j + 3) instead of (j**2 + 4*j + 3),
yielding 5j+3 in place of j^2+4j+3 and producing materially wrong covariance
values for the default smoothness setting. Fix the coefficient to match
Rasmussen & Williams Eq. 4.21 (and the kernel's own docstring and test
reference). Add a q=2 numeric test with closely-spaced inputs so the
polynomial term is actually exercised (existing tests only used q=0 with
out-of-support distances, masking the bug).
@gpleiss
gpleiss enabled auto-merge (squash) July 10, 2026 17:35
@gpleiss
gpleiss merged commit e99e33d into cornellius-gp:main Jul 10, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants