diff: add dy_d_best — reconciled dy.d_ (dy.dx step + NNS.stack on X*)#132
Merged
Conversation
…mator Adds nns.dy_d_best, a faithful Python port of the original NNS 0.5.7 dy.d_ used to produce the results in Vinod & Viole (2020), SSRN 3681436. It differs from the shipped dy_d (which sources f(x +/- h) from nns_stack(method=(1, 2))): it uses nns_reg(dim_red_method='equal', point_only=True) directly (no smoothing), a quantile-spacing step, a degree-0 quantile grid, plain-mean aggregation, distance = 2*h_step, and a plain nanmean blend across the v0.5.7 bandwidths h_s = 1/log(size(x), [2, 10]) extended by 10*h_s and doubled when nns_dep(x[:, wrt], y) < 0.5. The wrapper is byte-for-byte the same algorithm as the restored R NNS.dy.d_ (OVVO-Financial/NNS), so R and Python agree given the same NNS.reg engine; against the current engine both differ from the 0.5.7 paper values because the regression engine itself changed since 0.5.7. Adds invariant tests (export, shapes, single-wrt 1-D output, mixed, determinism, pinned golden values). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
… + NNS.stack)
Reworks dy_d_best from the raw-engine v0.5.7 port into the reconciled estimator:
* h_step now shares the dy.dx() logic - a locally-adaptive step centred on the
evaluation point's percentile, h_step = VaR(p+H,1,x) - VaR(p-H,1,x) with
p = lpm_ratio(1, eval, x) - and drops the cumulative window. This is what
makes the estimates uniform across identically-distributed regressors
(previously 0.29-0.62 scatter on the 5-way product benchmark).
* estimates now come from nns_stack on the equal-weight synthetic regressor X*
via the increased-dimension trick cbind(X*, X*), with method=(1, 2),
dim_red_method='equal', order='max', folds=5. The cross-validated n.best
regularises the sharper current engine.
On Example 2 of Vinod & Viole (2020, SSRN 3681436) this yields a tight, uniform
First = [0.94, 0.95, 0.93, 0.92, 0.93] at (1,1,1,1,1) (vs the paper's ~0.65 on
the retired 0.5.7 engine), and the mixed derivative for x1^2*x2^2 at (.5,.5)
recovers ~1.0. Bandwidths, First=(u-l)/(2h), Second=(u-2f+l)/h^2 blended with
nanmean across bandwidths. Golden test values updated.
Byte-for-byte the same algorithm as the reconciled R dy.d_ (OVVO-Financial/NNS).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
Adds
nns.dy_d_best, a reconciliation of the original NNS 0.5.7dy.d_(Vinod & Viole 2020, SSRN 3681436) with the current regression engine. It keeps the v0.5.7 finite-difference scaffolding but makes two changes so the estimates are uniform across identically-distributed regressors and share step logic withdy.dx.It is the byte-for-byte counterpart of the reconciled R
dy.d_inOVVO-Financial/NNS(PR #54).Two changes
h_stepshares thedy.dxlogic — a locally-adaptive step centred on the evaluation point's percentile,h_step = VaR(p+H, 1, x) − VaR(p−H, 1, x)withp = lpm_ratio(1, eval, x), and no cumulative window. This removes the cross-regressor scatter the raw v0.5.7 step produced.nns_stackon the equal-weight synthetic regressorX*viacbind(X*, X*), withmethod=(1, 2),dim_red_method="equal",order="max",folds=5. The cross-validatedn.bestregularises the sharper current engine.Bandwidths follow v0.5.7 (
h_s = 1/log(size(x), [2, 10]),+ 10·h_s, doubled whennns_dep(x[:, wrt], y) < 0.5).First = (upper − lower)/(2·h_step),Second = (upper − 2f(x) + lower)/h_step², blended withnanmean.Result (Example 2, at (1,1,1,1,1))
First = [0.94, 0.95, 0.93, 0.92, 0.93]— tight and uniform (the raw design scattered 0.29–0.62). ~1.4× above the paper's 0.65 (expected: the current engine is sharper;folds=5sets the level), and closer to the analytic 2.0. Mixed derivative forx₁²·x₂²at (.5,.5) recovers ≈ 1.0.Validation
h_stepvalidated to match the R port to numerical precision.wrt1-D output, mixed, determinism, pinned golden values). 17 diff tests pass;ruffclean.🤖 Generated with Claude Code