generated from aesara-devs/aesara-repo
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a function to generate prior samples
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .prior import sample_prior | ||
|
||
__all__ = ["sample_prior"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import aesara | ||
import aesara.tensor as at | ||
import aesara.tensor.random as ar | ||
|
||
|
||
def sample_prior( | ||
srng: ar.RandomStream, num_samples: at.TensorVariable, *rvs: at.TensorVariable | ||
) -> at.TensorVariable: | ||
"""Sample from a model's prior distributions. | ||
Parameters | ||
---------- | ||
srng: | ||
`RandomStream` instance with which the model was defined. | ||
num_samples: | ||
The number of prior samples to generate. | ||
rvs: | ||
The random variables whose prior distribution we want to sample. | ||
""" | ||
|
||
def step_fn(): | ||
return rvs, srng.state_updates | ||
|
||
samples, updates = aesara.scan(step_fn, n_steps=num_samples) | ||
|
||
return samples, updates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import aesara | ||
import aesara.tensor as at | ||
import numpy as np | ||
|
||
from aemcmc.sample import sample_prior | ||
|
||
|
||
def test_sample_prior(): | ||
srng = at.random.RandomStream(0) | ||
mu_rv = srng.normal(0, 1) | ||
Y_rv = srng.normal(mu_rv, 1.0) | ||
|
||
samples, updates = sample_prior(srng, 10, Y_rv) | ||
fn = aesara.function([], samples) | ||
|
||
samples_vals = fn() | ||
assert np.shape(np.unique(samples_vals)) == (10,) |