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

Pass a string to NDArray(name=...) and raise NotImplemented on named models in SMC #4365

Merged
merged 3 commits into from
Jan 19, 2021
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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ It also brings some dreadfully awaited fixes, so be sure to go through the chang
- Improve numerical stability in `logp` and `logcdf` methods of `ExGaussian` (see [#4407](https://github.com/pymc-devs/pymc3/pull/4407))
- Issue UserWarning when doing prior or posterior predictive sampling with models containing Potential factors (see [#4419](https://github.com/pymc-devs/pymc3/pull/4419))
- Dirichlet distribution's `random` method is now optimized and gives outputs in correct shape (see [#4416](https://github.com/pymc-devs/pymc3/pull/4407))
- Attempting to sample a named model with SMC will now raise a `NotImplementedError`. (see [#4365](https://github.com/pymc-devs/pymc3/pull/4365))

## PyMC3 3.10.0 (7 December 2020)

Expand Down
5 changes: 5 additions & 0 deletions pymc3/smc/sample_smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ def sample_smc(
_log.info("Initializing SMC sampler...")

model = modelcontext(model)
if model.name:
raise NotImplementedError(
"The SMC implementation currently does not support named models. "
"See https://github.com/pymc-devs/pymc3/pull/4365."
)
if cores is None:
cores = _cpu_count()

Expand Down
2 changes: 1 addition & 1 deletion pymc3/smc/smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def posterior_to_trace(self):
varnames = [v.name for v in self.variables]

with self.model:
strace = NDArray(self.model)
strace = NDArray(name=self.model.name)
strace.setup(lenght_pos, self.chain)
for i in range(lenght_pos):
value = []
Expand Down
21 changes: 21 additions & 0 deletions pymc3/tests/test_smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import numpy as np
import pytest
import theano.tensor as tt

import pymc3 as pm
Expand Down Expand Up @@ -189,3 +190,23 @@ def test_repr_latex(self):
assert expected == self.s._repr_latex_()
assert self.s._repr_latex_() == self.s.__latex__()
assert self.SMABC_test.model._repr_latex_() == self.SMABC_test.model.__latex__()

def test_name_is_string_type(self):
with self.SMABC_potential:
assert not self.SMABC_potential.name
trace = pm.sample_smc(draws=10, kernel="ABC")
assert isinstance(trace._straces[0].name, str)
michaelosthege marked this conversation as resolved.
Show resolved Hide resolved

def test_named_models_are_unsupported(self):
def normal_sim(a, b):
return np.random.normal(a, b, 1000)

with pm.Model(name="NamedModel"):
a = pm.Normal("a", mu=0, sigma=1)
b = pm.HalfNormal("b", sigma=1)
c = pm.Potential("c", pm.math.switch(a > 0, 0, -np.inf))
s = pm.Simulator(
"s", normal_sim, params=(a, b), sum_stat="sort", epsilon=1, observed=self.data
)
with pytest.raises(NotImplementedError, match="named models"):
pm.sample_smc(draws=10, kernel="ABC")