Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix random seed handling for "vi" #869

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bambi/backend/pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def run(
**kwargs,
)
elif inference_method in self.pymc_methods["vi"]:
result = self._run_vi(**kwargs)
result = self._run_vi(random_seed, **kwargs)
elif inference_method == "laplace":
result = self._run_laplace(draws, omit_offsets, include_response_params)
else:
Expand Down Expand Up @@ -382,9 +382,9 @@ def _clean_results(self, idata, omit_offsets, include_response_params, idata_fro

return idata

def _run_vi(self, **kwargs):
def _run_vi(self, random_seed, **kwargs):
with self.model:
self.vi_approx = pm.fit(**kwargs)
self.vi_approx = pm.fit(random_seed=random_seed, **kwargs)
return self.vi_approx

def _run_laplace(self, draws, omit_offsets, include_response_params):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,16 +824,16 @@ def test_beta_regression(self, gasoline_data):
model.predict(idata, kind="response")

assert (0 < idata.posterior["mu"]).all() & (idata.posterior["mu"] < 1).all()
assert (0 < idata.posterior_predictive["yield"]).all() & (
idata.posterior_predictive["yield"] < 1
assert (0 <= idata.posterior_predictive["yield"]).all() & (
idata.posterior_predictive["yield"] <= 1
).all()

model.predict(idata, kind="response_params", data=gasoline_data.iloc[:20, :])
model.predict(idata, kind="response", data=gasoline_data.iloc[:20, :])

assert (0 < idata.posterior["mu"]).all() & (idata.posterior["mu"] < 1).all()
assert (0 < idata.posterior_predictive["yield"]).all() & (
idata.posterior_predictive["yield"] < 1
assert (0 <= idata.posterior_predictive["yield"]).all() & (
idata.posterior_predictive["yield"] <= 1
).all()


Expand Down
Loading