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

Update Loo, implement improved algorithm #2730

Merged
merged 7 commits into from
Nov 24, 2017
Merged
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions pymc3/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -249,7 +249,8 @@ def loo(trace, model=None, pointwise=False, reff=1., progressbar=False):
Default False
reff : float
Copy link
Member

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?

Copy link
Member Author

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.

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
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont work with multivariate RVs. I suggest
pm.stats.dict2pd(eff,'eff').mean()

Copy link
Member Author

Choose a reason for hiding this comment

The 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.')
Expand Down