Skip to content

Seed the LBA simulators from random_state - #457

Merged
AlexanderFengler merged 1 commit into
mainfrom
399-lba-random-state
Nov 29, 2026
Merged

AlexanderFengler merged 1 commit into
mainfrom
399-lba-random-state

Conversation

@AlexanderFengler

@AlexanderFengler AlexanderFengler commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Closes #399.

  • The four LBA kernels in src/cssm/lba_models.pyx drew from NumPy's global generator and had no random_state parameter, so the seed was dropped into **kwargs. The same seed gave different data on every call and in every process.
  • Each kernel now takes random_state and draws from np.random.default_rng(random_state).
  • Affected models: lba2, lba3, lba4, lba_3_vs_constraint, lba_angle_3, lba_angle_3_vs_constraint and the three dev_rlwm_lba_* models. A scan of all 124 registered models found no other model that is not reproducible for a fixed seed.
  • Behaviour changes: seeded output of these nine models changes, np.random.seed no longer controls it, and a negative random_state now raises ValueError, as the other kernels already do (ValueError or OverflowError).
  • New test tests/test_lba_random_state.py runs the nine models in two fresh interpreters: same seed gives the same data, different seeds give different data. It fails on main.
  • Run locally: uv run pytest tests/ (1770 passed, 154 skipped), uv run pytest tests/test_parallel_equivalence.py --run-statistical --no-cov (56 passed), uv run ruff check . && uv run ruff format --check ..
  • Not in this PR: src/cssm/sequential_models.pyx holds four unreferenced copies of these kernels that still use np.random.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added an optional random seed to several LBA simulators, allowing runs to produce repeatable results when given the same seed.

The four LBA kernels drew from NumPy's global generator and had no
random_state parameter, so the seed was dropped into **kwargs and the same
seed gave different data on every call and in every process.

Each kernel now takes random_state and draws from
np.random.default_rng(random_state). This changes the seeded output of lba2,
lba3, lba4, lba_3_vs_constraint, lba_angle_3, lba_angle_3_vs_constraint and
the three dev_rlwm_lba_* models; np.random.seed no longer controls it.

The new test runs these models in two fresh interpreters and compares the
output.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Four LBA simulators now accept random_state and use a NumPy generator for random draws. New tests check repeatability for the same seed and differing results for another seed, including across fresh Python processes.

Changes: Seeded LBA random draws

Layer / File(s) Summary
Add seeded random draws
src/cssm/lba_models.pyx
lba_vanilla, lba_angle, rlwm_lba_pw_v1, and rlwm_lba_race accept and document random_state. Each uses a generator initialized from that value for starting-point and drift-rate draws.
Test seeded outputs
tests/test_lba_random_state.py
Tests select configured LBA models and check that seed 1 produces matching draws within and across fresh processes, while seed 2 produces different draws.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • lnccbrown/ssm-simulators#436: PR #436 checks matching minimal and full outputs under the same seed and excludes LBA models that ignore random_state. Its LBA validation depends on the seeding behavior added here.

Suggested reviewers: cpaniaguam

Merge Risk: Minimal · up to 455a3

This change makes the LBA simulators reproducible when a seed is supplied. Seeded output differs from the previous output, which is intended. No merge-blocking risk was found in the supplied changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Seed the LBA simulators from random_state" matches the main change. The PR adds an optional random_state parameter to the LBA simulators in src/cssm/lba_models.pyx, and they draw from np.ra…
Linked Issues check Passed Issue #399 requires that LBA kernels draw from a generator seeded by random_state, not NumPy's global generator, and that tests cover same-seed reproducibility and different-seed divergence. The diff …
Out of Scope Changes check Passed All changes in src/cssm/lba_models.pyx serve the objective: adding random_state parameters with docstrings and switching draws to a seeded default_rng. The new test file tests/test_lba_random_state.py…
Full details: Title check

Explanation

The title "Seed the LBA simulators from random_state" matches the main change. The PR adds an optional random_state parameter to the LBA simulators in src/cssm/lba_models.pyx, and they draw from np.random.default_rng(random_state) instead of NumPy's global generator. The title is concise, specific, and avoids vague terms. It omits the test addition and the note that seeded output changes, but those are secondary details.

Full details: Linked Issues check

Explanation

Issue #399 requires that LBA kernels draw from a generator seeded by random_state, not NumPy's global generator, and that tests cover same-seed reproducibility and different-seed divergence. The diff to src/cssm/lba_models.pyx replaces every np.random.uniform and np.random.normal draw in the four LBA kernels (lba_vanilla, lba_angle, rlwm_lba_pw_v1, rlwm_lba_race) with calls on rng = np.random.default_rng(random_state). The four kernels match the four draw sites named in the issue (around lines 104, 214, 337, 460). Each kernel gains a random_state=None parameter. The new test tests/test_lba_random_state.py covers same-seed reproducibility within one process, across two fresh interpreters, and different-seed divergence, for every model whose simulator module is cssm.lba_models. Not verified: the mapping from the nine named model configs (for example lba_3_vs_constraint, lba_angle_3_vs_constraint, dev_rlwm_lba_*) to these kernels was not read from the model registry. The test asserts only that lba2, lba3, lba4 and lba_angle_3 are discovered. The PR reports a scan of all 124 registered models, which is not independently re-run here. The issue's acceptance criteria are otherwise met by the code and test.

Full details: Out of Scope Changes check

Explanation

All changes in src/cssm/lba_models.pyx serve the objective: adding random_state parameters with docstrings and switching draws to a seeded default_rng. The new test file tests/test_lba_random_state.py directly verifies the objective. The PR explicitly excludes src/cssm/sequential_models.pyx, and no changes to that file appear in the reviewed diff. No unrelated changes were found in the inspected hunks.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5d7e0d3e-5492-4a8d-9b75-b4b192f291bc
Commits

Reviewing files that changed from the base of the PR and between a02ee1d and 455a3c8.

Files selected for processing: 2

  • src/cssm/lba_models.pyx
  • tests/test_lba_random_state.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 95.08% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AlexanderFengler
AlexanderFengler merged commit c855783 into main Nov 29, 2026
16 checks passed
AlexanderFengler added a commit to lnccbrown/HSSM that referenced this pull request Nov 29, 2026
At the default 0.8, one of twelve sampler seeds diverged on the seed-5
dataset of the fixed simulator (lnccbrown/ssm-simulators#457). At 0.95 none
of twelve does, on ssm-simulators 0.17.0 and on the fix.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
AlexanderFengler added a commit that referenced this pull request Nov 29, 2026
Patch release carrying the LBA random_state fix (#457, closes #399).

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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.

LBA simulators ignore random_state (draw from NumPy's global RNG)

1 participant