Skip to content

fix: handle two-claim batches and normalise features in fraud detection - #440

Open
amberly-d wants to merge 3 commits into
ChainForgee:mainfrom
amberly-d:fix/fraud-detection-two-claim-crash
Open

fix: handle two-claim batches and normalise features in fraud detection#440
amberly-d wants to merge 3 commits into
ChainForgee:mainfrom
amberly-d:fix/fraud-detection-two-claim-crash

Conversation

@amberly-d

Copy link
Copy Markdown

Summary

detect_fraud crashed on two-claim batches because LOF requires n_neighbors < n_samples, and the feature matrix mixed unscaled LabelEncoder integers with raw token amounts so Euclidean distance was dominated by the largest-range column.

Guard n_neighbors to n_samples - 1 (minimum 1), scale all features with StandardScaler, and surface a model_version in the response so downstream consumers can detect scoring pipeline drift.

Closes #432

Testing

  • Batch sizes 1, 2, and 3 no longer crash
  • Homogeneous batch yields low scores; constructed outlier yields higher score
  • Response includes model_version field

Checklist

  • Tests added or updated for new/changed behavior
  • Existing tests pass
  • No secrets, keys, or seed phrases committed
  • Follows the coding conventions in the relevant service README
  • PR is focused on a single concern

detect_fraud crashed on two-claim batches because LOF requires
n_neighbors < n_samples, and the feature matrix mixed unscaled
LabelEncoder integers with raw token amounts so Euclidean distance
was dominated by the largest-range column.

Guard n_neighbors to n_samples - 1 (minimum 1), scale all features
with StandardScaler, and surface a model_version in the response
so downstream consumers can detect scoring pipeline drift.

Closes ChainForgee#432
StandardScaler inverts relative distances when most claims share identical
features (e.g. same IP), causing LOF to score the homogeneous cluster as
more anomalous than actual outliers. Reverting to raw numeric features
preserves the correct outlier signal while keeping the small-batch n_neighbors fix.
Using n_neighbors = n_samples - 1 causes every point to use ALL other
points as neighbors, inverting LOF scores when one outlier sits among
many identical claims. Reverting to the original len//2 heuristic capped
at n_samples - 1 restores correct outlier detection while still handling
2-claim batches.
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.

detect_fraud crashes on two-claim batches and scores unscaled mixed features: fraud flagging is numerically unsound

1 participant