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

clean laplace results #563

Merged
merged 5 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion bambi/backend/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def identity(x):


def inverse_squared(x):
return at.inv(at.sqrt(x))
return at.reciprocal(at.sqrt(x))


def arctan_2(x):
Expand Down
14 changes: 8 additions & 6 deletions bambi/backend/pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PyMCModel:
"cloglog": cloglog,
"identity": identity,
"inverse_squared": inverse_squared,
"inverse": at.inv,
"inverse": at.reciprocal,
"log": at.exp,
"logit": logit,
"probit": probit,
Expand Down Expand Up @@ -108,7 +108,7 @@ def run(
elif inference_method == "vi":
result = self._run_vi(**kwargs)
elif inference_method == "laplace":
result = self._run_laplace(draws)
result = self._run_laplace(draws, omit_offsets, include_mean)
else:
raise NotImplementedError(f"{inference_method} method has not been implemented")

Expand Down Expand Up @@ -349,10 +349,10 @@ def _run_mcmc(
f"``mcmc``, ``nuts_numpyro`` or ``nuts_blackjax``"
)

idata = self._clean_mcmc_results(idata, omit_offsets, include_mean)
idata = self._clean_results(idata, omit_offsets, include_mean)
return idata

def _clean_mcmc_results(self, idata, omit_offsets, include_mean):
def _clean_results(self, idata, omit_offsets, include_mean):
for group in idata.groups():
getattr(idata, group).attrs["modeling_interface"] = "bambi"
getattr(idata, group).attrs["modeling_interface_version"] = version.__version__
Expand Down Expand Up @@ -438,7 +438,7 @@ def _run_vi(self, **kwargs):
self.vi_approx = pm.fit(**kwargs)
return self.vi_approx

def _run_laplace(self, draws):
def _run_laplace(self, draws, omit_offsets, include_mean):
"""Fit a model using a Laplace approximation.

Mainly for pedagogical use, provides reasonable results for approximately
Expand Down Expand Up @@ -473,7 +473,9 @@ def _run_laplace(self, draws):

samples = np.random.multivariate_normal(modes, cov, size=draws)

return _posterior_samples_to_idata(samples, self.model)
idata = _posterior_samples_to_idata(samples, self.model)
idata = self._clean_results(idata, omit_offsets, include_mean)
return idata


def _posterior_samples_to_idata(samples, model):
Expand Down