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

Refactor ECDF code #2311

Merged
merged 36 commits into from
Feb 21, 2024
Merged

Refactor ECDF code #2311

merged 36 commits into from
Feb 21, 2024

Conversation

sethaxen
Copy link
Member

@sethaxen sethaxen commented Feb 10, 2024

Description

This PR is a major refactor of the ECDF plot code to remove dead code, reduce code duplication, use more descriptive variable/function names, and make it easier to make the changes detailed in #2309. It makes no changes to the functionality of plot_ecdf.

All utility functions have been moved to arviz.stats.ecdf_utils. It might be worth exporting ecdf_confidence_band, but this can be left for a future PR.

Example

This example shows that public functionality is unchanged.

import numpy as np
import scipy.stats

import matplotlib.pyplot as plt
import arviz as az

np.random.seed(0)

dist = scipy.stats.expon(1)
x = dist.rvs(100)

fig, axs = plt.subplots(1, 3, figsize=(15, 4))
az.plot_ecdf(x, ax=axs[0])
az.plot_ecdf(x, ax=axs[1], confidence_bands=True, cdf=dist.cdf)
az.plot_ecdf(x, ax=axs[2], confidence_bands=True, cdf=dist.cdf, pit=True, difference=True)

On main
ecdf_old

This PR
ecdf_new

Checklist

  • Follows official PR format
  • Includes a sample plot to visually illustrate the changes (only for plot-related functions)
  • New features are properly documented (with an example if appropriate)?
  • Includes new or updated tests to cover the new feature
  • Code style correct (follows pylint and black guidelines)
  • Changes are listed in changelog

📚 Documentation preview 📚: https://arviz--2311.org.readthedocs.build/en/2311/

Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

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

This is amazing, thanks!

After this it will probably be possible to use these functions in plot_ppc(kind="cumulative") and in plot_loo_pit instead of each having their own subset of ecdf functionality

arviz/stats/ecdf_utils.py Outdated Show resolved Hide resolved
arviz/stats/ecdf_utils.py Outdated Show resolved Hide resolved
arviz/stats/ecdf_utils.py Outdated Show resolved Hide resolved


@pytest.mark.parametrize(
"dist", [scipy.stats.norm(3, 10), scipy.stats.binom(10, 0.5)], ids=["continuous", "discrete"]
Copy link
Member

Choose a reason for hiding this comment

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

I didn't know the ids argument existed, I love it!

arviz/tests/base_tests/test_stats_ecdf_utils.py Outdated Show resolved Hide resolved
arviz/tests/base_tests/test_stats_ecdf_utils.py Outdated Show resolved Hide resolved
@sethaxen
Copy link
Member Author

After this it will probably be possible to use these functions in plot_ppc(kind="cumulative") and in plot_loo_pit instead of each having their own subset of ecdf functionality

Yeah! I also think it would be great if the plots where one could use kind to select the representation of some distribution (e.g. plot_dist) also supported ecdf (or cumulative?) and dot options.

I've made all requested changes.

Copy link
Member

@OriolAbril OriolAbril left a comment

Choose a reason for hiding this comment

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

left one last documentation related comment but looks great

arviz/plots/ecdfplot.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Feb 21, 2024

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (44db4c8) 86.73% compared to head (090af26) 86.79%.
Report is 3 commits behind head on main.

Files Patch % Lines
arviz/plots/ecdfplot.py 67.85% 9 Missing ⚠️
arviz/stats/ecdf_utils.py 96.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2311      +/-   ##
==========================================
+ Coverage   86.73%   86.79%   +0.05%     
==========================================
  Files         122      123       +1     
  Lines       12705    12730      +25     
==========================================
+ Hits        11020    11049      +29     
+ Misses       1685     1681       -4     

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

@OriolAbril OriolAbril merged commit 7c1637f into main Feb 21, 2024
9 of 11 checks passed
@OriolAbril OriolAbril deleted the ecdf_refactor branch February 21, 2024 21:58
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.

2 participants