Skip to content

mode: break tied discrete modes by gravity, not their mean (mirrors R #111) - #178

Closed
gitRasheed wants to merge 1 commit into
rasheed/book-stack-univariate-kfrom
rasheed/py-mode-class-ties
Closed

gitRasheed wants to merge 1 commit into
rasheed/book-stack-univariate-kfrom
rasheed/py-mode-class-ties

Conversation

@gitRasheed

Copy link
Copy Markdown
Collaborator

R's discrete NNS.mode used 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 nearest NNS.gravity(x), then to the lower one, because a mean of tied classes is not a class. This slice mirrors that. The discrete mode backs nns_part's "mode" and "mode_class" noise reduction, so it reaches univariate type="CLASS" fits as well.

Stacked on #176; merge with a merge commit.

Changes

  • _discrete_mode drops the small-sample median branch. Tied modes resolve to the one nearest nns_gravity of the raw values, then the lower, and multi=True returns 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] with multi=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.
  • The vendored NNS-core (a0d3b93) still averages tied modes, so 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

Mirrors OVVO-Financial/NNS#111

Review

GPT-6.1 Sol reviewed this slice and asked for fixes.

  • Accepted: the native comparison had no tiny samples, where the old core's small-sample branch still differs. I added the fixed tiny-sample check as a strict xfail.
  • Rejected: 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.

…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.
@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 be84664319cbba64542769392d9701aed54cd9f0 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/py-mode-class-ties branch November 7, 2026 18:52
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