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

Added plot_bf() function for bayes_factor in arviz-plots #158

Merged
merged 1 commit into from
Mar 9, 2025

Conversation

PiyushPanwarFST
Copy link
Contributor

@PiyushPanwarFST PiyushPanwarFST commented Mar 1, 2025

Implemented the plot_bf() function in arviz-plots.
Added a test case to validate the functionality of plot_bf().
Included plot_bf() in the documentation.
Incorporated feedback received so far.
Resolved all the linter(pylint) errors.
Screenshot 2025-03-01 at 2 31 11 PM


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

@PiyushPanwarFST
Copy link
Contributor Author

PiyushPanwarFST commented Mar 3, 2025

I have created this PR incorporating all suggested changes. Let me know if any changes are needed. Thank you

pc_kwargs=pc_kwargs,
)

if ref_val == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

ref_val could take any real value.

pc_kwargs=pc_kwargs,
)

if ref_val == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ref_val == 0:
if ref_val is not False:

Comment on lines 208 to 209
x=ref_val + 13,
y=max(ref_vals["prior"], ref_vals["posterior"]) * 1.7,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure these magic numbers will generally work. See for instance https://arviz-plots--158.org.readthedocs.build/en/158/gallery/plot_bf.html for plotly and bokeh backends

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, As the annotation was coming in between the line while plotting. I added this value to adjust the position of the annotation.
Can you suggest if any better way exists?

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that, but as you can see, this solution does not work for the example.
Other solution could be:

  • Find a way to position the text using a relative frame-reference
  • plot it at max(value_at_posterior, value_at_prior), but just one number (the BF10 or BF01), this is the same as before but because we have just one number it could be less crowed. Probably this is the best not only in this case but in general
  • Use the legend
  • Use the title.

@codecov-commenter
Copy link

codecov-commenter commented Mar 3, 2025

Codecov Report

Attention: Patch coverage is 88.88889% with 9 lines in your changes missing coverage. Please review.

Project coverage is 76.28%. Comparing base (8e4294e) to head (4a3ed61).
Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
src/arviz_plots/plots/bfplot.py 88.00% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #158      +/-   ##
==========================================
+ Coverage   75.93%   76.28%   +0.35%     
==========================================
  Files          30       32       +2     
  Lines        3611     3741     +130     
==========================================
+ Hits         2742     2854     +112     
- Misses        869      887      +18     

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@PiyushPanwarFST
Copy link
Contributor Author

Screenshot 2025-03-07 at 6 02 50 PM

Heyy @aloctavodia, I have added a new func called add_text_legend in plot_collection.py to add the annotation values of bayes_factor. This function will help us to add text-based legends for other plots also. I think there might be better way exist but this one I found satisfactory. Let me know your thoughts, always ready to improve.

@aloctavodia
Copy link
Contributor

aloctavodia commented Mar 8, 2025

Most of the code is duplicated from add legend. A better approach may be to add an argument to the existing function instead of duplicating it. Also better to reduce the amount of text.

A simpler approach will be to use anotate text, and just reduce the amount of information, adding the value of one of the bayes factors should be enough if the docs explain wich value is the one plotted

@PiyushPanwarFST
Copy link
Contributor Author

Screenshot 2025-02-26 at 12 13 05 PM

The main issue with the annotated text approach is that the BF info appears in the middle of the plot. Since the legend approach works only with Matplotlib, I can:

  1. Use an if statement to apply annotation only when using Matplotlib, avoiding issues in Bokeh or Plotly.
  2. Otherwise, I can go ahead with adding another argument to the existing legend function.

@PiyushPanwarFST
Copy link
Contributor Author

Screenshot 2025-03-08 at 7 25 15 PM

Hey, as per your suggestion, I have added a new argument to the existing add_legend() function to adjust the Bayes factor values. Let me know if any further modifications are needed.

@@ -0,0 +1,23 @@
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Bayes factors are not a method for inference diagnostics. They are a tool for model comparison.

Copy link
Contributor

@aloctavodia aloctavodia Mar 9, 2025

Choose a reason for hiding this comment

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

Please fix this. You need to move this file to a different folder

@@ -0,0 +1,210 @@
"""Contain functions for Bayesian Factor plotting."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""Contain functions for Bayesian Factor plotting."""
"""Contain functions for Bayes Factor plotting."""

@PiyushPanwarFST PiyushPanwarFST force-pushed the feature branch 2 times, most recently from ce55030 to 8c3ccb7 Compare March 9, 2025 11:16
Implemented the plot_bf() function in arviz-plots.
Added test cases to validate the functionality of plot_bf().
Introduced a new text_only argument in the add_legend() function.
Included plot_bf() in the documentation.
Incorporated feedback recieve so far.
Resolved all the linter(pylint) errors.

Signed-off-by: PiyushPanwarFST <[email protected]>
@aloctavodia aloctavodia merged commit 5a7df08 into arviz-devs:main Mar 9, 2025
3 checks passed
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.

3 participants