Skip to content

[Repo Assist] feat(gcm): add random_seed to interventional_samples, counterfactual_samples, average_causal_effect - #1731

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/fix-gcm-whatif-random-seed-20260802-4ac919e7ae1930f5
Draft

[Repo Assist] feat(gcm): add random_seed to interventional_samples, counterfactual_samples, average_causal_effect#1731
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/fix-gcm-whatif-random-seed-20260802-4ac919e7ae1930f5

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Problem

interventional_samples(), counterfactual_samples(), and average_causal_effect() in dowhy/gcm/whatif.py have no random_seed parameter, making reproducible results across independent Python sessions unnecessarily fragile. Users must currently call set_random_seed() globally before calling these functions — which is order-sensitive, easy to miss, and inconsistent with other GCM functions.

Reported in #1307 (multiple users experiencing non-reproducible results across sessions). A previous fix PR #1489 was closed as stale in July 2026 before being merged.

Change

Add an optional random_seed: Optional[int] = None parameter to:

  • interventional_samples()
  • counterfactual_samples()
  • average_causal_effect()

When provided, set_random_seed(random_seed) is called at function entry, following the existing GCM convention used in falsify_graph, model_evaluation, etc.

# Before — fragile global seed required
from dowhy.gcm.util.general import set_random_seed
set_random_seed(42)
samples = gcm.interventional_samples(model, {"X": lambda x: 1}, num_samples_to_draw=500)

# After — explicit, self-contained
samples = gcm.interventional_samples(model, {"X": lambda x: 1}, num_samples_to_draw=500, random_seed=42)

Trade-offs

  • Backwards compatible: parameter is optional with default None (existing call sites unaffected).
  • Global side effect: set_random_seed affects numpy/torch global state, consistent with the rest of the GCM module's approach.

Test Status

Added 3 new unit tests in tests/gcm/test_whatif.py:

  • test_given_random_seed_when_draw_interventional_samples_then_results_are_reproducible — same seed → identical samples; different seeds → different samples.
  • test_given_random_seed_when_draw_counterfactual_samples_then_results_are_reproducible — same seed → identical counterfactual frames.
  • test_given_random_seed_when_estimate_average_causal_effect_then_results_are_reproducible — same seed → identical ACE; different seeds → different ACE.
19 passed in 9.04s

All 19 tests in tests/gcm/test_whatif.py pass. Format/lint: black and isort show no changes; 3 pre-existing E501 lint warnings in the file (unrelated to this PR).

Closes #1307

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@11c9a2c442e519ff2b427bf58679f5a525353f76

…samples, average_causal_effect

Add an optional random_seed: Optional[int] = None parameter to:
- interventional_samples()
- counterfactual_samples()
- average_causal_effect()

When provided, set_random_seed(random_seed) is called at function entry,
following the existing GCM convention used in falsify_graph,
confidence_intervals_bootstrap, model_evaluation, etc.

This makes results reproducible across independent calls without requiring
the caller to manage global state via set_random_seed().

Reported in #1307 (multiple users experiencing non-reproducible results).
Previous fix PR #1489 was closed as stale.

Closes #1307

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interventional samples gives inconsistent results.

0 participants