Repository navigation
Make smooth=True, dy_dx and nns_diff reproduce R 13.3 - #151
Closed
gitRasheed wants to merge 7 commits into
Closed
gitRasheed wants to merge 7 commits into
gitRasheed wants to merge 7 commits into
Conversation
The smooth=True spline port differed from stats::smooth.spline in two ways: - The penalty matrix was the exact Gauss-Legendre integral of B''B''. R's Fortran sgram uses a linear approximation of B'' on each knot interval with the constant .3330 instead of 1/3. - A knot was placed at every unique x. R uses .nknots.smspl(nx) knots at xbar[seq.int(1, nx, length.out = nknots)] once nx > 49. The port now builds Sigma with the sgram formula (one vectorised evaluation of B'' at the knots), thins knots like .nknots.smspl, groups x within R's tie tolerance 1e-6 * IQR(x), and solves the 4-band system with a banded Cholesky like R's sslvrg. Evidence (audit A/B harness, 8 smooth cases vs NNS 13.3): - Before: every case differed from R; Point.est off by up to 8.8% on bigsin_smooth (n = 5000) and 43% relative on fin_univ_smooth. - After: 7 of 8 agree to 1e-10 relative. sin_smooth agrees to 3e-8 relative; the remaining gap comes from a last-ulp difference in the dependence that sets spar, amplified by a small lambda. - smooth.spline alone (x of length 8, spar 0.3/0.6/0.75; n = 80): agrees with R to 12 digits. The old per-basis Gauss-Legendre loop was quadratic in the number of knots: nns_reg(smooth=True, order="max") on n = 1000 took 119 s and warned about an ill-conditioned solve. It now takes 0.4 s. Public results change for every smooth=True call, and therefore for dy_dx and the smoothed nns_arma path. The parity tests' loose smooth tolerances (5e-4, 5e-5, 5e-3) are tightened to the default 1e-10, and a new parity test covers more than 49 regression points (knot thinning).
Two bugs in the numeric dy_dx path (eval_point given):
- The step was gravity(|diff(x)|) * (position of h in h_s), so it used
steps of 1..5 x spacing. R uses h_s[index], the bandwidth value
itself (2..sqrt(n) x spacing).
- When an eval point sat at min(x) or max(x), the code rewrote the
upper column to eval - fallback after nns_reg had already been
evaluated. That put the upper point below the eval point, and it did
the same for zero lower runs, so rise and run stopped matching. R's
fallback is dead code: it uses the one-sided slope for the first
derivative and the second derivative is NaN. The port now does the
same.
R detail reproduced here: for n < 49, h_s repeats a bandwidth. R's
`deriv.points[[index]] <- ...` with a length-2 index is a recursive
assignment, and rbind() drops those blocks, so R only uses the
bandwidths that occur exactly once in h_s. Below n = 16 every bandwidth
repeats and R's dy.dx fails ("no such index at level 2"); the port uses
the distinct bandwidths there instead of failing.
Evidence (vs NNS 13.3, with the smooth.spline fix):
- y = x^3 on 500 uniform x, at (-1.5, 0, 0.5, 1.5): before (3.859,
3.223, 3.282, 3.900), R (3.708, 3.774, 3.679, 3.719); after it agrees
to 2e-8.
- y = x^3 - x, n = 16, 24, 30, 36, 49, 60: after it agrees to 2e-11.
Keeping duplicated bandwidths instead would differ by up to 3e-2.
- sin on R's own example grid at 0 and 2*pi: before -0.563 / 35.55
(truth 1), R 0.965 / 0.966. After it agrees with R to 5e-13 once the
regression dependence matches. On this grid the public result still
differs because of the separate copula mean knife-edge.
Public dy_dx(eval_point=...) results change. The parity tolerance goes
from 5e-3 to 1e-10, with new parity tests for random-x bandwidths
(n = 30, 60, 250) and boundary eval points.
dy_dx(x, y) with the default eval_point=None always failed with
"ValueError: some columns are not in the data.table: [eval.point]", a
copy of an internal R data.table message. R's dy.dx(x, y) also crashes
there ("undefined columns selected"), so no call with the default ever
worked on either side.
The Python default now raises
ValueError("eval_point must be provided: a numeric point/vector or
'overall'") before any input checks or fitting. Only the error message
changes; numerics are unchanged.
nns_diff solved for the inferred h with scipy brentq at xtol=1e-14, and widened the bracket by doubling both ends. R calls uniroot(..., extendInt = "yes") with the default tol eps^0.25 (1.2e-4), which widens additively (0.01 * max(1e-4, |u|), doubling) and refines with R's zeroin2. Because |inferred h| < tol is the convergence test, the much tighter root made Python bisect until max_iter on smooth functions and report converged=0, termination.code=1 where R converges in about 30 iterations. The equal-intercept shortcut also used np.isclose relative to upper_b with no absolute fallback. R uses all.equal(lower.B, upper.B): relative to |lower.B|, or absolute when |lower.B| <= tolerance. For abs(x) at -1 (B1 = B2 = 0) R returns at iteration 0 while Python iterated once and reported converged=0. The port now: - extends the interval like uniroot(extendInt = "yes"); - solves with a line-by-line port of R_zeroin2 (tol eps^0.25, maxiter 1000); - checks equal intercepts like all.equal. Evidence (10 functions vs NNS 13.3, all 14 output fields at R's 12 digits): - Before: 8 of 10 differ. sin@1, x^2@3, x^3@2, log@5, tanh@0.5 and x^4-3x@-2 hit max_iter with converged=0; DERIVATIVE is off by 1e-7 to 2e-5 relative. - After: all 10 match exactly, including iterations and flags. tanh matches when f is the C library tanh R uses; np.tanh differs by an ulp and takes a different bisection path. Public nns_diff results change: DERIVATIVE, the inferred h, iterations, converged and termination.code now follow R. The derivatives are slightly less accurate than before (R's looser root), which is the intended parity. The parity test now compares every field at 1e-12 on 11 cases (previously DERIVATIVE only, at 1e-5). Also drops a cast in _eval_real that mypy flags as redundant.
Update docs/conventions.md for the preceding fixes: - smooth=True: tie grouping, knot thinning, sgram penalty and banded solve. - dy_dx: bandwidth-value steps, the boundary one-sided slope, repeated-bandwidth handling and the required eval_point. - nns_diff: R's uniroot tolerance. Documentation only.
…keys are portable x**3 is not correctly rounded on every numpy build, so the y values, and with them the hashed R-cache keys, differed in the last bits between machines and the three bandwidth entries missed. x is now drawn from integers / 1024 in [-2, 2], where x * x * x - x is exact. The three new entries come from NNS 13.3; the three old entries are removed.
The ZeroDivisionError ended the solve: f = quintic, point -0.25, h = 0.2 gave Inferred h NaN, 1 iteration, converged 0; now 8.5e-11, 32, 1 as R.
gitRasheed
force-pushed
the
rasheed/timeseries
branch
from
November 2, 2026 23:09
18e5ce9 to
7f0654d
Compare
gitRasheed
force-pushed
the
rasheed/regression-diff
branch
from
November 2, 2026 23:09
879ce04 to
256378c
Compare
Contributor
|
Incorporated through NNS R 14.0 integration #192, merged as |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
nns_reg(smooth=True)was an approximation ofstats::smooth.spline, up to 8.8% off R.dy_dxstepped by the bandwidth's position instead of its value and rewrote boundary estimates after the fit.nns_diffsolved for h far more tightly than R'suniroot, so it hitmax_iterand reported non-convergence where R converges. We port R's algorithms for all three.Stacked on #150; merge with a merge commit.
Changes
smooth=Truebuilds the penalty with R'ssgramformula (constant .3330), thins knots like.nknots.smsplonce nx > 49, groups x within R's tie tolerance and solves with a banded Cholesky likesslvrg. Before, every one of 8 smooth cases differed from R, with Point.est off by up to 8.8%. Now 7 of 8 agree to 1e-10 relative and the last to 3e-8, a gap that comes from a copula value sitting on a knife edge.nns_reg(smooth=True, order="max")at n = 1000 goes from 119 s to 0.4 s. Everysmooth=Trueresult changes, and with itdy_dxand the smoothednns_armapath.dy_dx(eval_point=...)steps by the bandwidth value, and at min(x) or max(x) it uses the one-sided slope with a NaN second derivative, as R does. For y = x^3 on 500 uniform points at (-1.5, 0, 0.5, 1.5), Python gave (3.859, 3.223, 3.282, 3.900) against R's (3.708, 3.774, 3.679, 3.719) and now agrees to 2e-8. Below n = 16, where R'sdy.dxfails, the port uses the distinct bandwidths.dy_dxwithouteval_pointraises "eval_point must be provided" instead of a copied data.table message. R's default call crashes too.nns_diffwidens the bracket likeuniroot(extendInt = "yes"), solves with a port ofR_zeroin2at R's tolerance, and checks equal intercepts likeall.equal. On 10 test functions, 8 differed from R before; all 10 now match every output field, including iterations and flags. The derivatives are slightly less accurate than before, because R's root is looser.Tests
Parity tolerances for smooth fits go from 5e-4/5e-5/5e-3 to 1e-10, with a new case above 49 regression points.
dy_dxparity goes from 5e-3 to 1e-10, with random-x bandwidth cases (n = 30, 60, 250) and boundary eval points drawn on a 1/1024 grid so the cache keys are portable.nns_diffparity compares every field at 1e-12 on 11 cases. The parity cache encodes unpatched R 13.3.Review
GPT-6.1 Sol reviewed this slice and raised one finding, which we took: the inferred-step callback in
nns_diffraised ZeroDivisionError at a zero step, where R's uniroot replaces the non-finite value and converges. The callback now returns NaN at h = 0, as R's 0/0 does: inferred h, iterations and converged go from NaN, 1, 0 to R's 8.5e-11, 32, 1. The fix commit has not been re-reviewed.