Skip to content

Average repeated samples of an edge expanded at max_depth - #136

Open
salma2vec wants to merge 1 commit into
google-deepmind:mainfrom
salma2vec:fix/average-depth-limited-leaf-samples
Open

salma2vec wants to merge 1 commit into
google-deepmind:mainfrom
salma2vec:fix/average-depth-limited-leaf-samples

Conversation

@salma2vec

@salma2vec salma2vec commented Oct 22, 2026 •

Copy link
Copy Markdown

Fixes #135

At the depth cutoff, simulate can return an edge whose child already exists, and expand evaluates it again. With a stochastic recurrent_fn (the max_depth=1 stochastic-bandit setup recommended in #33), every evaluation is a new sample. expand overwrote the edge's children_rewards and children_discounts and the child's node_values with it, so Tree.qvalues() reported the last sample while children_visits kept counting all of them. In the issue's bandit, arm 0 has sample mean 0.648 over 32 visits but reported Q = 0.25, and the search picks arm 1 (constant 0.5).

Change

search.body_fun now:

  1. reads the edge's reward, discount, child value and visit count before expand (_leaf_statistics);
  2. runs expand and backward unchanged, so backward still propagates the newest sampled return and ancestor values stay averages of sampled returns;
  3. for a revisited edge only, replaces the edge's reward, discount and child value (node_values and children_values) with running means of all samples (_average_revisited_leaf).

First expansions and deterministic environments are unaffected. The incremental mean m + (x - m) / n returns m exactly when x == m, so the existing golden-tree tests, which use max_depth with deterministic rewards, reproduce unchanged.

Averaging reward, discount and value separately is exact when the discount is the same across samples. With a stochastic discount, Tree.qvalues() uses E[r] + E[γ]E[v], which approximates E[r + γv]. Keeping an exact per-edge return sum would need a new Tree field, which seemed too large a change for this fix.

Tests

  • test_depth_limited_search_averages_stochastic_samples: one valid action, uniform random reward and value, constant discount, max_depth=1. It checks that the edge's Q-value equals the mean sampled return implied by the root value. On main: 0.736 vs 0.442.
  • test_depth_limited_search_selects_better_stochastic_arm: the issue's bandit. Q-values equal the per-arm sample means and arm 0 is selected. On main, arm 0's Q is its last sample (1.0) instead of its mean (0.672).
  • pytest mctx/_src/tests (run from mctx/, JAX 0.12.0, CPU): 26 passed, including the four golden trees in tree_test.py. 24 pass on main and the two new tests fail there.
  • The issue's script prints [0] [32. 32.] [[0.64843744 0.5]] with this change and [1] [32. 32.] [[0.25 0.5]] on main.

@google-cla

google-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

At the depth cutoff, simulate() can return an edge whose child already
exists, and expand() evaluates it again. With a stochastic recurrent_fn
each evaluation is a new sample, but expand() overwrote the edge's reward
and discount and the child's value with it, so Tree.qvalues() reported
the last sample while the visit count kept growing. A two-armed bandit
searched with max_depth=1 could therefore select the worse arm.

backward() still uses the new sample, so ancestor values are unchanged.
After it, the edge's reward, discount and child value become running
means of all samples.

Fixes google-deepmind#135
@salma2vec
salma2vec force-pushed the fix/average-depth-limited-leaf-samples branch from c780a1e to 3cac043 Compare October 22, 2026 21:18
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.

Depth-limited search uses the last stochastic sample, not the sample mean, as an edge's Q-value

1 participant