Skip to content

Adds a state property that mean and std are guaranteed to be on.#8319

Open
zaxtax wants to merge 1 commit into
pymc-devs:mainfrom
zaxtax:expose_state_field_in_approx
Open

Adds a state property that mean and std are guaranteed to be on.#8319
zaxtax wants to merge 1 commit into
pymc-devs:mainfrom
zaxtax:expose_state_field_in_approx

Conversation

@zaxtax

@zaxtax zaxtax commented May 31, 2026

Copy link
Copy Markdown
Contributor

Description

Right now, approximations do come with a mean/std fields when fitting. But these are often in the unconstrained posterior space and not the original, which makes it hard to reason about when tracking fit.

This change makes it such that there is a state accessor which returns a dataclass with mean and std fields. These are in the original space, and by introducing a new accessor doesn't break existing code that might use the current fields.

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

@read-the-docs-community

read-the-docs-community Bot commented May 31, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented May 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.76%. Comparing base (c032457) to head (19dd42b).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8319   +/-   ##
=======================================
  Coverage   91.76%   91.76%           
=======================================
  Files         128      128           
  Lines       20653    20667   +14     
=======================================
+ Hits        18952    18966   +14     
  Misses       1701     1701           
Files with missing lines Coverage Δ
pymc/variational/opvi.py 87.42% <100.00%> (+0.27%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zaxtax zaxtax requested a review from fonnesbeck June 2, 2026 07:49
@zaxtax zaxtax force-pushed the expose_state_field_in_approx branch from 50dbede to a99823d Compare June 10, 2026 09:01
Comment thread pymc/variational/opvi.py Outdated
Comment on lines +1218 to +1220
constrained = transform.backward(
pt.as_tensor(unconstrained), *rv.owner.inputs
).eval()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is naive, the rv.owner.inputs may also be transformed rvs, we need to get this functionality properly... it exists already in a private repo...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok I will use functionality from #8327 to fix this

@zaxtax zaxtax force-pushed the expose_state_field_in_approx branch 2 times, most recently from e609de3 to ab6ec8b Compare June 30, 2026 16:28
Uses the recently merged constrain_values utility from
pymc.model.transform_values to map unconstrained parameters
back to the original constrained space, replacing the custom
_untransform_tensor helper.
@zaxtax zaxtax force-pushed the expose_state_field_in_approx branch from ab6ec8b to 19dd42b Compare June 30, 2026 16:29
"""ADVI state has family='mean_field', mean and std in constrained space."""
rng = np.random.default_rng(42)
with pm.Model():
pm.HalfNormal("sigma", sigma=5.0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What should this do with a Dirichlet?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We would sample independently for each of the event dims. Is that not what
we did already? I maybe don't understand the question

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ricardoV94 want a test for this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah whenever we do some utility that constrains/unconstrains, it would be nice to test with a transform that changes the dimensionality. Also

@ricardoV94

Copy link
Copy Markdown
Member

What's the API to see the unconstrained ones (some people like to look at those instead) vs the constrained ones?

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