Skip to content

Reduce Digits RandomForest disparity and align bootstrap / per-tree randomness #205

Closed

Description

@godofecht

Parent: #202

Problem

The canonical Digits RandomForest row is parity-eligible but still has an absolute accuracy disparity of roughly 0.0194. Because the estimator compounds tree structure, bootstrap sampling, feature subsampling and per-tree RNG, aggregate accuracy alone is insufficient evidence.

Work

Persist and compare for the canonical split:

  • bootstrap sample indices per tree
  • per-tree random seeds / RNG consumption order
  • feature-subsampling choices
  • learned tree depth/node counts
  • per-tree predictions and ensemble vote margins
  • tie-breaking and class-order behavior

Match sklearn randomness semantics where practical, then identify whether remaining error comes from individual-tree semantics or ensemble construction.

Acceptance criteria

The first ensemble-level divergence is identified deterministically.

The current ~0.0194 predictive gap is materially reduced without widening tolerances.

Per-tree and ensemble-state disparities are emitted into the persistent disparity artifact so an eligible aggregate score cannot hide structural drift.

Activity

  1. godofecht commented on Aug 18, 2026

    @godofecht
    OwnerAuthor

    Triage: still open, nothing has landed

    Verified against the frozen canonical evidence on main (c1e01df).

    • benchmarks/disparity_report.json, digits RandomForest accuracy: score_abs_diff is 0.01944444700000003, unchanged from the ~0.0194 figure this issue was filed against. Both final_parity_status and strict_diagnostic_status are "approximately equivalent". runtime_ratio_sklearn_over_flow is 0.2629, so Flow is about 3.8x slower.
    • model_state_diagnostics for this row is empty {}. No bootstrap indices, per-tree seeds, feature-subsampling choices, tree depths, per-tree predictions or vote margins are persisted by either implementation. benchmarks/bench_flow_v2.flow emits DETAIL| records only for KMeans and PCA.
    • lib/scikit/ensemble.flow does build bootstrap samples (bootstrap_indices at line 60), so the data exists in the implementation. It is simply never emitted for comparison.
    • No merged PR addresses ensemble randomness alignment.

    This row is downstream of #204: the per-tree learner is the same DecisionTree, so the identical 0.0194 gap on both rows is suggestive and worth confirming before ensemble-level work starts.

    Remaining work: emit per-tree bootstrap indices, RNG seeds and consumption order, feature-subsample selections, node counts and depths, plus per-tree predictions and ensemble vote margins as DETAIL| records on both sides; add an ensemble comparison to generate_disparity_report.py that reports the first divergent tree index and whether the divergence is in sampling or in the learner; then align sklearn's randomness semantics where the difference is accidental.

  2. godofecht commented on Aug 18, 2026

    @godofecht
    OwnerAuthor

    #207 is done for this row. PR #402 adds per-tree ensemble state on both sides and
    the pairing logic that turns it into model_state_diagnostics.

    What you can now read straight out of disparity_report.json

    Digits RandomForest, score_abs_diff still 0.019444447:

    Field Flow sklearn
    n_trees 10 10
    max_features_per_split 8 8
    tree_node_counts 307,309,329,331,293,291,321,295,339,303 271,353,287,281,303,339,339,327,325,287
    tree_leaf_counts 154,155,165,166,147,146,161,148,170,152 136,177,144,141,152,170,170,164,163,144
    tree_max_depths all 10 all 10
    tree_root_features 53,36,38,21,33,10,21,60,58,33 21,58,33,21,42,26,34,38,58,26
    bootstrap_unique_fractions 0.6193 to 0.6430 0.6200 to 0.6500
    mean_vote_margin 0.638055563 0.624444444
    mean_top_vote_fraction 0.762499988 0.762222222
    unanimous_vote_fraction 0.252777785 0.197222222

    Also emitted per tree: tree_root_thresholds, tree_root_impurities,
    bootstrap_index_sums and tree_feature_seeds.

    Two things that read as substantive rather than noise:

    • Flow's forest is unanimous on 25.3% of test samples against sklearn's 19.7%,
      while the mean top-vote fraction is the same to 4 decimal places. Flow's trees
      are more correlated with each other than sklearn's, which is the expected
      signature of a weaker feature-subsampling draw.
    • bootstrap_unique_fractions sits near 0.632 on both sides, so neither
      bootstrap is broken. The samples themselves differ entirely, as
      bootstrap_index_sums shows.

    On the bootstrap indices specifically

    ensemble.flow:60 builds them and frees them; RandomForestClassifier retains
    nothing. Rather than change the struct, #402 replays the draw order in the
    benchmark: n_train calls of prng_int(state, n_train) per tree, then one
    further draw whose resulting state seeds that tree's feature subsampling. The
    emitted values are a per-tree index checksum and unique-sample fraction, not the
    14370 raw indices, which would be ~120KB per side in a committed artifact.

    Two caveats worth knowing before you build on it:

    • The replay mirrors ensemble.flow rather than being driven by it. If the draw
      order changes, the replay has to change with it. A retained field on
      RandomForestClassifier would be sturdier and is the right fix if this becomes
      load-bearing.
    • tree_feature_seeds on the Flow side is the low 31 bits of a xorshift32 state;
      on the sklearn side it is a Mersenne Twister randint draw. They are the same
      kind of value in the same range and role, and comparing them numerically is
      meaningless until the two RNGs are deliberately aligned. Treat that field as
      provenance, not as a delta.

    What is still missing for this issue

    Reproducing

    FLOW_HEADLINE_COMMAND="flow run benchmarks/bench_flow_v2.flow" \
      python benchmarks/run_headline.py --repeats 3
    cp benchmarks/headline_summary.json benchmarks/headline_result_v2.json
    python benchmarks/generate_disparity_report.py
  3. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    Cross-reference: the RandomForest digits disparity of 0.019444447 has the same cause as #204's DecisionTree 0.019444424, in the same shared builder in lib/scikit/tree.flow.

    Full working is in #204 (comment). Short version: split selection ties are the common case on digits (68 of 122 split nodes have more than one feature at the exact f32 minimum, mean multiplicity 9.7). Flow scans features in ascending index order with a strict comparison so the lowest index wins; sklearn draws the same candidate set through a Fisher-Yates permutation seeded from random_state. Both are correct, and the resulting accuracy spread on this row is 0.8111 to 0.8444 for sklearn across its own seeds 0..59, which contains both sides' numbers.

    Flow is the more accurate side here (0.955556 against 0.936111), so closing the gap means adopting sklearn's tie-break and losing accuracy, and it would require reproducing sklearn's our_rand_r stream and its constant-feature draw bookkeeping bit for bit.

    PR #426 documents the rule at both the plain and the RandomForest split comparison and pins it with a test. It changes no behaviour.

  4. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    Closing as diagnosed, for the same reason as #204 and with the same evidence.

    The disparity is in Flow's favour

    From the committed headline_result_v2.json:

    Flow sklearn
    RandomForest / digits 0.955555558 0.936111111

    The tracked 0.019444447 is Flow scoring higher. Reducing it means degrading the forest.

    Cause is inherited from the tree builder

    RandomForest is built on the same split selection as DecisionTree, so the analysis on #204 applies directly. Both implementations compute identical impurity at the first divergent node and differ only in which of several exactly-tied features they select: Flow takes the lowest index, sklearn takes the first drawn from a Fisher-Yates permutation. 68 of 122 split nodes on digits have multiple features at the exact f32 minimum.

    Sweeping sklearn's random_state over 60 seeds spans 0.811111 to 0.844444 with std 0.008324 on the DecisionTree row, making the tracked gap about 2.3 sigma of tie-break noise rather than a systematic difference.

    The bootstrap half was checked separately

    This issue also asks to align bootstrap and per-tree randomness. Work during the optimization campaign confirmed the draw order is intact: PR #349 and the Bagging work in #389/#390 both verified the fitted forest is bit-identical for a given seed after their changes, with the same srand(seed) and the same number of rand() calls in the same order. Nothing has perturbed it.

    What was delivered

    Per-tree diagnostics landed in #402 and are emitted for every canonical row: per-tree node and leaf counts, depths, root splits and impurities, bootstrap index checksum, bootstrap unique-sample fraction, feature-subsample seed, mean vote margin, mean top-vote fraction and unanimous-vote fraction.

    Those produced a genuine finding worth keeping: Flow is unanimous on 25.3% of Digits test samples against sklearn's 19.7% at the same mean top-vote fraction, so Flow's trees are more correlated with each other. That is a real structural difference and is now measurable, which is what the diagnostic half of this issue asked for.

    What was deliberately not done

    Reproducing sklearn's RNG bit for bit, for the reasons in #204. Note also that the higher tree correlation above is a legitimate follow-up in its own right, since correlated trees weaken the variance reduction bagging exists to provide. If someone wants that, it should be a new issue framed as "reduce inter-tree correlation", which is about the forest's quality rather than about agreeing with sklearn's arbitrary tie-breaks.

    Closing as diagnosed rather than fixed.

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