← Back to Pull requests

fix(dtrees): use the real right-child impurity for best-first leaf priority #3772

Closed
unknown closed this pull request without merging on Sep 09, 2026 12:38 UTC
View on GitHub
unknown unknown commented on Sep 01, 2026 15:14 UTC
Author

Summary

Partially fixes #3771 — see #3773 for the rest (a second, independent bug in the same area, found while validating this fix).

buildNode (used by buildBestFirst to rank pending leaves under max_leaf_nodes) computed the leaf-expansion priority as:

intermSummFPType improve = imp * item.totalWeights - impLeft * item.leftWeights
                            - (item.totalWeights - item.leftWeights) * (imp - impLeft);

Two bugs stacked here:

1. item.leftWeights was read before it was ever set for the split being evaluated (it's only assigned split.leftWeights further down). Every freshly built WorkItem initializes leftWeights to 0.0, so improve always collapsed to totalWeights impLeft, ignoring the right child entirely.
2. The right-child impurity was approximated as (imp - impLeft) instead of the actual computed right-child impurity — only exact if the parent's impurity equals the weighted average of the children's, which does not hold in general for Gini/variance impurity.

This made buildBestFirst pick a different (and lower-quality) leaf to expand next than sklearn's BestFirstTreeBuilder would, for the same max_leaf_nodes budget — same tree size, worse fit.

Fix

Use split.leftWeights (not item.leftWeights), and compute the real right-child impurity via the existing convertLeftImpToRight helper before the priority check instead of approximating it. nLeft/leftWeights/left-impurity are captured before that call since it mutates split in place to hold the right-child data.

buildDepthFirst's termination check (split.totalWeights split.impurityDecrease < minImpurityDecrease) doesn't go through this code path, which is why depth-first growth (max_depth, min_samples_split, unconstrained) already matched sklearn exactly and only max_leaf_nodes was affected.

Test plan

  • Repro from #3771: max_leaf_nodes=42 Brier error vs. sklearn drops from +23.0% to +8.7% with this fix alone (remaining gap is #3773, fixed separately).

  • Added cpp/oneapi/dal/algo/decision_forest/test/best_first_leaf_selection.cpp — a 10-point toy case where the buggy vs. fixed formula disagree on accepting the root's only candidate split. Verified it fails on pre-fix code.

  • Existing decision forest unit/system tests — not run in this environment (no CI access); deferred to CI.
  • Diff: 301 lines (+225 / -76) across 3 files Reviewer: avolkov-intel Review duration: 11363m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 301 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.342
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3772"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.1% (MEDIUM) · Higher = larger blast radius
    40.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.8% · Safe to merge
    15.8%