Skip to content

Stochastic dominance: midpoint check and compensated prefix sums - #167

Closed
gitRasheed wants to merge 3 commits into
rasheed/mirror-timeseriesfrom
rasheed/mirror-dominance
Closed

gitRasheed wants to merge 3 commits into
rasheed/mirror-timeseriesfrom
rasheed/mirror-dominance

Conversation

@gitRasheed

@gitRasheed gitRasheed commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Two fixes from the R stochastic dominance PR that the first mirror did not have. Continuous FSD missed dominance when the two ratio curves agreed at every knot and differed between them. The prefix sums were plain running sums, whose rounding grows with n past the 64 * eps * S^d tie tolerance.

Stacked on #166; merge with a merge commit.

Changes

  • Continuous FSD also compares the ratios at interval midpoints while the curves have not yet differed. Two linear-fractional ratios that agree at both ends and the midpoint agree on the whole interval. fsd([0, 1, 1], [0, 0, 1], "continuous") goes from 0 to 1, as in R.
  • Prefix sums and the sample mean use Neumaier compensation, equal to R's sequential sum. For z = qnorm(ppoints(1e5)), x = z + 100 and y = 2z + 100, ssd/tsd go from 0/0 to 1/1.

The existing SD parity cases are unchanged.

Tests

New parity cases for the midpoint and large-sample cases in NNS.FSD, NNS.FSD.uni, NNS.SSD, NNS.TSD and the efficient set. The parity cache encodes R 13.3 plus the fixes in OVVO-Financial/NNS#73-#92.

Mirrors OVVO-Financial/NNS#82

Update

This push mirrors OVVO-Financial/NNS#105. Each pair is compared centred on its joint midrange, with a tolerance for the rounding of the shifted inputs. The fallback matches R and the vendored core on the translation cases: TSD with both samples shifted by 20000 now matches the unshifted answer (0, where it gave 1), and at +1e6 the efficient set is [0], where it was [0, 1].

Review

GPT-6.1 Sol reviewed this slice and found nothing. It compared 500 small, tied, constant and unequal-length cases against live R.

…nts (mirror of the R fix)

When the continuous ratios agree at every knot and X's is lower between
two knots, X dominates Y, but the test saw equal curves and returned no
dominance in both directions. Two linear-fractional ratios that agree at
both ends of an interval and at its midpoint agree on the whole interval,
so, as in R's sd_dom_pair (sd-dominance), also compare them at the
midpoint while the curves have not yet differed.

Before -> after (= R), fsd(type="continuous"); fsd_uni x>y, y>x:
  x = [0, 1, 1], y = [0, 0, 1]    0; 0, 0  ->  1; 1, 0
  x = [1, 1],    y = [0, 1]       0; 0, 0  ->  1; 1, 0
SSD and TSD need no such check: their curves are polynomial between the
knots with the next-lower LPM as derivative.
The pairwise SD test treats differences up to 64 * eps * S^d as ties,
but its prefix sums and the sample mean were plain running sums whose
rounding grows with the sample size. As in R's SD finish_col
(sd-dominance), use Neumaier compensated sums. np.cumsum adds in order,
so the per-step error terms are recovered from the running sums without
a Python loop, and the result equals R's sequential Neumaier sum.

Before -> after (= R), z = qnorm(ppoints(1e5)), x = z + 100, y = 2z + 100:
  ssd / tsd            0 / 0 -> 1 / 1 ("X SSD Y" / "X TSD Y")
  ssd_uni / tsd_uni    0 / 0 -> 1 / 1
The existing SD parity cases are unchanged.
… fix)

The tie tolerances scaled with |value|^degree, so a common shift such as a
price level of 20000 grew the TSD tolerance past real differences:
tsd_uni(x + 20000, y + 20000) was 1 where tsd_uni(x, y) is 0. As R now does,
every test except discrete FSD centres the pair on its joint midrange c and
uses E1 = 64 eps S + din (din = 4 eps |c|) for the mean gate, SSD and the
continuous FSD ratios, and 64 eps S^2 + 2 din (|LPM1x| + |LPM1y| + din) per
threshold for TSD.
@gitRasheed
gitRasheed force-pushed the rasheed/mirror-timeseries branch from d581bd9 to d2767c8 Compare November 2, 2026 23:09
@gitRasheed
gitRasheed force-pushed the rasheed/mirror-dominance branch from 795254e to 11a8a43 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 11a8a430b898f41d4544da264ff9255e403f1473 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/mirror-dominance 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