From a3d4f878520835c7662e7e96a08e9904b9b81590 Mon Sep 17 00:00:00 2001 From: karm216 Date: Tue, 9 Nov 2021 20:29:15 +0530 Subject: [PATCH 1/9] add get_moment --- pymc/distributions/continuous.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 70c50502f7..79369f6376 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2771,15 +2771,22 @@ def dist(cls, mu=0.0, sigma=None, nu=None, sd=None, *args, **kwargs): sigma = at.as_tensor_variable(floatX(sigma)) nu = at.as_tensor_variable(floatX(nu)) - # sd = sigma - # mean = mu + nu - # variance = (sigma ** 2) + (nu ** 2) + sd = sigma + mean = mu + nu + variance = (sigma ** 2) + (nu ** 2) assert_negative_support(sigma, "sigma", "ExGaussian") assert_negative_support(nu, "nu", "ExGaussian") return super().dist([mu, sigma, nu], *args, **kwargs) + def get_moment(rv, size, mu, sigma, nu): + moment, _ = at.broadcast_arrays(sigma, nu) + moment = mu + nu + if not rv_size_is_none(size): + moment = at.full(size, moment) + return moment + def logp(value, mu, sigma, nu): """ Calculate log-probability of ExGaussian distribution at specified value. From 00ce7d0f089ace7a18d04efd50273537083e1c49 Mon Sep 17 00:00:00 2001 From: karm216 Date: Tue, 9 Nov 2021 22:38:49 +0530 Subject: [PATCH 2/9] added ExGaussian Test cases --- pymc/distributions/continuous.py | 4 ---- pymc/tests/test_distributions_moments.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 79369f6376..1df635b644 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2771,10 +2771,6 @@ def dist(cls, mu=0.0, sigma=None, nu=None, sd=None, *args, **kwargs): sigma = at.as_tensor_variable(floatX(sigma)) nu = at.as_tensor_variable(floatX(nu)) - sd = sigma - mean = mu + nu - variance = (sigma ** 2) + (nu ** 2) - assert_negative_support(sigma, "sigma", "ExGaussian") assert_negative_support(nu, "nu", "ExGaussian") diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 9e2d56afdb..827527df1b 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -10,6 +10,7 @@ Cauchy, ChiSquared, Constant, + ExGaussian, Exponential, Gamma, HalfCauchy, @@ -434,3 +435,18 @@ def test_logistic_moment(mu, s, size, expected): with Model() as model: Logistic("x", mu=mu, s=s, size=size) assert_moment_is_expected(model, expected) + + +@pytest.mark.parametrize( + "mu, nu, sigma, size, expected", + [ + (1, 1, None, None, 2), + (1, 1, None, 5, np.full(5, 2)), + (1, np.arange(1, 6), None, None, np.arange(2, 7)), + (1, np.arange(1, 6), None, (2, 5), np.full((2, 5), np.arange(2, 7))), + ], +) +def test_exgaussian_moment(mu, nu, sigma, size, expected): + with Model() as model: + ExGaussian("x", mu=mu, sigma=sigma, nu=nu, size=size) + assert_moment_is_expected(model, expected) From 9cadcc30ad4c32ba64035b8a60d12d0ac96e54ab Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 08:07:33 +0530 Subject: [PATCH 3/9] Corrected broadcast_arrays in Exagaussion moment and added 1 testcase to test that line --- 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 1df635b644..5b1d3b9d48 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2777,7 +2777,7 @@ def dist(cls, mu=0.0, sigma=None, nu=None, sd=None, *args, **kwargs): return super().dist([mu, sigma, nu], *args, **kwargs) def get_moment(rv, size, mu, sigma, nu): - moment, _ = at.broadcast_arrays(sigma, nu) + mu, nu, _ = at.broadcast_arrays(mu, nu, sigma) moment = mu + nu if not rv_size_is_none(size): moment = at.full(size, moment) diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 827527df1b..47969e5730 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -441,6 +441,7 @@ def test_logistic_moment(mu, s, size, expected): "mu, nu, sigma, size, expected", [ (1, 1, None, None, 2), + (np.ones(2, 5), 1, 1, None, np.full([2, 5], 2)), (1, 1, None, 5, np.full(5, 2)), (1, np.arange(1, 6), None, None, np.arange(2, 7)), (1, np.arange(1, 6), None, (2, 5), np.full((2, 5), np.arange(2, 7))), From 7973579acad35fab609d8019d4884f8dee9f763f Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 08:46:11 +0530 Subject: [PATCH 4/9] Corrected test case for ExGaussian --- 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 47969e5730..49691e0829 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -441,7 +441,7 @@ def test_logistic_moment(mu, s, size, expected): "mu, nu, sigma, size, expected", [ (1, 1, None, None, 2), - (np.ones(2, 5), 1, 1, None, np.full([2, 5], 2)), + (np.ones((2, 5)), 1, 1, None, np.full([2, 5], 2)), (1, 1, None, 5, np.full(5, 2)), (1, np.arange(1, 6), None, None, np.arange(2, 7)), (1, np.arange(1, 6), None, (2, 5), np.full((2, 5), np.arange(2, 7))), From 0c8e6cc872aed63382d3a92e27dd73ec39474ec1 Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 18:26:11 +0530 Subject: [PATCH 5/9] Exgaussian: commented broadcast_arrays line for testing in get_moment() --- pymc/distributions/continuous.py | 2 +- pymc/tests/test_distributions_moments.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 0dfb0e9293..563d184447 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2776,7 +2776,7 @@ def dist(cls, mu=0.0, sigma=None, nu=None, sd=None, *args, **kwargs): return super().dist([mu, sigma, nu], *args, **kwargs) def get_moment(rv, size, mu, sigma, nu): - mu, nu, _ = at.broadcast_arrays(mu, nu, sigma) + #mu, nu, _ = at.broadcast_arrays(mu, nu, sigma) moment = mu + nu if not rv_size_is_none(size): moment = at.full(size, moment) diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index bb8ec80e53..8da57a5873 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -495,7 +495,7 @@ def test_logistic_moment(mu, s, size, expected): "mu, nu, sigma, size, expected", [ (1, 1, None, None, 2), - (np.ones((2, 5)), 1, 1, None, np.full([2, 5], 2)), + (1, 1, np.ones((2, 5)), None, np.full([2, 5], 2)), (1, 1, None, 5, np.full(5, 2)), (1, np.arange(1, 6), None, None, np.arange(2, 7)), (1, np.arange(1, 6), None, (2, 5), np.full((2, 5), np.arange(2, 7))), From cd13f9b9a7da4a45fbe93c0ba2e5bd624d978b1d Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 19:47:50 +0530 Subject: [PATCH 6/9] ExGaussian: Uncomment broadcast_arrays() line in get_moment() to prevent failure --- 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 563d184447..0dfb0e9293 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -2776,7 +2776,7 @@ def dist(cls, mu=0.0, sigma=None, nu=None, sd=None, *args, **kwargs): return super().dist([mu, sigma, nu], *args, **kwargs) def get_moment(rv, size, mu, sigma, nu): - #mu, nu, _ = at.broadcast_arrays(mu, nu, sigma) + mu, nu, _ = at.broadcast_arrays(mu, nu, sigma) moment = mu + nu if not rv_size_is_none(size): moment = at.full(size, moment) From 80fd8b8f03020e46fcf893f8ffe8304ebc56673f Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 22:06:35 +0530 Subject: [PATCH 7/9] Exgaussian: Reolve pre-commit by removing white space --- 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 8da57a5873..1c767d69bd 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -519,4 +519,4 @@ def test_exgaussian_moment(mu, nu, sigma, size, expected): def test_geometric_moment(p, size, expected): with Model() as model: Geometric("x", p=p, size=size) - assert_moment_is_expected(model, expected) + assert_moment_is_expected(model, expected) \ No newline at end of file From b7318b784aecedd9b8aad62f11ba609b9c2763db Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 22:12:05 +0530 Subject: [PATCH 8/9] Exgaussian: Resolve pre-commit by removing white space --- pymc/tests/test_distributions_moments.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index 1c767d69bd..f06d54b11c 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -519,4 +519,5 @@ def test_exgaussian_moment(mu, nu, sigma, size, expected): def test_geometric_moment(p, size, expected): with Model() as model: Geometric("x", p=p, size=size) - assert_moment_is_expected(model, expected) \ No newline at end of file + assert_moment_is_expected(model, expected) + \ No newline at end of file From 056e9903a03e51d6225aa475ca1d37974a779f97 Mon Sep 17 00:00:00 2001 From: karm216 Date: Wed, 10 Nov 2021 22:24:25 +0530 Subject: [PATCH 9/9] Exgaussian: Resolve pre-commit by removing white space --- pymc/tests/test_distributions_moments.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index f06d54b11c..a14e966608 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -505,8 +505,8 @@ def test_exgaussian_moment(mu, nu, sigma, size, expected): with Model() as model: ExGaussian("x", mu=mu, sigma=sigma, nu=nu, size=size) assert_moment_is_expected(model, expected) - - + + @pytest.mark.parametrize( "p, size, expected", [ @@ -520,4 +520,3 @@ def test_geometric_moment(p, size, expected): with Model() as model: Geometric("x", p=p, size=size) assert_moment_is_expected(model, expected) - \ No newline at end of file