Skip to content

RandomForest trees are more correlated than sklearn's, weakening bagging's variance reduction #437

Closed

Description

@godofecht

Fell out of the #205 diagnosis. Not a benchmark-agreement question; a forest-quality one.

The observation

From the per-tree diagnostics added in #402, on Digits test data at the same mean top-vote fraction:

unanimous predictions
Flow 25.3%
sklearn 19.7%

A forest that agrees with itself more often has trees that are more correlated with each other. Bagging exists to reduce variance by averaging over decorrelated estimators, so higher correlation means less of that benefit than the ensemble size suggests.

Flow currently scores higher on this row (0.955555558 against 0.936111111), so this is not a defect showing up as lost accuracy today. It is a structural property worth understanding, and it would matter more on harder data or with fewer trees.

Where the correlation could come from

In rough order of how much I would suspect each:

  1. Feature subsampling. Check how many features are drawn per split and whether the draw is per-node or per-tree. sklearn's default for classifiers is sqrt(n_features) per split. If Flow draws more, or draws once per tree, trees see more of the same information and correlate.
  2. Tie-breaking. Reduce Digits DecisionTree predictive disparity and compare learned tree structure #204 established that Flow takes the lowest feature index among exactly-tied candidates while sklearn takes the first from a random permutation, and that 68 of 122 split nodes on digits have multiple features at the exact f32 minimum. A deterministic tie-break is a systematic bias shared by every tree in the forest, which is precisely the kind of thing that correlates them. This is the most interesting candidate because it is a direct consequence of a rule we deliberately kept.
  3. Bootstrap sampling. The per-tree bootstrap unique-sample fraction is already emitted; compare it against sklearn's expected ~63.2%.
  4. Tree depth or stopping rules producing more similar structures.

Suggested approach

Measure before changing anything. The diagnostics needed are already emitted per #402: per-tree node and leaf counts, depths, root splits, bootstrap checksums and unique fractions, vote margins. Compare the distribution of root split features across the 10 trees on both sides. If Flow's trees share a root feature far more often than sklearn's, candidate 1 or 2 is confirmed and which one follows from whether the shared features are tied.

If it turns out to be the tie-break, note the tension: #204 and #426 deliberately kept and pinned the lowest-index rule, and tests/test_tree_split_tiebreak.flow enforces it. Randomising ties inside the forest builder only, leaving the single-tree builder deterministic, may be the right resolution, but that is a design decision worth stating explicitly rather than slipping in.

What would count as done

Either an explanation of the correlation with numbers, or a change that reduces it with the effect on the canonical RandomForest row reported both ways. Do not chase the accuracy number; Flow is already ahead there. The question is whether the ensemble is doing what an ensemble is supposed to do.

Activity

  1. godofecht commented on Aug 20, 2026

    @godofecht
    OwnerAuthor

    Measured: the premise does not hold. Flow's trees are less correlated than sklearn's.

    I measured tree correlation directly rather than through the unanimity proxy. On the
    canonical row (digits, 10 trees, max_depth=10, seed 42, standard-scaled, canonical
    fixture split) Flow's forest is less correlated than sklearn's on every direct
    measure, while being more unanimous.

    Unanimity and correlation move in opposite directions here, so unanimity is not
    serving as a correlation measure.

    Direct correlation, both sides, same environment

    metric Flow sklearn
    forest accuracy 0.955556 0.936111
    mean per-tree accuracy 0.754722 0.753333
    mean pairwise tree agreement 0.625185 0.629198
    expected agreement if trees were independent 0.576275 0.574194
    excess agreement over independence 0.048910 0.055004
    kappa correlation 0.115429 0.129176
    unanimous predictions 0.252778 0.205556

    Mean per-tree accuracy is the same to within 0.0014, so the two forests are built
    from equally strong trees and the correlation numbers are directly comparable.
    Flow's agreement in excess of independence is 0.0489 against sklearn's 0.0550, and
    kappa is 0.1154 against 0.1292. Flow is roughly 11% less correlated.

    Note on the sklearn unanimity figure: locally I measure 0.205556 where the committed
    sklearn_results_v2.txt has 0.197222. Per-tree node counts match the committed file
    exactly, so the trees are identical and the 3-row difference is a platform artifact
    in StandardScaler output, not a modelling difference. Both figures sit well below
    Flow's 0.253 and the conclusion is unchanged.

    Why unanimity is higher despite lower correlation

    Decomposing pairwise agreement into its four outcomes:

    Flow sklearn
    P(pair both correct) 0.604012 0.602716
    P(pair both wrong, same label) 0.021173 0.026481
    P(pair both wrong, different label) 0.073395 0.069568
    P(pair exactly one correct) 0.301420 0.301235
    mean distinct labels per test row 2.7778 2.7000
    distinct-label histogram (1..7 trees) 91, 72, 83, 68, 34, 10, 2 74, 100, 90, 58, 32, 6, 0
    unanimous and wrong 0.000000 0.000000

    Correlated error is the quantity bagging is meant to suppress, and Flow has 20% less
    of it (0.0212 against 0.0265). sklearn's higher total agreement is concentrated
    entirely in trees being wrong the same way.

    Every unanimous prediction is correct on both sides. Flow's extra unanimity is 91
    test rows where all ten trees are right against sklearn's 74. That is a strength
    signal about easy examples. Flow's distinct-label histogram also has more mass in
    the tail (2 rows split 7 ways; sklearn's maximum is 6), so Flow is simultaneously
    more decisive on easy rows and more diverse on hard ones. That is the profile you
    want from an ensemble, and it is consistent with Flow's higher forest accuracy at
    equal tree strength.

    Candidates 1, 3 and 4: checked, all clear

    1. Feature subsampling. lib/scikit/ensemble.flow:43-45 sets
    max_features = floor(sqrt(n_features)) = 8 for digits, matching sklearn's classifier
    default. The draw happens per node inside _build_classifier_tree_rf
    (lib/scikit/tree.flow:951-965), not per tree. This matches sklearn.

    I also checked the PRNG itself, since a biased draw would look identical to a biased
    tie-break. Simulating prng_int(p, 64) (which reduces to the low 6 bits of
    xorshift32) over 200k nodes gives quadrant shares 0.2502 / 0.2501 / 0.2497 / 0.2500
    and mean drawn index 31.4955 against a uniform 31.5. The draw is clean.

    3. Bootstrap sampling. For n=1437 the theoretical unique fraction is 0.63225,
    with sd 0.00822 per tree and 0.00260 for a mean of ten. Flow's mean is 0.62867
    (1.38 sd low), sklearn's is 0.63549 (1.25 sd high). Both are inside noise. Flow's
    sample is very slightly more duplicated, which decorrelates rather than correlates.

    4. Depth and stopping. Every tree reaches depth 10 on both sides. Total internal
    split nodes: Flow 1554, sklearn 1551. Per-tree node counts are Flow mean 311.8 sd
    16.3 (range 291-339) against sklearn mean 311.2 sd 27.4 (range 271-353). Flow's trees
    are more uniform in size, but as the table above shows this does not translate into
    more correlated predictions.

    The root-split test proposed in the issue comes back negative

    Distinct root features across the 10 trees on digits:

    • Flow: 53, 36, 38, 21, 33, 10, 21, 60, 58, 33 = 8 distinct of 10
    • sklearn: 21, 58, 33, 21, 42, 26, 34, 38, 58, 26 = 7 distinct of 10

    Flow's roots are more diverse. This test does not detect the tie-break, because the
    root sees 1437 samples and exact f32 ties in weighted Gini are rare there. The 68 of
    122 tied nodes from #204 are concentrated in deep, small nodes.

    Candidate 2: the tie-break is real, and it is a feature-usage bias

    Looking at all internal nodes rather than just the roots, the deterministic rule is
    clearly visible:

    split-feature usage Flow sklearn
    mean split feature index 26.08 32.04
    fraction of splits with feature index < 32 0.6396 0.5035
    split-feature entropy (bits, max 6) 5.4743 5.5482
    mean pairwise split-histogram cosine 0.8400 0.8019

    By feature quadrant (counts of splits):

    features Flow sklearn
    0-15 516 325
    16-31 478 456
    32-47 326 397
    48-63 234 373

    Flow uses the first sixteen pixels 59% more often than sklearn and the last sixteen
    37% less often, a monotone decline across the index range. sklearn sits near flat.

    The cause is lib/scikit/tree.flow:966: the candidate loop walks feature_mask in
    ascending index order, so among features tied at the exact f32 minimum the lowest
    index always wins. sklearn scans its sampled features in Fisher-Yates order, so the
    winner among ties is uniform over the tied set.

    Ablation confirms causation

    I changed only the scan order in _build_classifier_tree_rf, recording the rejection-
    sampling draw order and iterating that instead of ascending index order. The set of
    candidate features per node is untouched; only which tied feature wins changes.

    Flow main Flow, drawn-order ties sklearn
    mean split feature index 26.08 31.28 32.04
    fraction feature index < 32 0.6396 0.5208 0.5035
    split-feature entropy (bits) 5.4743 5.5258 5.5482
    quadrant counts 516/478/326/234 332/471/394/345 325/456/397/373
    forest accuracy 0.955556 0.955556 0.936111
    mean per-tree accuracy 0.7547 0.7606 0.7533
    kappa correlation 0.1154 0.1231 0.1292
    P(pair both wrong, same label) 0.0212 0.0245 0.0265
    unanimous predictions 0.2528 0.2417 0.2056

    One line of scan order closes about 85% of the feature-usage gap and lands the
    quadrant profile almost exactly on sklearn's. The tie-break is confirmed as the sole
    source of the low-index bias.

    Two things follow that argue against making the change:

    1. Forest accuracy is bit-identical at 0.955555558. The canonical row does not move.
    2. Randomising ties makes correlation worse. Kappa rises from 0.1154 to 0.1231 and
      correlated errors from 0.0212 to 0.0245, both moving toward sklearn. Unanimity only
      falls from 0.2528 to 0.2417, nowhere near sklearn's 0.2056.

    So the deterministic tie-break is not the source of any correlation problem. On these
    measurements it is mildly protective.

    Recommendation: no change

    I recommend closing this without a code change, for three reasons.

    The stated defect is not present. Flow's trees are already less correlated than
    sklearn's by every direct measure, and Flow's correlated-error rate is 20% lower.
    Unanimity was standing in for correlation and it does not track it; the two move in
    opposite directions across all three configurations measured above.

    The tie-break bias is real and now quantified, but it is a feature-usage bias rather
    than a correlation problem. It costs nothing on accuracy and removing it would move
    three metrics the wrong way.

    #204 and #426 pinned the lowest-index rule deliberately and
    tests/test_tree_split_tiebreak.flow enforces it. Randomising ties inside the forest
    builder only would buy a feature-usage histogram closer to sklearn's, at the cost of
    splitting the tie-break rule in two, weakening the property that test pins, and
    slightly increasing tree correlation. That trade does not look worth taking on this
    evidence.

    The one residual difference worth recording for a future issue: sklearn's
    BestSplitter keeps drawing until it has evaluated max_features non-constant
    features, tracking n_found_constants and n_drawn_constants. Flow draws exactly
    max_features features and then skips constant columns
    (lib/scikit/tree.flow:975-983), so it evaluates fewer real candidates per node. On
    digits many pixel columns are constant at a given node. This is the most likely
    source of the ~15% of the feature-usage gap the tie-break ablation did not close. It
    is a separate question from this issue and does not affect the correlation finding.

    Reproduction

    Diagnostics on both sides come from the #402 emitters plus a scratch harness computing
    per-tree test predictions, pairwise agreement and its decomposition, and split-feature
    histograms over all internal nodes. Flow-side numbers reproduce the committed
    flow_results_v2.txt exactly (total internal splits 1554, per-tree node and leaf counts
    matching); sklearn-side reproduces sklearn_results_v2.txt node counts exactly. The
    harness was scratch-only and is not committed. lib/scikit/tree.flow was reverted after
    the ablation and the worktree is clean.

  2. godofecht commented on Aug 20, 2026

    @godofecht
    OwnerAuthor

    Closing. The premise of this issue is wrong, and I filed it on a misleading proxy.

    I opened this off the unanimity figure alone: Flow unanimous on 25.3% of Digits predictions against sklearn's 19.7%, and inferred more correlated trees. Measuring correlation directly gives the opposite answer:

    Flow sklearn
    mean per-tree accuracy 0.754722 0.753333
    excess agreement over independence 0.048910 0.055004
    kappa correlation 0.115429 0.129176
    unanimous 0.252778 0.205556

    Tree strength is equal to within 0.0014, so those correlation figures are directly comparable. Flow's trees are about 11% less correlated, not more.

    Why unanimity pointed the wrong way

    Decomposing pairwise agreement: Flow's P(pair both wrong, same label) is 0.0212 against sklearn's 0.0265. Correlated error, the thing bagging exists to suppress, is 20% lower in Flow. sklearn's extra agreement is entirely trees being wrong the same way.

    Unanimous-and-wrong is 0.000000 on both sides, so Flow's extra unanimity is 91 rows where all ten trees are right against sklearn's 74. Flow also carries more tail mass, with 2 rows splitting 7 ways where sklearn maxes at 6. Decisive on easy rows, diverse on hard ones, which is the good version of this.

    Unanimity conflates "agrees because correct" with "agrees because correlated". It should not be read as a correlation proxy, and I should not have filed on it without the decomposition.

    The tie-break is real, but it is not correlation

    The root-split test I proposed comes back negative: Flow has 8 distinct roots of 10 against sklearn's 7, so Flow's roots are more diverse. The root sees 1437 samples where exact f32 ties are rare, so it cannot detect the tie-break at all.

    Over all 1554 internal nodes the bias is clear: Flow's mean split feature index is 26.08 against sklearn's 32.04, and 63.96% of splits use a feature below index 32 against sklearn's 50.35%. That is a feature-usage bias from the lowest-index rule, confirmed by ablation. The PRNG was ruled out as an alternative explanation (simulating prng_int(p, 64) over 200k nodes gives mean 31.4955 against a uniform 31.5).

    The ablation settles it against changing anything

    Scanning in drawn order instead of ascending index moves the mean index 26.08 to 31.28 and the quadrant counts almost exactly onto sklearn's. But forest accuracy stays bit-identical at 0.955555558, and kappa rises 0.1154 to 0.1231 with correlated errors going 0.0212 to 0.0245.

    Randomising ties makes correlation worse. So the change buys a prettier feature-usage histogram at the cost of the property this issue was opened to protect, while weakening tests/test_tree_split_tiebreak.flow which #204 and #426 deliberately pinned.

    Candidates 1, 3 and 4 are cleared with numbers: drawn per node, correct; bootstrap unique fraction 0.62867 against a theoretical 0.63225, 1.38 sd, inside noise; all trees at depth 10, 1554 nodes against sklearn's 1551.

    Closing as diagnosed, with no change. Two residuals worth pursuing separately are recorded below.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions