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

Add a function to generate prior samples #109

Merged
merged 1 commit into from
Mar 23, 2023

Conversation

rlouf
Copy link
Member

@rlouf rlouf commented Feb 17, 2023

Here we add a utility function to generate prior samples for any variable present in a model. The function should return samples in a format that is convenient for the users. The format is still TBD, although the standard in the Python PPL world seems to be XArray (for compatibility with ArviZ).

import aesara
import aesara.tensor as at

from aemcmc.sample import sample_prior


srng = at.random.RandomStream(0)
mu_rv = srng.normal(0, 1)
Y_rv = srng.normal(mu_rv, 1.0)

num_samples = at.scalar()
samples, updates = sample_prior(srng, num_samples, Y_rv, mu_rv)
sample_fn = aesara.function([num_samples], samples, updates=updates)
sample_fn(10)

Note: it is not necessary to pass the updates to function in this case (but we need to make sure to return them as outputs to Scan's inner function), can we just not return them at all to simplify the interface further?

Related to #101

@codecov
Copy link

codecov bot commented Feb 17, 2023

Codecov Report

Attention: Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.

Project coverage is 98.03%. Comparing base (64b0e50) to head (fd670d5).
Report is 19 commits behind head on main.

Files Patch % Lines
aemcmc/utils.py 80.00% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #109      +/-   ##
==========================================
- Coverage   98.50%   98.03%   -0.47%     
==========================================
  Files          10       11       +1     
  Lines         737      765      +28     
  Branches       63       69       +6     
==========================================
+ Hits          726      750      +24     
- Misses          4        5       +1     
- Partials        7       10       +3     

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

@rlouf rlouf force-pushed the sample-prior branch 2 times, most recently from cf670bb to c188971 Compare February 18, 2023 06:51
@brandonwillard brandonwillard added important enhancement New feature or request labels Feb 19, 2023
@rlouf
Copy link
Member Author

rlouf commented Feb 19, 2023

Decisions regarding the output format will be informed by the constraints that a general-purpose sampling function imposes.

Here are some thoughts:

  • Returning a list is certainly the easiest, but it is also more cumbersome for the user;
  • Returning a dictionary indexed by the random variables does not play well with downstream formats like XArray (to interface with ArviZ), and the random variables are not necessarily available to the user if the model is defined inside a function;
  • Returning a dictionary indexed by variable name requires users to name variables. How do we handle missing names?

@rlouf rlouf marked this pull request as ready for review February 20, 2023 20:55
@brandonwillard brandonwillard force-pushed the sample-prior branch 2 times, most recently from d477390 to d2fdc5f Compare March 17, 2023 22:12
@dgerlanc
Copy link
Contributor

  • Returning a dictionary indexed by variable name requires users to name variables. How do we handle missing names?

We could automatically generate sequential names.

Alternatively, we return a list and provide another function(s) that generate other formats, dict, xarray

@brandonwillard
Copy link
Member

We could automatically generate sequential names.

Definitely; and we can use the Variable.auto_name values for that.

Alternatively, we return a list and provide another function(s) that generate other formats, dict, xarray

Also a very viable approach!

brandonwillard
brandonwillard previously approved these changes Mar 22, 2023
Copy link
Member

@brandonwillard brandonwillard left a comment

Choose a reason for hiding this comment

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

This could use a test for the uncovered case in get_rv_updates, but, other than that, this should be good to go.

@brandonwillard brandonwillard force-pushed the sample-prior branch 3 times, most recently from b5e3b25 to 51db0a2 Compare March 23, 2023 23:00
@brandonwillard brandonwillard merged commit 12792a8 into aesara-devs:main Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request important
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants