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

Implement compute_log_likelihood method #769

Merged
merged 6 commits into from
Apr 1, 2024

Conversation

tomicapretto
Copy link
Collaborator

This PR adds a new method called compute_log_likelihood which is mostly useful to get the log likelihood for out of sample observations. It's still useful to compute the log likelihood for in sample observations, but they were already available with model.fit(..., idata_kwargs = {"log_likelihood": True}).

Basic example

import bambi as bmb
import numpy as np

df = bmb.load_data("my_data")
model = bmb.Model("y ~ x", df)
idata = model.fit(idata_kwargs={"log_likelihood": True})

idata2 = model.compute_log_likelihood(idata, inplace=False)
idata3 = model.compute_log_likelihood(idata, data=df, inplace=False)

lp1 = idata.log_likelihood["y"].to_numpy()
lp2 = idata2.log_likelihood["y"].to_numpy()
lp3 = idata3.log_likelihood["y"].to_numpy()
print(np.allclose(lp1, lp2)) # True
print(np.allclose(lp1, lp3)) # True

Context

This PR closes #766, where this was mentioned and briefly discussed.

Caveat

Notice this is a draft and I don't expect the implementation to work in all cases. Also, the implementation itself could be polished as there's a lot of repetitive code now since the method for compute_log_likelihood is extremely close to the posterior_predictive.

@tomicapretto tomicapretto changed the title Implement draf of compute_log_likelihood function Implement compute_log_likelihood method Jan 5, 2024
@tomicapretto tomicapretto marked this pull request as ready for review April 1, 2024 13:25
@codecov-commenter
Copy link

codecov-commenter commented Apr 1, 2024

Codecov Report

Attention: Patch coverage is 92.15686% with 8 lines in your changes are missing coverage. Please review.

Project coverage is 90.20%. Comparing base (714ccb7) to head (067170f).

❗ Current head 067170f differs from pull request most recent head dc71525. Consider uploading reports for the commit dc71525 to get more accurate results

Files Patch % Lines
bambi/families/family.py 80.95% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #769      +/-   ##
==========================================
+ Coverage   90.14%   90.20%   +0.05%     
==========================================
  Files          46       46              
  Lines        3836     3900      +64     
==========================================
+ Hits         3458     3518      +60     
- Misses        378      382       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tomicapretto tomicapretto merged commit ed1e936 into bambinos:main Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Computing likelihoods
2 participants