From 1f468a960696914d562c9dc16ab4f75aed8d20fd Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 21 Dec 2020 17:57:10 +0100 Subject: [PATCH 1/3] pass a string to NDArray(name=...) Currently, `name` is a `Model`. --- pymc3/smc/smc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc3/smc/smc.py b/pymc3/smc/smc.py index 1b4400856d..b49502cdfc 100644 --- a/pymc3/smc/smc.py +++ b/pymc3/smc/smc.py @@ -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(self.model.name) strace.setup(lenght_pos, self.chain) for i in range(lenght_pos): value = [] From 25016fbdde26baa39da568db1fd1d00a97a22e65 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 22 Dec 2020 14:57:35 +0000 Subject: [PATCH 2/3] Add tests related to named models A test was added to asserts that strace.name is a string (this was not the case before #4365). Non-empty model names are actually not supported (again, see #4365) so attempting to SMC-sample a named model will now raise a NotImplementedError. --- pymc3/smc/sample_smc.py | 5 +++++ pymc3/smc/smc.py | 2 +- pymc3/tests/test_smc.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pymc3/smc/sample_smc.py b/pymc3/smc/sample_smc.py index b02f789627..90b953b066 100644 --- a/pymc3/smc/sample_smc.py +++ b/pymc3/smc/sample_smc.py @@ -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() diff --git a/pymc3/smc/smc.py b/pymc3/smc/smc.py index b49502cdfc..6d8b92427c 100644 --- a/pymc3/smc/smc.py +++ b/pymc3/smc/smc.py @@ -255,7 +255,7 @@ def posterior_to_trace(self): varnames = [v.name for v in self.variables] with self.model: - strace = NDArray(self.model.name) + strace = NDArray(name=self.model.name) strace.setup(lenght_pos, self.chain) for i in range(lenght_pos): value = [] diff --git a/pymc3/tests/test_smc.py b/pymc3/tests/test_smc.py index bad44f0f43..695ea461f7 100644 --- a/pymc3/tests/test_smc.py +++ b/pymc3/tests/test_smc.py @@ -13,6 +13,7 @@ # limitations under the License. import numpy as np +import pytest import theano.tensor as tt import pymc3 as pm @@ -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) + + 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") From 9b4bff30d3d5cf385441ccaaa3c8359fd95589e4 Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Fri, 15 Jan 2021 22:58:06 +0100 Subject: [PATCH 3/3] Mention SMC/named models related PR #4365 --- RELEASE-NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index a9fb04990a..ac4bb2f877 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -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)