Skip to content

Add warning if observed in DensityDist is dict #6292

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

Merged
merged 4 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions pymc/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,14 @@ def random(mu, rng=None, size=None):

def __new__(cls, name, *args, **kwargs):
kwargs.setdefault("class_name", name)
if isinstance(kwargs.get("observed", None), dict):
raise TypeError(
"Since ``v4.0.0`` the ``observed`` parameter should be of type"
" ``pd.Series``, ``np.array``, or ``pm.Data``."
" Previous versions allowed passing distribution parameters as"
" a dictionary in ``observed``, in the current version these "
"parameters are positional arguments."
)
return super().__new__(cls, name, *args, **kwargs)

@classmethod
Expand Down
22 changes: 21 additions & 1 deletion pymc/tests/distributions/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,29 @@ def test_density_dist_with_random(self, size):
random=lambda mu, rng=None, size=None: rng.normal(loc=mu, scale=1, size=size),
observed=np.random.randn(100, *size),
)

assert obs.eval().shape == (100,) + size

def test_density_dist_with_random_invalid_observed(self):
with pytest.raises(
TypeError,
match=(
"Since ``v4.0.0`` the ``observed`` parameter should be of type"
" ``pd.Series``, ``np.array``, or ``pm.Data``."
" Previous versions allowed passing distribution parameters as"
" a dictionary in ``observed``, in the current version these "
"parameters are positional arguments."
),
):
size = (3,)
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
pm.DensityDist(
"density_dist",
mu,
random=lambda mu, rng=None, size=None: rng.normal(loc=mu, scale=1, size=size),
observed={"values": np.random.randn(100, *size)},
)

def test_density_dist_without_random(self):
with pm.Model() as model:
mu = pm.Normal("mu", 0, 1)
Expand Down