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 str representations for KroneckerNormal and MatrixNormal #4243

Merged
merged 4 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ def _str_repr(self, name=None, dist=None, formatting="plain"):
)

def __str__(self, **kwargs):
return self._str_repr(formatting="plain", **kwargs)
try:
return self._str_repr(formatting="plain", **kwargs)
except:
return super().__str__()
AlexAndorra marked this conversation as resolved.
Show resolved Hide resolved

def _repr_latex_(self, **kwargs):
"""Magic method name for IPython to use for LaTeX formatting."""
Expand Down
10 changes: 10 additions & 0 deletions pymc3/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,9 @@ def logp(self, x):
broadcast_conditions=False,
)

def _distr_parameters_for_repr(self):
return ["eta", "n"]


class MatrixNormal(Continuous):
R"""
Expand Down Expand Up @@ -1712,6 +1715,10 @@ def logp(self, value):
norm = -0.5 * m * n * pm.floatX(np.log(2 * np.pi))
return norm - 0.5 * trquaddist - m * half_collogdet - n * half_rowlogdet

def _distr_parameters_for_repr(self):
mapping = {"tau": "tau", "cov": "cov", "chol": "chol_cov"}
return ["mu", "row" + mapping[self._rowcov_type], "col" + mapping[self._colcov_type]]


class KroneckerNormal(Continuous):
R"""
Expand Down Expand Up @@ -1954,3 +1961,6 @@ def logp(self, value):
"""
quad, logdet = self._quaddist(value)
return -(quad + logdet + self.N * tt.log(2 * np.pi)) / 2.0

def _distr_parameters_for_repr(self):
return ["mu"]
5 changes: 4 additions & 1 deletion pymc3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def _repr_latex_(self, **kwargs):
return self._str_repr(formatting="latex", **kwargs)

def __str__(self, **kwargs):
return self._str_repr(formatting="plain", **kwargs)
try:
return self._str_repr(formatting="plain", **kwargs)
except:
return super().__str__()

__latex__ = _repr_latex_

Expand Down
19 changes: 19 additions & 0 deletions pymc3/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,8 +1782,23 @@ def setup_class(self):
# add a bounded variable as well
bound_var = Bound(Normal, lower=1.0)("bound_var", mu=0, sigma=10)

# KroneckerNormal
n, m = 3, 4
covs = [np.eye(n), np.eye(m)]
kron_normal = KroneckerNormal("kron_normal", mu=np.zeros(n * m), covs=covs, shape=n * m)

# MatrixNormal
matrix_normal = MatrixNormal(
"mat_normal",
mu=np.random.normal(size=n),
rowcov=np.eye(n),
colchol=np.linalg.cholesky(np.eye(n)),
shape=(n, n),
)

# 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, bound_var]
self.expected_latex = (
r"$\text{alpha} \sim \text{Normal}(\mathit{mu}=0.0,~\mathit{sigma}=10.0)$",
Expand All @@ -1793,6 +1808,8 @@ def setup_class(self):
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{kron_normal} \sim \text{KroneckerNormal}(\mathit{mu}=array)$",
r"$\text{mat_normal} \sim \text{MatrixNormal}(\mathit{mu}=array,~\mathit{rowcov}=array,~\mathit{colchol_cov}=array)$",
)
self.expected_str = (
r"alpha ~ Normal(mu=0.0, sigma=10.0)",
Expand All @@ -1802,6 +1819,8 @@ def setup_class(self):
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)",
r"kron_normal ~ KroneckerNormal(mu=array)",
r"mat_normal ~ MatrixNormal(mu=array, rowcov=array, colchol_cov=array)",
)

def test__repr_latex_(self):
Expand Down