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
1 parent
ab429b2
commit 51db0a2
Showing
3 changed files
with
137 additions
and
2 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,31 @@ | ||
import aesara | ||
import aesara.tensor as at | ||
import aesara.tensor.random as ar | ||
|
||
from aemcmc.utils import get_rv_updates | ||
|
||
|
||
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. | ||
""" | ||
|
||
rv_updates = get_rv_updates(srng, *rvs) | ||
|
||
def step_fn(): | ||
return rvs, rv_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
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,50 @@ | ||
import aesara | ||
import aesara.tensor as at | ||
import numpy as np | ||
from aesara.compile.sharedvalue import SharedVariable | ||
|
||
from aemcmc.sample import sample_prior | ||
|
||
|
||
def test_sample_prior(): | ||
srng = at.random.RandomStream(123) | ||
|
||
mu_rv = srng.normal(0, 1, name="mu") | ||
Y_rv = srng.normal(mu_rv, 1.0, name="Y") | ||
Z_rv = srng.gamma(0.5, 0.5, name="Z") | ||
|
||
samples, updates = sample_prior(srng, 10, Y_rv) | ||
fn = aesara.function([], samples, updates=updates) | ||
|
||
# Make sure that `Z_rv` doesn't sneak into our prior sampling. | ||
rng_objects = set( | ||
var.get_value(borrow=True) | ||
for var in fn.maker.fgraph.variables | ||
if isinstance(var, SharedVariable) | ||
) | ||
|
||
assert mu_rv.owner.inputs[0].get_value(borrow=True) in rng_objects | ||
assert Y_rv.owner.inputs[0].get_value(borrow=True) in rng_objects | ||
assert Z_rv.owner.inputs[0].get_value(borrow=True) not in rng_objects | ||
|
||
samples_vals = fn() | ||
assert np.shape(np.unique(samples_vals)) == (10,) | ||
|
||
# Try it again, but without a default update | ||
Y_rv.owner.inputs[0].default_update = None | ||
|
||
samples, updates = sample_prior(srng, 10, Y_rv) | ||
fn = aesara.function([], samples, updates=updates) | ||
|
||
rng_objects = set( | ||
var.get_value(borrow=True) | ||
for var in fn.maker.fgraph.variables | ||
if isinstance(var, SharedVariable) | ||
) | ||
|
||
assert mu_rv.owner.inputs[0].get_value(borrow=True) in rng_objects | ||
assert Y_rv.owner.inputs[0].get_value(borrow=True) in rng_objects | ||
assert Z_rv.owner.inputs[0].get_value(borrow=True) not in rng_objects | ||
|
||
samples_vals = fn() | ||
assert np.shape(np.unique(samples_vals)) == (10,) |