Skip to content

Bring NNS Python to R NNS 14.2 (f0387ed) bit for bit - #204

Merged
OVVO-Financial merged 29 commits into
mainfrom
claude/nns-r14-parity-fidelity-rgag0e
Nov 22, 2026
Merged

OVVO-Financial merged 29 commits into
mainfrom
claude/nns-r14-parity-fidelity-rgag0e

Conversation

@OVVO-Financial

Copy link
Copy Markdown
Owner

Summary

  • Oracle: R NNS 14.2, NNS_14.2.tar.gz as committed at OVVO-Financial/NNS@f0387ed (sha256 a4e04b26…a649201). Its R/ and src/ files equal the Git tree at that commit apart from CRLF line endings and final newlines. Run under R 4.5.0 with the reference BLAS/LAPACK.
  • Live parity: all 19 sweeps match R exactly with and without the native extension. That is 22,454 comparisons at rtol = atol = 0, including 546 input-validation cases.
  • Cache: regenerated fresh from live R. It now holds 3,126 entries in 151 shards, all stamped 14.2.
  • Gate: 3,341 tests pass with the native extension and 3,316 with the NumPy fallback (cache-only); ruff check . and strict mypy are clean.

Includes the open 14.2 PRs

Merging this PR supersedes #200, #201, #202 and #203.

Port changes (R 14.2 → Python)

Numeric (all verified bit for bit by the sweeps):

  • NNS.moments re-centring.
  • PM.matrix "mean" target, with its origin shift.
  • Distance ties keep their stable order.
  • NNS.mode uses the new peak rule.
  • NNS.rescale NaN and type rules.
  • NNS.dep: asymmetric matrix diagonal and permutation matches.
  • Regression points are grouped by exact keys, and the first zero's sign is kept.
  • Default n.best.
  • M.reg distances are summed in long double, in the native overlay and in NumPy.
  • Classification: the default k, and NNS.reg.variables.
  • Classification stack tie-breaking:
    • each fold's class priorities;
    • pooled out-of-fold rows averaged over their sorted contributions;
    • the blended threshold;
    • final-row class order;
    • prediction sets using R's long-double cumsum.
  • NNS.meboot bounds and scl.adjustment variance; ARMA bands.
  • dy.dx: the endpoint stencil and eval.point rules.
  • NNS.diff: the refine step.
  • NNS.FSD without a plot uses NNS.FSD.uni both ways.
  • NNS.VAR takes a per-variable tau.
  • NNS.caus: the shift null now stops when no circular shift is admissible.

Input layer: the new src/nns/_inputs.py ports R/inputs.R onto Python types. A pandas Categorical is a factor, a DataFrame a data.frame, and string or boolean arrays are character or logical vectors. The public functions now accept the inputs R accepts and raise R's messages in R's order. The differences NNS Python keeps are documented in docs/conventions.md (Input Validation):

  • matrices with several columns are rejected;
  • empty input raises;
  • one-column DataFrames are accepted;
  • nns_copula takes (x, y) rather than one matrix.

Live sweeps in the repo and in the builder

  • scripts/live_parity/ holds the harness, the R batch evaluator, the 19 sweeps and run.py, which runs every sweep in both modes, writes a Markdown table and exits non-zero on any difference. See its README.
  • inspect-r-api-update.yml now runs every sweep in both modes after it regenerates the cache. It uploads the log and reports, puts the table in its pull request, and fails the run on any difference.

Manifest, docs, workflows

  • sync/nns_source.json:
    • r_commit f0387ed and the archive's sha256;
    • six overlays, all verified against f0387ed;
    • central tendencies and M.reg prediction re-ported from f0387ed;
    • the dependence and D.UPM sources keep their port commits, because R 14.2 changed those files only in label ordering and the zero-total rule.
  • r14-reproduction.yml now builds NNS_14.2.tar.gz.
  • Docs updated: docs/parity.md (oracle, install steps, sweep table, provenance), docs/conventions.md, README and api_status.

Needs a maintainer

  • This PR changes protected workflows (inspect-r-api-update.yml, r14-reproduction.yml). They need the workflow-change-approved label; until it is added, the baseline bot reverts them.
  • The version stays at 2.3.0. This PR does not bump it for 14.2.

Upstream R issues found (not changed here)

  • In R 14.2, NNS.MC(x) with an infinite value in x crashes the R session.
  • LPM.VaR, UPM.VaR and NNS.ANOVA stop with "missing value where TRUE/FALSE needed" on infinite values.

Test plan

  • python scripts/live_parity/run.py --mode both: 22,454/22,454 exact
  • NNS_R_CACHE_ONLY=1 pytest tests: 3,341 passed (native)
  • NNS_DISABLE_NATIVE=1 NNS_R_CACHE_ONLY=1 pytest tests: 3,316 passed (fallback)
  • ruff check ., mypy (strict)
  • CI on this PR

Generated by Claude Code

gitRasheed and others added 29 commits November 19, 2026 16:33
The vendored NNS-core d_upm swaps its two mixed-degree branches: with one
integer and one fractional degree it applies repeated multiplication to
the wrong side and drops the fractional power, so D.UPM(1, 1.5, ...)
returned the D.UPM(1, 1, ...) value. Every public d_upm call goes through
d_upm_v, which uses that kernel.

Add src/nns/_r14_partial_moments.cpp, the vendored partial_moments.cpp
with those two branches matched to R's DUPM_C, swap it in for the vendor
translation unit in CMake, and register it with its hashes in
sync/nns_source.json. extern/NNS-core is unchanged.

The co-moment parity grid gains (1, 1.5), (1.5, 1), (2, 0.5), (0.5, 1)
and (0, 0.5), with five new pm14.co_moments cache entries generated by
the pinned NNS 14.0 archive under R 4.5.0, and checks the scalar native
d_lpm/d_upm kernels too.
…ns-r14-parity-fidelity-rgag0e

#200 and #203 both added src/nns/_r14_partial_moments.cpp: #200's is the
vendor partial-moment overlay with the D.UPM fix, #203's the R 14.2
normalisation helpers for the nD moments and PM.matrix. #200's keeps the
name; #203's becomes src/nns/_r14_pm_norm.{cpp,hpp}.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
- NNS.moments centres the deviations once more on their own mean (R 14.2:
  the mean can round onto an observation when the spread is small), and
  the sample skewness is NaN below three observations.
- PM.matrix's column-mean target first moves each column by the end of
  its range nearest zero, max(0, min) + min(0, max), takes R's mean() of
  the moved column, and computes the moments on the moved data, as
  PMMatrix_RCPP now does.
- NNS.distance and NNS.distance.bulk order equal distances by RPM row,
  the total order R 14.2's nearest_rows uses in every distance kernel.
  The libstdc++ sort replay (nns._stdsort) is no longer needed.

Live sweeps against R 14.2 at exact equality: partial moments 2,932/2,932
and distance 928/928, with and without the native extension.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
…lues

- NNS.mode (native overlay and NumPy fallback): a peak is above both
  neighbours, the tallest bin within three bins and, within three bins of
  either end, the tallest bin overall; an equally tall bin in the window
  disqualifies it unless that bin is a peak too. The concavity test and
  the non-maximum suppression are gone, and multiple modes are unique.
  NNS.part with noise.reduction = "mode" follows.
- NNS.rescale keeps a missing value missing when every x is identical,
  and the risk-neutral type accepts any leading part of "Terminal" or
  "Discounted", rejecting anything else.
- NNS.dep's directional matrix (asym = TRUE) takes each column's own
  measure on the diagonal, and a p-value shuffle that reproduces the
  observed joint table scores the observed values, reversed for a
  reflected balanced binary table (.nns_dep_perm_matches).

Exact sweeps against R 14.2: NNS.part 920/920 in both modes, and 150
p-value cases built to repeat the observed table (binary, balanced,
constant, tied, same values) match with and without the native extension.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
…ances

- Univariate regression points group equal x values exactly (R 14.2:
  split(y, match(x, sort(unique(x))))), where R 14.0 split on the
  15-digit character form. At order = "max" the training boundaries now
  merge into the end points instead of adding two near-duplicates.
- The default multivariate n.best is floor((1 - D) * sqrt(R)) for R
  regression points, and the classifier's floor((1 - mean strength) *
  sqrt(cells)), instead of sqrt(n).
- Multivariate prediction distances (native overlay and NumPy fallback)
  round each column's term to double and accumulate the terms in long
  double, as R 14.2's kernel does to match rowSums().

Exact sweeps against R 14.2: regression 480/480 and large multivariate
predictions 84/84, with and without the native extension.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
R 14.2's classification NNS.stack returns NNS.reg.variables: the encoded
predictor names of the final multivariate classification fit (after its
importance selection), or NULL when only method 2 runs.

Exact sweep against R 14.2: stack/boost 56/56 with and without the
native extension.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
….diff

- NNS.meboot: xmin and xmax are always the residual tails' endpoints
  (xx[1] - dvtrim, xx[n] + dvtrim); supplied bounds clamp the ensemble.
  scl.adjustment matches the variance of one residual draw, built from
  type-7 quantile breaks with tail mass 1/n.
- NNS.ARMA: a single seasonal lag's band reads the lag's estimate at step
  h, as several lags do, so it adds no spread of its own.
- dy.dx: eval.point must be "overall" or finite numbers (R's messages),
  a point at either end of the support takes the one-sided stencil
  lo, lo + (hi - lo) / 2, hi, and results follow the input points.
- NNS.diff: when the projected intercepts coincide, the step is halved
  and the centred slope extrapolated (R 14.2's refine.step), stopping on
  rounding in the samples, on convergence or at max.iter.

Exact sweeps against R 14.2 with and without the native extension:
bootstrap 502/502, forecasting and derivatives 345/345.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
R 14.2's NNS.FSD evaluates the LPM.ratio curves only to draw them; the
verdict is NNS.FSD.uni in both directions. Without a plot the discrete
test therefore accepts infinite values and the continuous one rejects
them with R's "You have some infinite values" message.

Exact sweep against R 14.2: stochastic dominance edge cases 1,389/1,389.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
R 14.2 decides a tied out-of-fold class by the first appearance of each
class among the fold's training rows (the earliest original row for a row
validated in several folds), and a tied final prediction by first
appearance among the balanced final rows. Rows validated more than once
average each class's sorted contributions, the blended threshold is scored
on the full probability matrices, and prediction sets use R's long-double
cumsum in the final rows' class order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
R 14.2 takes tau[[min(i, length(tau))]] for variable i, so a flat vector
gives one element per variable and the last one repeats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
- M.reg distances: the reference sums each column's double term in long
  double, as R 14.2's kernel does, and a scalar transcription of the C++
  distance() checks sampled entries.
- order = "max" groups by exactly equal keys, keeping the first zero's sign.
- NNS.SSD/TSD always reject infinite values; NNS.FSD only on its plot path.
- dy.dx: a finite second derivative at support endpoints, the rejection of
  character points other than "overall", and R 14.2's missing-point message.
- dy.d_ pinned values re-verified against f0387ed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
Every entry recomputed from live R 4.5.0 with the NNS_14.2.tar.gz stored at
f0387ed (sha256 a4e04b26...). The 2985 entries shared with the builder's own
regeneration on automation/r-cache-nns-14.2 are byte-identical. The meboot
test helpers call NNS:::.nns_meboot_draw now that NNS.meboot is no longer a
Vectorize() wrapper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
…-14.2

The builder's regeneration (bb05d3d) and this branch's agree on every shared
cache entry. This branch's cache adds the entries for tests the builder's run
did not have (the merged #203 tests, the meboot helpers and the new 14.2
contracts), so the resolution keeps this branch's tree unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
- r_commit f0387ed and NNS_14.2.tar.gz as stored there (sha256 a4e04b26...),
  the archive the cache builder downloads; its R/ and src/ files equal the
  Git tree apart from CRLF line endings and final newlines.
- The central-tendency and M.reg prediction overlays now port f0387ed's
  sources. The dependence and D.UPM overlays keep their source commits: R
  14.2 changes those files only in label ordering and the zero-total rule.
- #203's normalisation helper is tracked under its new name,
  src/nns/_r14_pm_norm.cpp, beside #200's D.UPM overlay, so there are six
  overlays, all verified against f0387ed.
- Cache summary: 150 shards, 3124 entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
R NNS 14.2 checks every data argument through one input layer, which fixes
the inputs each function accepts and the messages it stops with.
src/nns/_inputs.py ports its helpers onto Python types (a pandas Categorical
is a factor, a DataFrame a data.frame, string and boolean arrays character
and logical vectors), and the public functions now accept the same inputs and
raise R's messages in R's order:

- co-moments, LPM/UPM ratios, NNS.CDF: R's type rules and messages, a
  one-column DataFrame read as its column;
- VaR, gravity, mode, rescale, NNS.SS, NNS.meboot, NNS.MC: factors rejected,
  R's empty, missing-value, shape and two-observation rules;
- NNS.dep, NNS.part, NNS.caus, NNS.copula: R's pair checks (a factor
  predictor by its level codes or numeric labels, as R uses it);
- NNS.seas, NNS.ARMA, NNS.reg, dy.dx, NNS.ANOVA: R's series, response and
  predictor messages, in R's order.

NNS.caus's shift null admits no offsets when the series is too short for
max(tau + 1, n / 10) both ways, and stops as R 14.2 does instead of falling
back to every offset.

Python stays stricter where it documents it: a matrix with several columns
and empty input are rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
The differential sweeps that verified the port, now in the repository:
harness.py runs one Rscript batch per sweep and compares decoded R values
exactly (RTOL = ATOL = 0 unless set); run.py runs every sweep with and
without the native extension, writes a Markdown summary and exits non-zero
on any difference. The sweeps cover every exported area (22,454
comparisons over both modes), including classification stacks built to tie
and input validation against R 14.2's input layer, whose documented
Python/R differences are listed rather than counted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
inspect-r-api-update.yml runs every live sweep in both modes after it
regenerates the cache for a new R release, uploads the log and reports, puts
the summary table in its pull request and fails the run on any difference.
r14-reproduction.yml bundles and builds NNS_14.2.tar.gz at f0387ed (sha256
a4e04b26...).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
docs/parity.md names the 14.2 oracle and its install steps, the sweep table
and the cache provenance; docs/conventions.md describes R 14.2's input
layer in Python terms and the differences NNS Python documents; README and
api_status refer to 14.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
@OVVO-Financial
OVVO-Financial merged commit 56410bd into main Nov 22, 2026
6 checks passed
OVVO-Financial pushed a commit that referenced this pull request Nov 22, 2026
#204 merged after the workflow baseline bot had restored the protected
workflows, so main lacks the two changes it carried:

- inspect-r-api-update.yml runs every live R sweep (scripts/live_parity)
  in both modes after regenerating the cache, uploads the log and reports,
  puts the summary table in its pull request and fails on any difference;
- r14-reproduction.yml bundles and builds NNS_14.2.tar.gz at f0387ed
  (sha256 a4e04b26...).

This reverts commit ff41639.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Wws6NpuQ72C5sfEYVoVmGt
OVVO-Financial added a commit that referenced this pull request Nov 22, 2026
…elity-rgag0e

Re-apply the 14.2 builder and reproduction workflows (#204 merged without them)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants