From 294d59ad038c0d561bcad1130bd4ba7fd8c970f8 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Sun, 7 Nov 2021 12:07:23 +0000 Subject: [PATCH 1/7] Add moment and base tests --- pymc/distributions/continuous.py | 6 ++++++ pymc/tests/test_distributions_moments.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index d3bfe644c4..17ec179f56 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2637,6 +2637,12 @@ def dist(cls, nu=1, sigma=None, lam=None, sd=None, *args, **kwargs): assert_negative_support(sigma, "sigma", "HalfStudentT") return super().dist([nu, sigma], *args, **kwargs) + + def get_moment(rv, size, nu, sigma): + median = at.as_tensor_varialbe(sigma) + if not rv_size_is_none(size): + median = at.full(size, median) + return median def logp(value, nu, sigma): """ diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 36fe591e69..10aa61369e 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -16,6 +16,7 @@ LogNormal, StudentT, Weibull, + HalfStudentT, ) from pymc.distributions.shape_utils import rv_size_is_none from pymc.initial_point import make_initial_point_fn @@ -127,6 +128,26 @@ def test_halfnormal_moment(sigma, size, expected): assert_moment_is_expected(model, expected) +@pytest.mark.parametrize( + "mu, sigma, size, expected", + [ + (0, 1, None, np.exp(0.5)), + (0, 1, 5, np.full(5, np.exp(0.5))), + (np.arange(5), 1, None, np.exp(np.arange(5) + 0.5)), + ( + np.arange(5), + np.arange(1, 6), + (2, 5), + np.full((2, 5), np.exp(np.arange(5) + 0.5 * np.arange(1, 6) ** 2)), + ), + ], +) +def test_halfstudentt_moment(nu, sigma, size, expected): + with Model() as model: + HalfStudentT("x", nu=nu, sigma=sigma, size=size) + assert_moment_is_expected(model, expected) + + @pytest.mark.skip(reason="aeppl interval transform fails when both edges are None") @pytest.mark.parametrize( "mu, sigma, lower, upper, size, expected", From e0a83d760b6c1626a60acee504cc6ec64e3620d1 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Mon, 8 Nov 2021 07:42:12 +0000 Subject: [PATCH 2/7] Fix tests and clean code --- pymc/distributions/continuous.py | 7 ++----- pymc/tests/test_distributions_moments.py | 15 +++++---------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 17ec179f56..2b96f58d50 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2628,10 +2628,6 @@ def dist(cls, nu=1, sigma=None, lam=None, sd=None, *args, **kwargs): lam, sigma = get_tau_sigma(lam, sigma) sigma = at.as_tensor_variable(sigma) - # mode = at.as_tensor_variable(0) - # median = at.as_tensor_variable(sigma) - # sd = at.as_tensor_variable(sigma) - assert_negative_support(nu, "nu", "HalfStudentT") assert_negative_support(lam, "lam", "HalfStudentT") assert_negative_support(sigma, "sigma", "HalfStudentT") @@ -2639,7 +2635,8 @@ def dist(cls, nu=1, sigma=None, lam=None, sd=None, *args, **kwargs): return super().dist([nu, sigma], *args, **kwargs) def get_moment(rv, size, nu, sigma): - median = at.as_tensor_varialbe(sigma) + lam, sigma = get_tau_sigma(None, sigma) + median = at.as_tensor_variable(sigma) if not rv_size_is_none(size): median = at.full(size, median) return median diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 10aa61369e..3f9aecde22 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -129,17 +129,12 @@ def test_halfnormal_moment(sigma, size, expected): @pytest.mark.parametrize( - "mu, sigma, size, expected", + "nu, sigma, size, expected", [ - (0, 1, None, np.exp(0.5)), - (0, 1, 5, np.full(5, np.exp(0.5))), - (np.arange(5), 1, None, np.exp(np.arange(5) + 0.5)), - ( - np.arange(5), - np.arange(1, 6), - (2, 5), - np.full((2, 5), np.exp(np.arange(5) + 0.5 * np.arange(1, 6) ** 2)), - ), + (1, 1, None, 1), + (1, 1, 5, np.ones(5)), + (1, np.arange(5), None, np.arange(5)), + (1, np.arange(5), (2, 5), np.full((2, 5), np.arange(5))), ], ) def test_halfstudentt_moment(nu, sigma, size, expected): From 72f1fa3222d4ebc6a5a0fb5c3580a0a97a3c7b70 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Mon, 8 Nov 2021 09:38:02 +0000 Subject: [PATCH 3/7] Add new test condition and broadcasting --- pymc/distributions/continuous.py | 2 +- pymc/tests/test_distributions_moments.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 2b96f58d50..30042a2698 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2635,8 +2635,8 @@ def dist(cls, nu=1, sigma=None, lam=None, sd=None, *args, **kwargs): return super().dist([nu, sigma], *args, **kwargs) def get_moment(rv, size, nu, sigma): - lam, sigma = get_tau_sigma(None, sigma) median = at.as_tensor_variable(sigma) + sigma, _ = at.broadcast_arrays(sigma, nu) if not rv_size_is_none(size): median = at.full(size, median) return median diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 3f9aecde22..c2bf402946 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -135,6 +135,7 @@ def test_halfnormal_moment(sigma, size, expected): (1, 1, 5, np.ones(5)), (1, np.arange(5), None, np.arange(5)), (1, np.arange(5), (2, 5), np.full((2, 5), np.arange(5))), + (np.arange(1, 6), 1, 5, np.full(5, 1)), ], ) def test_halfstudentt_moment(nu, sigma, size, expected): From 8331f0b75bb0ad51d2764ef89a64f8010a0ef3e1 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Mon, 8 Nov 2021 11:35:30 +0000 Subject: [PATCH 4/7] Update pymc/distributions/continuous.py Co-authored-by: Ricardo Vieira <28983449+ricardoV94@users.noreply.github.com> --- pymc/distributions/continuous.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 30042a2698..b2571cdbd5 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2635,11 +2635,10 @@ def dist(cls, nu=1, sigma=None, lam=None, sd=None, *args, **kwargs): return super().dist([nu, sigma], *args, **kwargs) def get_moment(rv, size, nu, sigma): - median = at.as_tensor_variable(sigma) sigma, _ = at.broadcast_arrays(sigma, nu) if not rv_size_is_none(size): - median = at.full(size, median) - return median + sigma = at.full(size, sigma) + return sigma def logp(value, nu, sigma): """ From e16f97410c5f9de40176b239ff008147134c6d80 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Mon, 8 Nov 2021 15:56:54 +0000 Subject: [PATCH 5/7] fix precommit errors --- pymc/tests/test_distributions_moments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index c2bf402946..2b27283bdc 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -11,12 +11,12 @@ Gamma, HalfCauchy, HalfNormal, + HalfStudentT, Kumaraswamy, Laplace, LogNormal, StudentT, Weibull, - HalfStudentT, ) from pymc.distributions.shape_utils import rv_size_is_none from pymc.initial_point import make_initial_point_fn From 037fff0628543bf57bad7e2cf1dc8eff363b7da6 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Mon, 8 Nov 2021 16:52:34 +0000 Subject: [PATCH 6/7] fix precommit errors blank --- pymc/distributions/continuous.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index b2571cdbd5..8578ab5a95 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2633,7 +2633,7 @@ def dist(cls, nu=1, sigma=None, lam=None, sd=None, *args, **kwargs): assert_negative_support(sigma, "sigma", "HalfStudentT") return super().dist([nu, sigma], *args, **kwargs) - + def get_moment(rv, size, nu, sigma): sigma, _ = at.broadcast_arrays(sigma, nu) if not rv_size_is_none(size): From 0c6c46cb1dea8625abb1934083f531ca5008a746 Mon Sep 17 00:00:00 2001 From: Alihan Zihna Date: Mon, 8 Nov 2021 17:34:11 +0000 Subject: [PATCH 7/7] change the tests --- pymc/tests/test_distributions_moments.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 2b27283bdc..f41f9f63eb 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -133,9 +133,8 @@ def test_halfnormal_moment(sigma, size, expected): [ (1, 1, None, 1), (1, 1, 5, np.ones(5)), - (1, np.arange(5), None, np.arange(5)), (1, np.arange(5), (2, 5), np.full((2, 5), np.arange(5))), - (np.arange(1, 6), 1, 5, np.full(5, 1)), + (np.arange(1, 6), 1, None, np.full(5, 1)), ], ) def test_halfstudentt_moment(nu, sigma, size, expected):