Repository navigation
mode: break tied discrete modes by gravity, not their mean (mirrors R #111) - #178
Closed
gitRasheed wants to merge 1 commit into
Closed
gitRasheed wants to merge 1 commit into
gitRasheed wants to merge 1 commit into
Conversation
…ass-ties)
Mirrors R's discrete NNS.mode rule (OVVO-Financial/NNS mode-class-ties).
Tied integer modes resolve to the one nearest NNS.gravity, then the lower,
because a mean of tied classes is no class. The small-sample rounded median
for three or fewer values is gone, so multi=True returns every mode there too.
This reaches nns_part's "mode" and "mode_class" noise reduction and the
univariate CLASS fits built on it.
Before -> after (= R):
nns_mode([1, 1, 3, 3], discrete=True) 2 -> 1
nns_mode(np.repeat(np.arange(20), 400), discrete=True) 9.5 -> 9
nns_mode([1, 3], discrete=True, multi=True) [2] -> [1, 3]
nns_reg(1:8, [1, 1, 3, 3, 2, 2, 2, 2], type="CLASS", order=2) fitted
1 2 2 2 2 2 2 2 -> 1 1 1 1 2 2 2 2
The vendored NNS-core still averages, so the discrete path uses the NumPy
port until the core is re-vendored; the native bit-identity check for it is a
strict xfail until then.
64 cache entries (NNS.reg 52, NNS.boost.numeric 9, NNS.part 2, NNS.mode 1)
predate the R change and were refilled from the cache's R build plus that
change alone; the other entries in those shards came back identical.
gitRasheed
force-pushed
the
rasheed/py-mode-class-ties
branch
from
November 3, 2026 02:17
65ac5f7 to
be84664
Compare
This was referenced Nov 3, 2026
Contributor
|
Incorporated through NNS R 14.0 integration #192, merged as |
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.
R's discrete
NNS.modeused to average tied integer modes, and for three or fewer values it returned their rounded median. OVVO-Financial/NNS#111 (merged) resolves a tie to the mode nearestNNS.gravity(x), then to the lower one, because a mean of tied classes is not a class. This slice mirrors that. The discrete mode backsnns_part's "mode" and "mode_class" noise reduction, so it reaches univariatetype="CLASS"fits as well.Stacked on #176; merge with a merge commit.
Changes
_discrete_modedrops the small-sample median branch. Tied modes resolve to the one nearestnns_gravityof the raw values, then the lower, andmulti=Truereturns every mode for any n. Before and after, with after equal to R main:[1, 1, 3, 3]2 -> 1,np.repeat(np.arange(20), 400)9.5 -> 9,[1, 3]withmulti=True[2] -> [1, 3],[3e9, 3e9, -5e9, -5e9, 1]-1e9 -> -5e9.nns_reg(1:8, [1, 1, 3, 3, 2, 2, 2, 2], type="CLASS", order=2)used to predict class 2 for x = 2:4, a cell that holds only classes 1 and 3. It now fits 1 1 1 1 2 2 2 2, as R does.nns_mode(discrete=True)uses the NumPy port even when the extension is built. Native and fallback builds then give R's answer. NNS-core#38 carries the same rule, and the re-vendor of Bump version to 1.0.6 #38 removes this routing line and the two strict xfails below.Tests
[1, 3],[1, 2],[2, 2, 7],[4],[1, 1, 3, 3]), are strict xfails while the vendored core averages. They fail on the mismatch itself and will XPASS, and so fail, once the core is re-vendored.git diff --minimalgives 40.7k lines, which matches the 44k JSON values that changed.NNS_DISABLE_NATIVE=1: 2855 passed, 29 skipped. Both runs also have 2 xpassed, the existing non-strict live-R VAR xfails, which pass against patched R.Mirrors OVVO-Financial/NNS#111
Review
GPT-6.1 Sol reviewed this slice and asked for fixes.
nns_mode([1e308, 1.5e308], discrete=True)returns 1e308 where R returns NA, because the gravity overflows. Inputs within a factor of two of DBL_MAX are out of scope, the same call I made for NNS-core#38.