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

Support PyTensor deterministic operations as observations #7656

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pymc/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from pytensor.scalar import Cast
from pytensor.tensor.elemwise import Elemwise
from pytensor.tensor.random.basic import IntegersRV
from pytensor.tensor.random.var import RandomGeneratorSharedVariable
from pytensor.tensor.type import TensorType
from pytensor.tensor.variable import TensorConstant, TensorVariable

Expand Down Expand Up @@ -148,6 +149,24 @@ def __str__(self):
return "Minibatch"


def first_inputs(r):
if not r.owner:
return

inputs = r.owner.inputs

if not inputs:
return

first_input = inputs[0]
yield first_input
yield from first_inputs(first_input)


def has_random_ancestor(r):
return any(isinstance(i, RandomGeneratorSharedVariable) for i in first_inputs(r))
wd60622 marked this conversation as resolved.
Show resolved Hide resolved


def is_valid_observed(v) -> bool:
if not isinstance(v, Variable):
# Non-symbolic constant
Expand All @@ -165,6 +184,7 @@ def is_valid_observed(v) -> bool:
and isinstance(v.owner.op.scalar_op, Cast)
and is_valid_observed(v.owner.inputs[0])
)
or not has_random_ancestor(v)
wd60622 marked this conversation as resolved.
Show resolved Hide resolved
# Or Minibatch
or (
isinstance(v.owner.op, MinibatchOp)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,14 @@ def test_multiple_vars(self):
[draw_mA, draw_mB] = pm.draw([mA, mB])
assert draw_mA.shape == (10,)
np.testing.assert_allclose(draw_mA, -draw_mB)


def test_scaling_data_works_in_likelihood() -> None:
data = np.array([10, 11, 12, 13, 14, 15])

with pm.Model() as model:
target = pm.Data("target", data)
scale = 12
scaled_target = target / scale
mu = pm.Normal("mu", mu=0, sigma=1)
pm.Normal("x", mu=mu, sigma=1, observed=scaled_target)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I sample this to check that it has the correct data in the InferenceData?

Copy link
Member

Choose a reason for hiding this comment

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

No, we have more direct ways of testing it

Loading