pymc-labs / CausalPy

A Python package for causal inference in quasi-experimental settings
https://causalpy.readthedocs.io
Apache License 2.0
829 stars 52 forks source link

Fix failing test `test_inverse_prop` #336

Closed drbenvincent closed 1 month ago

drbenvincent commented 1 month ago

Slightly strange because all tests passed for #311. But we have a failing remote test for https://github.com/pymc-labs/CausalPy/actions/runs/8974649334/job/24647471387

On main I re-built the environment from scratch following instructions in CONTRIBUTING.md and can reproduce this bug locally:

FAILED causalpy/tests/test_pymc_experiments.py::test_inverse_prop - ValueError: autodetected range of [nan, nan] is not finite

This was fixed by adding in the rand_seed code to the PropensityScore.fit method which overrides the ModelBuilder.fit method. So the method should now be:

def fit(self, X, t, coords):
    """Draw samples from posterior, prior predictive, and posterior predictive
    distributions. We overwrite the base method because the base method assumes
    a variable y and we use t to indicate the treatment variable here.
    """

    # Ensure random_seed is used in sample_prior_predictive() and
    # sample_posterior_predictive() if provided in sample_kwargs.
    random_seed = self.sample_kwargs.get("random_seed", None)

    self.build_model(X, t, coords)
    with self:
        self.idata = pm.sample(**self.sample_kwargs)
        self.idata.extend(pm.sample_prior_predictive(random_seed=random_seed))
        self.idata.extend(
            pm.sample_posterior_predictive(
                self.idata, progressbar=False, random_seed=random_seed
            )
        )
    return self.idata

Though it's worth double checking the details of the test and error message which I didn't do šŸ¤·šŸ»ā€ā™‚ļøšŸ˜œ

drbenvincent commented 1 month ago

closed by #337