From 2e28dcf53005cae98e1cc6b2409cf157dd3d126b Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 12:15:07 +0100 Subject: [PATCH 1/8] handling of parameters equal to None --- pymc3/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymc3/util.py b/pymc3/util.py index 95c530825a..ec624ab344 100644 --- a/pymc3/util.py +++ b/pymc3/util.py @@ -128,8 +128,8 @@ def get_default_varnames(var_iterator, include_transformed): def get_repr_for_variable(variable, formatting="plain"): """Build a human-readable string representation for a variable.""" - name = variable.name - if name is None: + name = variable.name if variable is not None else None + if name is None and variable is not None: if hasattr(variable, "get_parents"): try: names = [ From 661da7c79d3b38275361b29190a04e7c4fb96954 Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 12:26:28 +0100 Subject: [PATCH 2/8] improved type juggling of bounds --- pymc3/distributions/bound.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pymc3/distributions/bound.py b/pymc3/distributions/bound.py index 11fc9ddafe..67915ee592 100644 --- a/pymc3/distributions/bound.py +++ b/pymc3/distributions/bound.py @@ -28,6 +28,8 @@ from pymc3.distributions import transforms from pymc3.distributions.dist_math import bound +from pymc3.theanof import floatX + __all__ = ["Bound"] @@ -187,12 +189,10 @@ class _ContinuousBounded(_Bounded, Continuous): """ def __init__(self, distribution, lower, upper, transform="infer", *args, **kwargs): - dtype = kwargs.get("dtype", theano.config.floatX) - if lower is not None: - lower = tt.as_tensor_variable(lower).astype(dtype) + lower = tt.as_tensor_variable(floatX(lower)) if upper is not None: - upper = tt.as_tensor_variable(upper).astype(dtype) + upper = tt.as_tensor_variable(floatX(upper)) if transform == "infer": if lower is None and upper is None: From 64642163aa928b3621dc0b5c09eede969d557dd9 Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 13:14:55 +0100 Subject: [PATCH 3/8] updating str repr for bounded variables --- pymc3/distributions/bound.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pymc3/distributions/bound.py b/pymc3/distributions/bound.py index 67915ee592..b7bd2469c1 100644 --- a/pymc3/distributions/bound.py +++ b/pymc3/distributions/bound.py @@ -150,6 +150,25 @@ def random(self, point=None, size=None): not_broadcast_kwargs={"point": point}, ) + def _distr_parameters_for_repr(self): + return ['lower', 'upper'] + + def _distr_name_for_repr(self): + return 'Bound' + + def _str_repr(self, **kwargs): + distr_repr = self._wrapped._str_repr(**{**kwargs, 'dist': self._wrapped}) + if 'formatting' in kwargs and kwargs['formatting'] == 'latex': + distr_repr = distr_repr[distr_repr.index(' \sim')+6:] + else: + distr_repr = distr_repr[distr_repr.index(' ~')+3:] + self_repr = super()._str_repr(**kwargs) + + if 'formatting' in kwargs and kwargs['formatting'] == 'latex': + return self_repr + ' -- ' + distr_repr + else: + return self_repr + '-' + distr_repr + class _DiscreteBounded(_Bounded, Discrete): def __init__(self, distribution, lower, upper, transform="infer", *args, **kwargs): From 83a6a3c144a63994489f7a4965c69f498447ce2f Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 13:15:26 +0100 Subject: [PATCH 4/8] adding bounded variable to str/_repr_latex test --- pymc3/tests/test_distributions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 0ee4e00dd5..13561f9dca 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -1779,9 +1779,12 @@ def setup_class(self): # Expected value of outcome mu = Deterministic("mu", floatX(alpha + tt.dot(X, b))) + # add a bounded variable as well + bound_var = Bound(Normal, lower=1.0)('bound_var', mu=0, sigma=10) + # Likelihood (sampling distribution) of observations Y_obs = Normal("Y_obs", mu=mu, sigma=sigma, observed=Y) - self.distributions = [alpha, sigma, mu, b, Z, Y_obs] + self.distributions = [alpha, sigma, mu, b, Z, Y_obs, bound_var] self.expected_latex = ( r"$\text{alpha} \sim \text{Normal}(\mathit{mu}=0.0,~\mathit{sigma}=10.0)$", r"$\text{sigma} \sim \text{HalfNormal}(\mathit{sigma}=1.0)$", @@ -1789,6 +1792,7 @@ def setup_class(self): r"$\text{beta} \sim \text{Normal}(\mathit{mu}=0.0,~\mathit{sigma}=10.0)$", r"$\text{Z} \sim \text{MvNormal}(\mathit{mu}=array,~\mathit{chol_cov}=array)$", r"$\text{Y_obs} \sim \text{Normal}(\mathit{mu}=\text{mu},~\mathit{sigma}=f(\text{sigma}))$", + r"$\\text{bound_var} \\sim \\text{Bound}(\\mathit{lower}=1.0,~\\mathit{upper}=\\text{None})$ -- \\text{Normal}(\\mathit{mu}=0.0,~\\mathit{sigma}=10.0)$", ) self.expected_str = ( r"alpha ~ Normal(mu=0.0, sigma=10.0)", @@ -1797,6 +1801,7 @@ def setup_class(self): r"beta ~ Normal(mu=0.0, sigma=10.0)", r"Z ~ MvNormal(mu=array, chol_cov=array)", r"Y_obs ~ Normal(mu=mu, sigma=f(sigma))", + r"bound_var ~ Bound(lower=1.0, upper=None)-Normal(mu=0.0, sigma=10.0)", ) def test__repr_latex_(self): From 203789dfa9253466f863519a73c2d1e278760060 Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 13:18:28 +0100 Subject: [PATCH 5/8] updating test --- pymc3/tests/test_distributions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 13561f9dca..66552020d6 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -1792,7 +1792,7 @@ def setup_class(self): r"$\text{beta} \sim \text{Normal}(\mathit{mu}=0.0,~\mathit{sigma}=10.0)$", r"$\text{Z} \sim \text{MvNormal}(\mathit{mu}=array,~\mathit{chol_cov}=array)$", r"$\text{Y_obs} \sim \text{Normal}(\mathit{mu}=\text{mu},~\mathit{sigma}=f(\text{sigma}))$", - r"$\\text{bound_var} \\sim \\text{Bound}(\\mathit{lower}=1.0,~\\mathit{upper}=\\text{None})$ -- \\text{Normal}(\\mathit{mu}=0.0,~\\mathit{sigma}=10.0)$", + r"$\text{bound_var} \sim \text{Bound}(\mathit{lower}=1.0,~\mathit{upper}=\text{None})$ -- \text{Normal}(\mathit{mu}=0.0,~\mathit{sigma}=10.0)$", ) self.expected_str = ( r"alpha ~ Normal(mu=0.0, sigma=10.0)", From 2732bb7f6986b45976f0e2e9a9748ac6da6b88c2 Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 15:08:50 +0100 Subject: [PATCH 6/8] black formatting --- pymc3/distributions/bound.py | 18 +++++++++--------- pymc3/tests/test_distributions.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pymc3/distributions/bound.py b/pymc3/distributions/bound.py index b7bd2469c1..901baf6b8e 100644 --- a/pymc3/distributions/bound.py +++ b/pymc3/distributions/bound.py @@ -151,23 +151,23 @@ def random(self, point=None, size=None): ) def _distr_parameters_for_repr(self): - return ['lower', 'upper'] + return ["lower", "upper"] def _distr_name_for_repr(self): - return 'Bound' + return "Bound" def _str_repr(self, **kwargs): - distr_repr = self._wrapped._str_repr(**{**kwargs, 'dist': self._wrapped}) - if 'formatting' in kwargs and kwargs['formatting'] == 'latex': - distr_repr = distr_repr[distr_repr.index(' \sim')+6:] + distr_repr = self._wrapped._str_repr(**{**kwargs, "dist": self._wrapped}) + if "formatting" in kwargs and kwargs["formatting"] == "latex": + distr_repr = distr_repr[distr_repr.index(" \sim") + 6 :] else: - distr_repr = distr_repr[distr_repr.index(' ~')+3:] + distr_repr = distr_repr[distr_repr.index(" ~") + 3 :] self_repr = super()._str_repr(**kwargs) - if 'formatting' in kwargs and kwargs['formatting'] == 'latex': - return self_repr + ' -- ' + distr_repr + if "formatting" in kwargs and kwargs["formatting"] == "latex": + return self_repr + " -- " + distr_repr else: - return self_repr + '-' + distr_repr + return self_repr + "-" + distr_repr class _DiscreteBounded(_Bounded, Discrete): diff --git a/pymc3/tests/test_distributions.py b/pymc3/tests/test_distributions.py index 66552020d6..20349d104f 100644 --- a/pymc3/tests/test_distributions.py +++ b/pymc3/tests/test_distributions.py @@ -1780,7 +1780,7 @@ def setup_class(self): mu = Deterministic("mu", floatX(alpha + tt.dot(X, b))) # add a bounded variable as well - bound_var = Bound(Normal, lower=1.0)('bound_var', mu=0, sigma=10) + bound_var = Bound(Normal, lower=1.0)("bound_var", mu=0, sigma=10) # Likelihood (sampling distribution) of observations Y_obs = Normal("Y_obs", mu=mu, sigma=sigma, observed=Y) From c64502be025f7087014cc6a474677decd327f734 Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 15:09:53 +0100 Subject: [PATCH 7/8] removing unused import --- pymc3/distributions/bound.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pymc3/distributions/bound.py b/pymc3/distributions/bound.py index 901baf6b8e..2cbaeb110f 100644 --- a/pymc3/distributions/bound.py +++ b/pymc3/distributions/bound.py @@ -16,7 +16,6 @@ import numpy as np import theano.tensor as tt -import theano from pymc3.distributions.distribution import ( Distribution, From 87157bcec757a8d16233020221a8fa12a2a1a997 Mon Sep 17 00:00:00 2001 From: Eelke Spaak Date: Tue, 10 Nov 2020 15:14:32 +0100 Subject: [PATCH 8/8] change string to r-string --- pymc3/distributions/bound.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc3/distributions/bound.py b/pymc3/distributions/bound.py index 2cbaeb110f..ae0ac67354 100644 --- a/pymc3/distributions/bound.py +++ b/pymc3/distributions/bound.py @@ -158,7 +158,7 @@ def _distr_name_for_repr(self): def _str_repr(self, **kwargs): distr_repr = self._wrapped._str_repr(**{**kwargs, "dist": self._wrapped}) if "formatting" in kwargs and kwargs["formatting"] == "latex": - distr_repr = distr_repr[distr_repr.index(" \sim") + 6 :] + distr_repr = distr_repr[distr_repr.index(r" \sim") + 6 :] else: distr_repr = distr_repr[distr_repr.index(" ~") + 3 :] self_repr = super()._str_repr(**kwargs)