Solution (source code)

= Solution

Let $A=A_\lambda$ be the <influence matrix>, so $\widehat y=Ay$. Its <trace> is the <effective degrees of freedom>, accounting for shrinkage of the fitted smooth rather than just counting basis coefficients. The <generalised cross-validation> score is
$$
\boxed{\operatorname{GCV}(\lambda)=\frac{\|y-A_\lambda y\|^2/n}{\{1-\operatorname{tr}(A_\lambda)/n\}^2}
=\frac{n\sum_i(y_i-\widehat y_i)^2}{\{n-\operatorname{tr}(A_\lambda)\}^2}.}
$$
For a fixed <smoothing parameter>, the exact leave-one-out residual of this penalized linear fit is $(y_i-\widehat y_i)/(1-A_{ii})$, obtained by removing its observation's contribution to the normal equations. Thus <Leave-one-out cross-validation> uses the average of their squared values. <GCV> replaces the individual leverages $A_{ii}$ by their average $\operatorname{tr}(A)/n$. This avoids separate refits and gives a criterion balancing the small training residuals of a flexible fit against its larger complexity.

Evaluate the score across positive $\lambda$, or optimize it numerically, and choose $\widehat\lambda$ minimizing <GCV>. Then refit or retain the <spline> at this value. In the full natural smoothing-spline family the <trace> decreases from $n$ towards $2$ as smoothing increases. The selected parameter is intended to give good prediction, rather than necessarily to pass through every point.