Skip to content

Use interior histogram edges when scoring LODA projections - #777

Open
antonsoo wants to merge 1 commit into
yzhao062:developmentfrom
antonsoo:fix/loda-histogram-bin-lookup
Open

antonsoo wants to merge 1 commit into
yzhao062:developmentfrom
antonsoo:fix/loda-histogram-bin-lookup

Conversation

@antonsoo

Copy link
Copy Markdown

LODA builds each histogram correctly, but searches its first n_bins - 1 edges when scoring projected values. That includes the outer left edge and omits the last interior edge, assigning most values inside bins to the wrong density.

For a simple example:

from pyod.models.loda import LODA

X = [[0.], [.1], [.2], [1.1], [2.1], [3.]]
model = LODA(n_bins=3, n_random_cuts=1, random_state=0).fit(X)
print(model.decision_scores_.round(3))

The histogram counts are [3, 1, 2], so the expected scores are [0.693, 0.693, 0.693, 1.792, 1.099, 1.099]. Currently they are [0.693, 1.792, 1.792, 1.099, 1.099, 1.099].

Search the interior edges with side='right' in both fit and decision_function, for fixed and automatic bin counts. This matches NumPy's left-closed bins while retaining the existing treatment of exact boundaries and values outside the histogram support. Projection generation, histogram counts, smoothing and score weighting are unchanged.

The regression covers bin interiors, boundaries, outside-support values and independent scalar histogram counts across multiple projections, including one-bin controls. The interior and multi-bin cases fail before the fix. All 32 LODA tests pass after it, with 97% statement coverage of pyod/models/loda.py and all four changed lookups covered.

In a separate 36-configuration run on sklearn's Digits, Wine and Iris datasets, scores now match direct interval-count references exactly. Before the fix, 8,104 training scores and 7,143 query scores differed; both counts are now zero. All projections and histograms are identical before/after, and one-bin controls retain identical scores. These checks establish correct histogram scoring, not improved detection quality.

Submission checks

  • Followed the contribution guidance and targeted development.
  • Checked existing issues and open PRs; no matching fix found.
  • Added an explanation and failing-then-passing regressions.
  • Ran the complete affected detector suite locally with coverage.
  • Upstream CI pending. Local validation used Linux/Python 3.12; the full cross-platform suite was not run locally.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T08:09:33.759198Z 0411a1f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant