Skip to content

Add MvStudentT and MatrixNormal moment #5173

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

Merged
merged 15 commits into from
Nov 21, 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
16 changes: 15 additions & 1 deletion pymc/distributions/multivariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ def dist(cls, nu, Sigma=None, mu=None, cov=None, tau=None, chol=None, lower=True
assert_negative_support(nu, "nu", "MvStudentT")
return super().dist([nu, mu, cov], **kwargs)

def get_moment(rv, size, nu, mu, cov):
moment = mu
if not rv_size_is_none(size):
moment_size = at.concatenate([size, moment.shape])
moment = at.full(moment_size, moment)
return moment

def logp(value, nu, mu, cov):
"""
Calculate log-probability of Multivariate Student's T distribution
Expand Down Expand Up @@ -692,7 +699,7 @@ def dist(cls, eta, cutpoints, n, *args, **kwargs):


class OrderedMultinomial:
R"""
r"""
Wrapper class for Ordered Multinomial distributions.

Useful for regression on ordinal data whose values range
Expand Down Expand Up @@ -1727,6 +1734,13 @@ def dist(

return super().dist([mu, rowchol_cov, colchol_cov], **kwargs)

def get_moment(rv, size, mu, rowchol, colchol):
output_shape = (rowchol.shape[0], colchol.shape[0])
if not rv_size_is_none(size):
output_shape = at.concatenate([size, output_shape])
moment = at.full(output_shape, mu)
return moment

def logp(value, mu, rowchol, colchol):
"""
Calculate log-probability of Matrix-valued Normal distribution
Expand Down
47 changes: 47 additions & 0 deletions pymc/tests/test_distributions_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
Logistic,
LogitNormal,
LogNormal,
MatrixNormal,
Moyal,
MvStudentT,
NegativeBinomial,
Normal,
Pareto,
Expand Down Expand Up @@ -830,6 +832,33 @@ def test_moyal_moment(mu, sigma, size, expected):
assert_moment_is_expected(model, expected)


rand1d = np.random.rand(2)
rand2d = np.random.rand(2, 3)


@pytest.mark.parametrize(
"nu, mu, cov, size, expected",
[
(2, np.ones(1), np.eye(1), None, np.ones(1)),
(2, rand1d, np.eye(2), None, rand1d),
(2, rand1d, np.eye(2), 2, np.full((2, 2), rand1d)),
(2, rand1d, np.eye(2), (2, 5), np.full((2, 5, 2), rand1d)),
(2, rand2d, np.eye(3), None, rand2d),
(2, rand2d, np.eye(3), 2, np.full((2, 2, 3), rand2d)),
(2, rand2d, np.eye(3), (2, 5), np.full((2, 5, 2, 3), rand2d)),
],
)
def test_mvstudentt_moment(nu, mu, cov, size, expected):
with Model() as model:
MvStudentT("x", nu=nu, mu=mu, cov=cov, size=size)
assert_moment_is_expected(model, expected)


def check_matrixnormal_moment(mu, rowchol, colchol, size, expected):
with Model() as model:
MatrixNormal("x", mu=mu, rowchol=rowchol, colchol=colchol, size=size)


@pytest.mark.parametrize(
"alpha, mu, sigma, size, expected",
[
Expand Down Expand Up @@ -886,6 +915,24 @@ def test_asymmetriclaplace_moment(b, kappa, mu, size, expected):
assert_moment_is_expected(model, expected)


@pytest.mark.parametrize(
"mu, rowchol, colchol, size, expected",
[
(np.ones((1, 1)), np.eye(1), np.eye(1), None, np.ones((1, 1))),
(np.ones((1, 1)), np.eye(2), np.eye(3), None, np.ones((2, 3))),
(rand2d, np.eye(2), np.eye(3), None, rand2d),
(rand2d, np.eye(2), np.eye(3), 2, np.full((2, 2, 3), rand2d)),
(rand2d, np.eye(2), np.eye(3), (2, 5), np.full((2, 5, 2, 3), rand2d)),
],
)
def test_matrixnormal_moment(mu, rowchol, colchol, size, expected):
if size is None:
check_matrixnormal_moment(mu, rowchol, colchol, size, expected)
else:
with pytest.raises(NotImplementedError):
check_matrixnormal_moment(mu, rowchol, colchol, size, expected)


@pytest.mark.parametrize(
"nu, sigma, size, expected",
[
Expand Down