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

fix #4216: str and latex representations for Bound variables #4217

Merged
merged 8 commits into from
Nov 15, 2020
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
28 changes: 23 additions & 5 deletions pymc3/distributions/bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

from pymc3.distributions.distribution import (
Distribution,
Expand All @@ -28,6 +27,8 @@
from pymc3.distributions import transforms
from pymc3.distributions.dist_math import bound

from pymc3.theanof import floatX

__all__ = ["Bound"]


Expand Down Expand Up @@ -148,6 +149,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(r" \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):
Expand Down Expand Up @@ -187,12 +207,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:
Expand Down
7 changes: 6 additions & 1 deletion pymc3/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,16 +1779,20 @@ 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)$",
r"$\text{mu} \sim \text{Deterministic}(\text{alpha},~\text{Constant},~\text{beta})$",
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)",
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pymc3/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down