-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Update Loo, implement improved algorithm #2730
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56cbbc1
update loo
aloctavodia 2657266
small fixes
aloctavodia c746feb
remove print
aloctavodia 615420b
remove unused import
aloctavodia 091bf45
add to release-notes
aloctavodia a3c18b1
automatic reff calculation
aloctavodia 24d226b
fix eff_ave
aloctavodia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,7 +234,7 @@ def waic(trace, model=None, pointwise=False, progressbar=False): | |
return WAIC_r(waic, waic_se, p_waic) | ||
|
||
|
||
def loo(trace, model=None, pointwise=False, reff=1., progressbar=False): | ||
def loo(trace, model=None, pointwise=False, reff=None, progressbar=False): | ||
"""Calculates leave-one-out (LOO) cross-validation for out of sample | ||
predictive model fit, following Vehtari et al. (2015). Cross-validation is | ||
computed using Pareto-smoothed importance sampling (PSIS). | ||
|
@@ -249,7 +249,8 @@ def loo(trace, model=None, pointwise=False, reff=1., progressbar=False): | |
Default False | ||
reff : float | ||
relative MCMC efficiency, `effective_n / n` i.e. number of effective | ||
samples divided by the number of actual samples | ||
samples divided by the number of actual samples. Computed from trace by | ||
default. | ||
progressbar: bool | ||
Whether or not to display a progress bar in the command line. The | ||
bar shows the percentage of completion, the evaluation speed, and | ||
|
@@ -265,6 +266,15 @@ def loo(trace, model=None, pointwise=False, reff=1., progressbar=False): | |
""" | ||
model = modelcontext(model) | ||
|
||
if reff is None: | ||
if trace.nchains == 1: | ||
reff = 1. | ||
else: | ||
eff = pm.effective_n(trace) | ||
eff_ave = sum(eff[v] for v in eff) / len(eff) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wont work with multivariate RVs. I suggest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
samples = len(trace) * trace.nchains | ||
reff = eff_ave / samples | ||
|
||
log_py = _log_post_trace(trace, model, progressbar=progressbar) | ||
if log_py.size == 0: | ||
raise ValueError('The model does not contain observed values.') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be computed within the function if not provided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can compute it by default.