Skip to content

Mirror the R fixes to robust ANOVA and stochastic dominance - #160

Closed
gitRasheed wants to merge 7 commits into
rasheed/r-fixes-timeseriesfrom
rasheed/r-fixes-dominance-anova
Closed

gitRasheed wants to merge 7 commits into
rasheed/r-fixes-timeseriesfrom
rasheed/r-fixes-dominance-anova

Conversation

@gitRasheed

@gitRasheed gitRasheed commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Robust nns_anova resampled only the first min(n1, n2) observations of the longer group, so the result depended on row order, and one-sided tails with an interval had no answer. Dominance was checked only at the sample points and compared curves exactly, so TSD and continuous FSD reported false dominance and textbook SSD cases flipped on 1e-16 rounding. We mirror the patched R for each.

Stacked on #159; merge with a merge commit.

Changes

  • Robust mode draws independent bootstrap indices for each group from its own full sample, still on R's Mersenne-Twister stream, so random_seed=s matches set.seed(s) in the patched R. For a control of 50 N(0,1) rows followed by 950 N(5,1) rows against 50 N(0,1) treatment rows, the estimate goes from 0.677 [0.265, 0.903] to 0 [0, 0].
  • tails="left"/"right" with a confidence interval return R's one-sided bounds instead of the ValueError from Stochastic dominance and ANOVA: drop the dense matrices, follow R's sampling #152. On the parity groups, left gives (-0.443, Inf) and right (-Inf, 0.959).
  • The continuous FSD 0/0 ratio at a point mass is the degree-0 ratio, as LPM.ratio does, instead of 0.
  • TSD and continuous FSD also check between knots: the vertex of the LPM2 quadratic and the stationary point of the difference of the two ratios. On 8 TSD counterexamples, tsd and tsd_uni reported dominance in 8 and now in 0.
  • Curve differences up to 64 * eps * S^d are ties. (0.1, 0.7) against (0, 0.8) now gives SSD, as the same data times 10 already did. Against the patched R on 2611 pairs x 12 tests, disagreeing verdicts go from 58 to 0, and 35 of 35 efficient sets and clusters equal R (32 before).
  • The pairwise test merges the two sorted samples instead of two binary searches, with bit-identical verdicts. ssd at n = 1e4 goes from 1.83 ms to 0.94 ms.

Tests

New parity cases against R 13.3 with the audit's R patches for the robust and one-sided ANOVA paths, the point-mass ratio, between-knot crossings, rounding-level ties and the patched .uni routines. The SD and NNS.ANOVA.custom shards are regenerated from that build; the three TSD extra-knot efficient-set entries change from "X_1" to ["X_1", "X_2"]. NNS.SSD.uni and NNS.TSD.uni shards are new. test_public_nns_var_objective_max_matches_r stays xfailed until #161.

Mirrors OVVO-Financial/NNS#81 and OVVO-Financial/NNS#82

Review

GPT-6.1 Sol reviewed this slice and raised two findings. We rejected both.

  • Prefix moments without compensation lose dominance between constant samples and report it between identical ones. The R branch this slice follows has the same bug, and Stochastic dominance: midpoint check and compensated prefix sums #167 mirrors the R fix. At the top of the stack the reviewer's cases give 1 1 1 and 0 0, as R does.
  • The invariant test's reference oracle still uses the knot-only rule. Those tests pass, and the R parity tests carry the new rule.

…of the R fix)

The robust path drew one index matrix with sample.int(min(n1, n2)) and
used it for both groups, so only the first min(n1, n2) observations of
the longer group were ever resampled and the result depended on row
order; Python reproduced R's draws and pairing exactly. R now
draws independent bootstrap indices for each group from its own full
sample and size (control first, then treatment); do the same, still on
the reproduced R Mersenne-Twister stream, so random_seed=s matches
set.seed(s) in the patched R.

Before -> after (Robust Certainty Estimate [95% CI], = patched R):
  control = 50 x N(0,1) then 950 x N(5,1), treatment = 50 x N(0,1), seed 1:
      0.677 [0.265, 0.903] -> 0 [0, 0]
  control = 50 sin values then 150 shifted by 5, treatment 50 sin values:
      0.785 [0.452, 0.926] -> 0 (base Certainty 0)
The base (non-robust) Certainty is unchanged. The NNS.ANOVA.custom shard
is regenerated from the patched R (the one-sided-tail entries keep their
13.3 values until the one-sided-tail R fix is mirrored next).
With confidence_interval and tails="left"/"right" R's NNS.ANOVA.bin
needed a quantile it had not computed and stopped with "object
'control_upper' / 'control_lower' not found"; Python raised a ValueError
pointing at that failure. R now computes both bootstrap
quantiles of both groups for every tail choice; replace the ValueError
with R's result:
  tails="left"   (treatment_lower - control_upper, Inf)
  tails="right"  (-Inf, treatment_upper - control_lower)
also on the robust path (with its one-sided "Lower/Upper <level>% bound").

Before -> after (the 30-point parity groups, ci = 0.95, random_seed=1;
= patched R with set.seed(1)):
  tails="left"   ValueError -> Effect_Size_LB -0.443, Effect_Size_UB Inf
  tails="right"  ValueError -> Effect_Size_LB -Inf, Effect_Size_UB 0.959
  tails="both"   unchanged (-0.545, 1.095)
The NNS.ANOVA.custom shard is regenerated from the patched R.
… fixes)

The continuous FSD curve L1/(L1+U1) is 0/0 when every observation of a
sample equals the threshold (a constant sample, or a threshold at a
point mass). R's .uni code and Python mapped it to 0, which makes a
point mass look as if it had no mass at its own location and produced
spurious dominance. R now uses the degree-0 ratio there, as LPM.ratio
does; do the same in the prefix-sum curve that
fsd/fsd_uni(type="continuous") and the efficient set use.

Before -> after (= patched R):
  a = [5, 5, 0, 4, 0, 2], b = [4] * 6:  fsd_uni(b, a, "continuous") 1 -> 0
  x = [0, 0], y = [0, 3]:  fsd_uni(y, x, "continuous") 0 -> 1;
                           fsd(x, y, "continuous") 0 -> -1 ("Y FSD X")

R's other .uni fixes (walk each sample with its own length;
no dominance when the curves coincide) needed
no Python change: Python already agreed with R's bidirectional tests.
New parity cases pin all three against the patched R .uni routines.
The NNS.FSD, NNS.FSD.uni, NNS.SSD, NNS.TSD, NNS.SD.efficient.set,
NNS.SD.cluster and NNS.SD.cluster.dendrogram shards are regenerated from
the patched R (no existing entry changes); NNS.SSD.uni and NNS.TSD.uni
shards are added.
…ror of the R fix)

Dominance was checked only at the merged sample points. That is exact
for discrete FSD (step functions) and SSD (LPM1 is piecewise linear),
but LPM2 is piecewise quadratic and the continuous FSD ratio
L1/(L1+U1) is linear-fractional between points, so the difference of
the two curves can peak strictly between knots. tsd/tsd_uni, continuous
fsd/fsd_uni, the efficient set and SD clustering reported false
dominance, e.g. x = (-0.85, -1.52, 0.68), y = (-0.21, -0.33, -1.67):
tsd(x, y) = 1 although LPM2_x - LPM2_y = +0.0389 at t = 0.16.

All pairwise verdicts now come from one port of the patched R's native
pairwise test (sd_dom_pair): curves from prefix sums on the merged
unique thresholds, each sample with its own count, plus the closed-form
interior extremum on each interval between knots: the vertex b/a of
the quadratic when a < 0 (TSD) and the stationary point of the
difference of the two linear-fractional ratios (continuous FSD).
Candidates whose excess is at rounding level (64 eps relative to the
data scale) are ignored, as in R. The bidirectional tests take their
verdict from that test in both directions, as before.

Before -> after (A/B against the patched R, 2611 pairs: the audit's
counterexamples and SD cases plus 1800 small random rounded pairs):
  8 TSD counterexamples: tsd and tsd_uni say X TSD Y in 8 -> 0
  continuous FSD x = (0.15, 1.55, -0.86, -0.8, 0.25, 0.33),
    y = (-0.15, 1.17, 0.26, -0.04, -2.44, 1.19): 1 -> 0
  agreement with R: continuous FSD 2567 -> 2611 (all; the 44 changes
    are curve crossings between knots, confirmed on an exact rational
    grid, most on integer-valued data), discrete FSD 2611 -> 2611,
    TSD 2590 -> 2599
  SSD 2600 -> 2594: SSD/TSD curves are now computed from prefix sums
    like R's native test (and Python's efficient set already did), which
    moves rounding-level ties; the next commit adds R's tie tolerance,
    after which every pair agrees.
The three NNS.SD.efficient.set cache entries of the TSD extra-knots
test change from "X_1" to ["X_1", "X_2"] after regeneration from the
patched R; every other SD entry the tests read is unchanged.
Efficient set 252 x 50: degree 2 2.1 -> 2.2 ms, degree 3 2.6 -> 3.6 ms,
continuous FSD 3.4 -> 5.7 ms; nns_sd_cluster degree 2 7.1 -> 11.7 ms.
…e R fix)

Dominance verdicts compared curve values exactly, so rounding noise of
order 1e-16 decided them. The textbook mean-preserving spread
x = qnorm(ppoints(100)), y = 2x and the same pair shifted by 0.37 flip
with the summation order, and x = (0.1, 0.7) vs y = (0, 0.8) gave no
SSD because 0.1 + 0.7 = 0.7999999999999999, while the same data times
10 gave X SSD Y.

As in the patched R's native pairwise test, differences up to
64 * eps * S^d are ties (S = largest |value| of the two samples; d = 1
for SSD curves and the mean gate, 2 for TSD, a ratio analogue
max(1, S / min(L1 + U1)) for continuous FSD); discrete FSD still
compares exact ECDF fractions. The bidirectional fsd/ssd/tsd already
take their verdict from the pairwise test in both directions, so they
and the *_uni functions keep agreeing.

Before -> after:
  (0.1, 0.7) vs (0, 0.8): ssd/tsd 0 -> 1 (= the x10 case)
  vs the patched R, 2611 pairs x 12 tests (fsd/ssd/tsd both types,
    the .uni functions both ways): 58 disagreeing verdicts -> 0
    (ssd 17, tsd 12, ssd_uni 14 + 3, tsd_uni 7 + 5)
  efficient sets and nns_sd_cluster, degrees 1-3 and continuous FSD, on
    the 11-asset daily returns, its last 252 rows, rounded / integer /
    mean-preserving-spread matrices: 35 of 35 equal to R (32 before)
The SD cache entries the tests read were regenerated from the patched
R and are unchanged; entries for the new rounding-noise tests are added.
Every pairwise SD test built the merged threshold grid with np.union1d
and then counted each sample's observations <= every threshold with
np.searchsorted over the 2n thresholds; the two searches were about 40%
of a pairwise test at n = 1e4 (cProfile, tsd). One stable argsort of the
two concatenated sorted runs (a linear merge) plus a cumulative count now
gives the same thresholds and the same integer counts.

Bit-identical: thresholds and counts equal union1d/searchsorted (new
invariant test), and an A/B over 3000 random pairs (normal, integer ties,
rounded with signed zeros, near-duplicates; FSD discrete and continuous,
SSD, TSD, both directions and the .uni forms), the efficient set at
degrees 1-3 and nns_sd_cluster gave identical verdicts.

Timings (best of 7, shared 4-core box), before -> after:
  ssd / ssd_uni n=1e4      1.83 / 1.67 ms -> 0.94 / 0.87 ms
  tsd / tsd_uni n=1e4      2.55 / 2.34 ms -> 2.09 / 1.54 ms
  fsd / ssd / tsd n=3e4    5.6 / 7.9 / 10.0 ms -> 3.5 / 4.4 / 6.1 ms
  efficient set, 11 x 4232, degree 1/2/3: 1.48 / 9.12 / 8.44 ms
                                       -> 1.44 / 3.72 / 4.70 ms
  nns_sd_cluster degree 2: 8.0 ms -> 3.5 ms
At n = 1e3 the tests take 0.02-0.1 ms (no change), already at R's
native .uni cost; the fixed small-n overhead was removed earlier.
@gitRasheed
gitRasheed force-pushed the rasheed/r-fixes-dominance-anova branch from 22c4a06 to ed0c496 Compare November 2, 2026 23:09
@gitRasheed
gitRasheed force-pushed the rasheed/r-fixes-timeseries branch from 72daf66 to ea29dbf Compare November 2, 2026 23:09
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Incorporated through NNS R 14.0 integration #192, merged as bd19c3303222af46da79d8c97f258248eb5674d2. Verified exact head ed0c4963062248f60fb640b334586c0a41938406 is an ancestor of main; original commits and authorship are preserved. The integration includes the subsequent R14 compatibility corrections and passed the complete GitHub Python 3.11-3.14 matrix. This supersedes the separate landing, not the work or review history.

@github-actions github-actions Bot closed this Nov 7, 2026
@github-actions
github-actions Bot deleted the rasheed/r-fixes-dominance-anova branch November 7, 2026 18:53
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.

1 participant