Skip to content

Implement summary method for InstrumentalVariable#851

Open
anevolbap wants to merge 6 commits into
pymc-labs:mainfrom
anevolbap:issue_360-iv_summary_method
Open

Implement summary method for InstrumentalVariable#851
anevolbap wants to merge 6 commits into
pymc-labs:mainfrom
anevolbap:issue_360-iv_summary_method

Conversation

@anevolbap
Copy link
Copy Markdown
Contributor

@anevolbap anevolbap commented Apr 15, 2026

Summary

Implement the summary method for InstrumentalVariable, replacing the NotImplementedError stub.

Fixes #360

Approach

Issue #360 floated overriding the base PyMCModel.print_coefficients method. This PR overrides summary directly instead, because IV has five distinct coefficient blocks that don't fit the single-stage shape of print_coefficients:

  1. Naive OLS coefficients
  2. 2SLS first-stage coefficients
  3. 2SLS second-stage coefficients
  4. Bayesian instrument-stage coefficients (beta_t over instruments)
  5. Bayesian outcome-stage coefficients (beta_z over covariates)

The base print_coefficients assumes one beta variable. Reusing it would require either splitting the call into multiple shapes or branching inside the base, both of which leak IV-specific structure into the parent class. Inlining the formatting in InstrumentalVariable.summary keeps the base unchanged.

The Bayesian section uses the same HDI label and quantile computation pattern as the base print_coefficients (in causalpy/pymc_models.py). Whether to switch the whole codebase to az.hdi is out of scope for this PR.

Changes

  • InstrumentalVariable.summary() prints OLS, 2SLS, and Bayesian coefficient blocks (instrument and outcome stages with HDI-style intervals).
  • Removed "summary" from test_iv_not_implemented_methods parametrize list.
  • Added result.summary() call to test_iv_reg.

Testing

@github-actions
Copy link
Copy Markdown
Contributor

👋 Welcome to CausalPy, @anevolbap!

Thank you for opening your first pull request! We're excited to have you contribute to the project. 🎉

Here are a few tips to help your PR get merged smoothly:

  • ✅ Make sure all CI checks pass (tests, linting, type checking)
  • 📝 Run prek run --all-files locally before pushing
  • 📖 Check our Contributing Guide for more details

A maintainer will review your changes soon. Thanks for helping make CausalPy better! 🚀


💼 LinkedIn Shoutout: Once your PR is merged, we'd love to give you a shoutout on LinkedIn to thank you for your contribution! If you're interested, just drop your LinkedIn profile URL in a comment below.

@github-actions github-actions Bot added enhancement New feature or request outputs Quantitative outputs of the model labels Apr 15, 2026
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 15, 2026

Documentation build overview

📚 causalpy | 🛠️ Build #32715548 | 📁 Comparing 080f31d against latest (1cf3387)

  🔍 Preview build  

242 files changed · + 57 added · ± 184 modified · - 1 deleted

+ Added

± Modified

- Deleted

@anevolbap anevolbap marked this pull request as draft April 16, 2026 10:59
@drbenvincent
Copy link
Copy Markdown
Collaborator

This PR's failing test (3.11) / test (3.14) jobs are caused by test_panel_regression_plot_unit_effects (TypeError: only 0-dimensional arrays can be converted to Python scalars), which was fixed in #853 (now merged to main). Updating this branch from main should turn CI green — no code changes needed here.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.60%. Comparing base (deb8774) to head (63f5f26).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #851      +/-   ##
==========================================
- Coverage   94.60%   94.60%   -0.01%     
==========================================
  Files          80       80              
  Lines       12764    12791      +27     
  Branches      770      775       +5     
==========================================
+ Hits        12076    12101      +25     
- Misses        485      486       +1     
- Partials      203      204       +1     

☔ 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.

@anevolbap anevolbap marked this pull request as ready for review April 25, 2026 20:15
@anevolbap
Copy link
Copy Markdown
Contributor Author

anevolbap commented Apr 25, 2026

@drbenvincent this is ready for review. CI fully green after merging main. Just pushed one fix (63f5f26a) so round_to=0 is no longer overridden to 2.

Two things to call out for review:

  1. Override target: in Implement summary method for cp.pymc_experiments.InstrumentalVariable #360 you suggested overriding print_coefficients. I went with overriding summary directly because IV has five coefficient blocks (OLS, 2SLS first stage, 2SLS second stage, Bayesian instrument stage, Bayesian outcome stage), and the base print_coefficients assumes a single beta variable. The PR description has the full reasoning. Happy to change to a custom print_coefficients if you prefer.

  2. HDI labeling: the Bayesian block uses the same X% HDI [lo, hi] label with equal-tailed quantiles that PyMCModel.print_coefficients uses. I matched the existing convention rather than diverging. If we want to switch the whole codebase to az.hdi, I can open a follow-up issue.

Thanks!

@anevolbap anevolbap marked this pull request as draft April 25, 2026 20:26
@anevolbap anevolbap marked this pull request as ready for review April 26, 2026 11:49
Copy link
Copy Markdown
Collaborator

@NathanielF NathanielF left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request outputs Quantitative outputs of the model

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement summary method for cp.pymc_experiments.InstrumentalVariable

3 participants