Skip to content

[Repo Assist] improve(gcm): add random_seed parameter to interventional_samples, counterfactual_samples, and average_causal_effect#1549

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-gcm-whatif-random-seed-20260527-4ee0cc006683176f
Draft

[Repo Assist] improve(gcm): add random_seed parameter to interventional_samples, counterfactual_samples, and average_causal_effect#1549
github-actions[bot] wants to merge 1 commit into
mainfrom
repo-assist/improve-gcm-whatif-random-seed-20260527-4ee0cc006683176f

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated PR from Repo Assist.

Problem

interventional_samples(), counterfactual_samples(), and average_causal_effect() in dowhy/gcm/whatif.py offered no way to control the random seed. Calls to the same function with the same inputs produced different results each run, making experiments non-reproducible. This was reported in issue #1307.

Solution

Added an optional random_seed: Optional[int] = None parameter to all three public functions. When a seed is provided, set_random_seed(random_seed) is called at the start of the function before any stochastic sampling takes place. The default None preserves the existing behaviour exactly.

# Before: non-reproducible
samples = gcm.interventional_samples(causal_model, {'X': lambda x: 1.0}, num_samples_to_draw=1000)

# After: reproducible with a seed
samples = gcm.interventional_samples(causal_model, {'X': lambda x: 1.0}, num_samples_to_draw=1000, random_seed=42)
```

The same pattern works for `counterfactual_samples` and `average_causal_effect`.

## Tests

Added two tests to `tests/gcm/test_whatif.py`:
- `test_interventional_samples_with_random_seed_is_reproducible`
- `test_counterfactual_samples_with_random_seed_is_reproducible`

Both confirm that the same seed produces bit-identical results across two independent calls.

## Test Status

```
tests/gcm/test_whatif.py: 17 passed (all existing + 2 new)

No new lint errors introduced (3 E501 violations are pre-existing in test_whatif.py).

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

…unterfactual_samples, and average_causal_effect

Addresses the reproducibility problem reported in #1307: calls to
interventional_samples() and counterfactual_samples() returned
different results each time because the stochastic mechanisms sample
from the global numpy random state.

Adding random_seed: Optional[int] = None allows callers to seed the
global numpy/random state before sampling, making results reproducible
for a given seed. When random_seed is None (default), behaviour is
unchanged.

Two regression tests added to tests/gcm/test_whatif.py confirming
that passing the same seed produces bit-identical results.

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