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

record TrandformedVariable sample #387

Merged
merged 13 commits into from
Aug 20, 2024
14 changes: 11 additions & 3 deletions model/src/pyrenew/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,14 +693,17 @@ def __init__(
self.transforms = transforms
self.validate()

def sample(self, **kwargs) -> tuple:
def sample(self, record=True, **kwargs) -> tuple:
"""
Sample method. Call self.base_rv.sample()
and then apply the transforms specified
in self.transforms.

Parameters
----------
record : bool, optional
Whether to record the value of the deterministic
RandomVariable. Defaults to True.
**kwargs :
Keyword arguments passed to self.base_rv.sample()

Expand All @@ -711,8 +714,7 @@ def sample(self, **kwargs) -> tuple:
"""

untransformed_values = self.base_rv.sample(**kwargs)

return tuple(
transformed_values = tuple(
SampledValue(
t(uv.value),
t_start=self.t_start,
Expand All @@ -721,6 +723,12 @@ def sample(self, **kwargs) -> tuple:
for t, uv in zip(self.transforms, untransformed_values)
)

if record:
for i, tv in enumerate(transformed_values):
numpyro.deterministic(f"{self.name}_{i}", tv.value)

return transformed_values

def sample_length(self):
"""
Sample length for a transformed
Expand Down