Skip to content

Add ZeroSumNormal example notebook#844

Open
derwells wants to merge 1 commit intopymc-devs:mainfrom
derwells:zerosumnormal-notebook
Open

Add ZeroSumNormal example notebook#844
derwells wants to merge 1 commit intopymc-devs:mainfrom
derwells:zerosumnormal-notebook

Conversation

@derwells
Copy link

Summary

This PR adds a comprehensive example notebook demonstrating PyMC's ZeroSumNormal distribution, which is ideal for categorical regression with sum-to-zero constraints.

What this notebook covers:

  • Identifiability Problems: Why sum-to-zero constraints are needed when modeling categorical predictors
  • Design Matrix Analysis: How design matrices create linear dependence with one-hot encoding
  • Contrast Coding Schemes: Comparison of reference coding vs sum-to-zero coding with trade-offs
  • ZeroSumNormal Mathematics: The covariance structure and properties of the ZeroSumNormal distribution
  • Practical Example: Complete worked example comparing treatment group means with posterior analysis
  • Advanced Usage: Multi-dimensional sum-to-zero constraints for factorial designs

Background

This continues the work started in #210 by @drbenvincent and @aseyboldt. The original PR from 2021 used PyMC3 with a custom ZeroSumNormal implementation. Since PyMC v4.2.2 (Oct 2022), pm.ZeroSumNormal is available natively, making this much simpler.

I've completely rewritten the notebook for modern PyMC v5, focusing on:

  1. Clear pedagogical progression from problem → solution
  2. Mathematical foundations with worked examples
  3. Visual comparisons between approaches
  4. Following all pymc-examples style conventions

Checklist

  • Notebook runs sequentially with fresh kernel
  • Uses RANDOM_SEED for reproducibility
  • ArviZ dark grid style
  • Retina figures
  • MyST frontmatter with tags, categories, author
  • Watermark cell
  • Page footer include
  • References original PR WIP ZeroSumNormal example notebook #210

Target column in tracker

Done - This is a complete, new notebook using PyMC v5 best practices.

Closes #210

🤖 Generated with Claude Code

This notebook demonstrates the ZeroSumNormal distribution, which was
introduced in PyMC v4.2.2. It covers:

- Why sum-to-zero constraints are needed for identifiability
- How design matrices create linear dependence with categorical predictors
- Contrast coding schemes (reference vs sum-to-zero)
- Mathematical foundations of ZeroSumNormal
- Practical example comparing group means
- Advanced usage with multiple zero-sum axes

Based on the original concept from PR pymc-devs#210 by Benjamin T. Vincent and
Adrian Seyboldt, rewritten for PyMC v5.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

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