-
-
Notifications
You must be signed in to change notification settings - Fork 9
Add order statistic diagnostic for compare()
#237
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
Open
jordandeklerk
wants to merge
2
commits into
main
Choose a base branch
from
stat-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−6
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import pandas as pd | ||
| from arviz_base import rcParams | ||
| from scipy.optimize import Bounds, LinearConstraint, minimize | ||
| from scipy.stats import dirichlet | ||
| from scipy.stats import dirichlet, norm | ||
|
|
||
| from arviz_stats.loo import loo | ||
| from arviz_stats.loo.helper_loo import _diff_srs_estimator | ||
|
|
@@ -23,8 +23,20 @@ def compare( | |
|
|
||
| The ELPD is estimated by Pareto smoothed importance sampling leave-one-out | ||
| cross-validation, the same method used by :func:`arviz_stats.loo`. | ||
| The method is described in [1]_ and [2]_. | ||
| By default, the weights are estimated using ``"stacking"`` as described in [3]_. | ||
| The method is described in [2]_ and [3]_. | ||
| By default, the weights are estimated using ``"stacking"`` as described in [4]_. | ||
|
|
||
| When more than 11 models are compared, an order statistics-based diagnostic | ||
| is performed to check whether differences in predictive performance are | ||
| potentially due to chance, as described by [1]_. A warning is issued if | ||
| selection-induced bias is detected. If this is the case, we recommend avoiding | ||
| model selection based on LOO-CV and instead favor model averaging/stacking | ||
| or projection predictive inference. | ||
|
|
||
| See the EABM chapters on `Model Comparison <https://arviz-devs.github.io/EABM/Chapters/Model_comparison.html>`_, | ||
| `Model Comparison (Case Study) <https://arviz-devs.github.io/EABM/Chapters/Case_study_model_comparison.html>`_, | ||
| and `Model Comparison for Large Data <https://arviz-devs.github.io/EABM/Chapters/Model_comparison_large_data.html>`_ | ||
| for more details. | ||
jordandeklerk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
@@ -117,15 +129,20 @@ def compare( | |
| References | ||
| ---------- | ||
|
|
||
| .. [1] Vehtari et al. *Practical Bayesian model evaluation using leave-one-out cross-validation | ||
| .. [1] McLatchie, Y., Vehtari, A. *Efficient estimation and correction of selection-induced | ||
| bias with order statistics*. Statistics and Computing, 34, 132 (2024). | ||
| https://doi.org/10.1007/s11222-024-10442-4 | ||
| arXiv preprint https://arxiv.org/abs/2309.03742 | ||
|
|
||
| .. [2] Vehtari et al. *Practical Bayesian model evaluation using leave-one-out cross-validation | ||
| and WAIC*. Statistics and Computing. 27(5) (2017) https://doi.org/10.1007/s11222-016-9696-4 | ||
| arXiv preprint https://arxiv.org/abs/1507.04544. | ||
|
|
||
| .. [2] Vehtari et al. *Pareto Smoothed Importance Sampling*. | ||
| .. [3] Vehtari et al. *Pareto Smoothed Importance Sampling*. | ||
| Journal of Machine Learning Research, 25(72) (2024) https://jmlr.org/papers/v25/19-556.html | ||
| arXiv preprint https://arxiv.org/abs/1507.02646 | ||
|
|
||
| .. [3] Yao et al. *Using stacking to average Bayesian predictive distributions* | ||
| .. [4] Yao et al. *Using stacking to average Bayesian predictive distributions* | ||
| Bayesian Analysis, 13, 3 (2018). https://doi.org/10.1214/17-BA1091 | ||
| arXiv preprint https://arxiv.org/abs/1704.02030. | ||
| """ | ||
|
|
@@ -270,6 +287,9 @@ def gradient(weights): | |
|
|
||
| df_comp["rank"] = df_comp["rank"].astype(int) | ||
| df_comp["warning"] = df_comp["warning"].astype(bool) | ||
|
|
||
| model_order = list(ics.index) | ||
| _order_stat_check(ics_dict, model_order, has_subsampling) | ||
| return df_comp.sort_values(by="elpd", ascending=False) | ||
|
|
||
|
|
||
|
|
@@ -529,3 +549,52 @@ def _calculate_ics( | |
| f"Encountered error trying to compute ELPD from model {name}." | ||
| ) from e | ||
| return new_compare_dict | ||
|
|
||
|
|
||
| def _order_stat_check(ics_dict, model_order, has_subsampling): | ||
| """Perform order statistics-based checks on models.""" | ||
| if has_subsampling or len(ics_dict) <= 11: | ||
| return | ||
|
|
||
| # Use the median model as the baseline model to compute ELPD differences | ||
| baseline_idx = len(model_order) // 2 | ||
| baseline_model = model_order[baseline_idx] | ||
| baseline_elpd = ics_dict[baseline_model] | ||
|
|
||
| elpd_diffs = [] | ||
| for model_name in model_order: | ||
| if model_name == baseline_model: | ||
| elpd_diffs.append(0.0) | ||
| else: | ||
| elpd_a_vals = np.asarray(baseline_elpd.elpd_i).flatten() | ||
| elpd_b_vals = np.asarray(ics_dict[model_name].elpd_i).flatten() | ||
| elpd_diffs.append(np.sum(elpd_b_vals - elpd_a_vals)) | ||
|
|
||
| elpd_diffs = np.array(elpd_diffs) | ||
| diff_median = np.median(elpd_diffs) | ||
| elpd_diff_trunc = elpd_diffs[elpd_diffs >= diff_median] | ||
| n_models = np.sum(~np.isnan(elpd_diff_trunc)) | ||
|
|
||
| if n_models < 1: | ||
| return | ||
|
|
||
| candidate_sd = np.sqrt(1 / n_models * np.sum(elpd_diff_trunc**2)) | ||
|
|
||
| # Defensive check to avoid a runtime error when computing the order statistic | ||
| if candidate_sd == 0 or not np.isfinite(candidate_sd): | ||
| warnings.warn( | ||
| "All models have nearly identical performance.", | ||
| UserWarning, | ||
| ) | ||
| return | ||
|
Comment on lines
+581
to
+586
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. Not sure if we want to add more to this warning? This is mostly to avoid the Scipy runtime error that will happen if |
||
|
|
||
| # Estimate expected best diff under null hypothesis | ||
| k = len(ics_dict) - 1 | ||
| order_stat = norm.ppf(1 - 1 / (k * 2), loc=0, scale=candidate_sd) | ||
|
|
||
| if np.nanmax(elpd_diffs) <= order_stat: | ||
| warnings.warn( | ||
| "Difference in performance potentially due to chance. " | ||
| "See https://doi.org/10.1007/s11222-024-10442-4 for details.", | ||
| UserWarning, | ||
| ) | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.