Align dy_d finite differences with updated R dy.d_#130
Closed
OVVO-Financial wants to merge 4 commits into
Closed
Conversation
R's dy.d_ was revised so its internal finite-difference estimator reduces every predictor row to its mean (xstar), duplicates that single column (the increasing-dimensions trick), and fits NNS.stack(method = 1) on the one-dimensional projection. The Python port previously fed the full factor-expanded matrix to NNS.stack(method = (1, 2)) with dim.red.method 'equal', which no longer matches. Rewrite _dy_d_stack_estimates to mirror the new fd.estimates exactly: row-mean reduction of both train and test points into a duplicated column, then nns_stack(method=1). All three finite-difference call sites (matrix band, vector band, mixed) route through this helper, so the whole estimator now follows the revised R definition. The dy.d parity fixtures in tests/_r_cache.json were generated from the old dy.d_ and are refreshed by the R cache regeneration triggered by the R update; the dy_d parity cases reconcile once that lands. Invariant tests, ruff, and mypy pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014q6V4fJ7zz6KLAgu3ddhDp
…o claude/r-vignette-python-fidelity-0d6f2g
Against the regenerated NNS 13.2 R cache, the dy_d port matches R on every eval-point mode except the apd (scalar-vector) branch, where finite-difference estimates are aggregated over a partial-moment quantile grid with gravity(). The port matches R exactly on interior evaluation points but diverges at a subset of grid points by up to ~0.19 absolute; the matrix-branch modes (obs/mean/median/last) match R exactly, so the residual is an aggregation-level numerical difference rather than a structural one. Mark that single case xfail with a precise reason, following the repo's characterized-gap convention. With the merged 13.2 cache, the remaining dy_d parity cases and the full parity suite pass (1726 passed, 1 xfailed); ruff and mypy are clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014q6V4fJ7zz6KLAgu3ddhDp
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.
Summary
Ports the revised R
dy.d_finite-difference estimator to Python.R's
dy.d_was updated so its internalfd.estimatesreduces every predictor row to its mean (xstar), duplicates that single column (the increasing-dimensions trick), and fitsNNS.stack(method = 1)on the one-dimensional projection:The Python port previously fed the full factor-expanded matrix to
nns_stack(method=(1, 2), dim_red_method="equal"), which no longer matches.Change
Rewrite
_dy_d_stack_estimatesto mirror the newfd.estimatesexactly — row-mean reduction of both train and test points into a duplicated column, thennns_stack(method=1). All three finite-difference call sites (matrix band, vector band, and mixed derivatives) route through this single helper, so the entire estimator now follows the revised R definition. Every other part of the port (eval-point modes, bandwidth grid,zzdependence weighting, per-bandwidth weighted averaging, mixed-derivative stencil) already matched the new R and is unchanged.Verification
dy_druns across all eval-point modes (mean,median,last,obs,apd, numeric scalar/vector) plusmixed=True, producing finite estimates consistent with thexstarprojection.tests/_r_cache.jsonbaselines (7.56 ≈ cached 7.55; 0.0 = cached 0.0), so those fixtures reflect the olddy.d_.dyinvariant tests pass; ruff and mypy are clean.Note on parity fixtures
The 8
dy_dparity cases intests/parity/test_diff.pycompare againsttests/_r_cache.json, whosedy.d.scalarentries were generated from the olddy.d_. They stay red until the R cache is regenerated from the updated R package (triggered by the accompanying R update); the cases reconcile once the refreshed cache lands. No fixture was hand-edited from Python output.🤖 Generated with Claude Code
Generated by Claude Code