Skip to content

Fix #1029: Thread random_state to BaseRLearner bootstraps - #1032

Merged
jeongyoonlee merged 1 commit into
uber:masterfrom
su-jin1425:fix-issue-1029
Aug 20, 2026
Merged

Fix #1029: Thread random_state to BaseRLearner bootstraps#1032
jeongyoonlee merged 1 commit into
uber:masterfrom
su-jin1425:fix-issue-1029

Conversation

@su-jin1425

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes #1029.

BaseRLearner bootstrap confidence intervals could depend on unrelated global NumPy RNG state even when random_state was provided.

This change:

  • Creates a local NumPy RNG from BaseRLearner.random_state.
  • Passes the RNG to the bootstrap sampling in fit_predict() and estimate_ate().
  • Adds a regression test covering both public bootstrap paths and verifying reproducibility after unrelated global NumPy RNG consumption.

The metric functions are unchanged because they already use their provided random_state for bootstrap sampling.

Before & After

1029

Test

$S="$env:TEMP\causalml-1029-review\test_1029.py";$B="$env:TEMP\causalml-1029-review\before";$A="$env:TEMP\causalml-1029-review\after";@'
import sys
import numpy as np
from sklearn.linear_model import LinearRegression
sys.path.insert(0, sys.argv[1])
from causalml.inference.meta.rlearner import BaseRRegressor

rng=np.random.RandomState(123)
X=rng.randn(160,4)
t=rng.binomial(1,.5,160)
y=1.5*X[:,0]-.8*X[:,1]+2*t+rng.randn(160)*.5

def run(noise):
    np.random.seed(2026)
    np.random.rand(noise)
    m=BaseRRegressor(learner=LinearRegression(),n_fold=3,random_state=42)
    _,lo,hi=m.fit_predict(X,y=y,treatment=t,return_ci=True,n_bootstraps=20,bootstrap_size=120,verbose=False)
    return lo,hi

a=run(0)
b=run(1000)
print("same lower:",np.array_equal(a[0],b[0]))
print("same upper:",np.array_equal(a[1],b[1]))
print("RESULT:","PASS" if np.array_equal(a[0],b[0]) and np.array_equal(a[1],b[1]) else "FAIL")
'@|Set-Content $S -Encoding UTF8;"BEFORE 35f6e734";python $S $B;"AFTER 52fd2734";python $S $A

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

The regression test verifies that identical seeded BaseRRegressor runs produce identical confidence intervals regardless of unrelated global NumPy RNG consumption.

Copilot AI balanced review requested due to automatic review settings August 19, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@su-jin1425

Copy link
Copy Markdown
Contributor Author

Use #1030#1033#1032#1031 this order to avoid merge conflicts

@jeongyoonlee jeongyoonlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.random.default_rng(self.random_state) (rlearner.py:315, :420) rejects a RandomState instance, which rlearner.py:62 documents as supported:

BaseRRegressor(learner=LinearRegression(), random_state=np.random.RandomState(42)) \
    .fit_predict(X, y=y, treatment=t, return_ci=True)
# TypeError: SeedSequence expects int or sequence of ints for entropy not RandomState(MT19937)

Same for estimate_ate(..., bootstrap_ci=True). Both work on master, so this is a regression.

Please use sklearn.utils.check_random_state(self.random_state) instead — it normalizes None/int/RandomState to a RandomState, which has .choice(), so the rest of each hunk is unchanged. That is also what the tree side already does (match.py:143, causalforest.py:482, upliftforest.py:66), and it matches sklearn's guidance: Controlling randomness recommends passing RandomState instances to estimators, and sklearn does not accept numpy Generator at all.

The new test only passes an int, so the instance path stays uncovered — please add it.

@su-jin1425
su-jin1425 force-pushed the fix-issue-1029 branch 5 times, most recently from a5bed84 to b55e864 Compare August 20, 2026 02:21
@su-jin1425

Copy link
Copy Markdown
Contributor Author

Done.
CI / CD failed so i worked on it.

@jeongyoonlee jeongyoonlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@jeongyoonlee
jeongyoonlee merged commit b85413a into uber:master Aug 20, 2026
15 checks passed
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.

Metric bootstraps and R-learner cross-validation draw from global NumPy state

3 participants