diff --git a/pymc/distributions/continuous.py b/pymc/distributions/continuous.py index 50f3f1f368..732322739e 100644 --- a/pymc/distributions/continuous.py +++ b/pymc/distributions/continuous.py @@ -3686,6 +3686,15 @@ def dist(cls, x_points, pdf_points, *args, **kwargs): return super().dist([x_points, pdf_points, cdf_points], **kwargs) + def get_moment(rv, size, x_points, pdf_points, cdf_points): + # cdf_points argument is unused + moment = at.sum(at.mul(x_points, pdf_points)) + + if not rv_size_is_none(size): + moment = at.full(size, moment) + + return moment + def logp(value, x_points, pdf_points, cdf_points): """ Calculate log-probability of Interpolated distribution at specified value. diff --git a/pymc/tests/test_distributions_moments.py b/pymc/tests/test_distributions_moments.py index b5d1b0cfd9..de374a39cd 100644 --- a/pymc/tests/test_distributions_moments.py +++ b/pymc/tests/test_distributions_moments.py @@ -33,6 +33,7 @@ HalfNormal, HalfStudentT, HyperGeometric, + Interpolated, InverseGamma, Kumaraswamy, Laplace, @@ -800,6 +801,36 @@ def test_categorical_moment(p, size, expected): assert_moment_is_expected(model, expected) +@pytest.mark.parametrize( + "x_points, pdf_points, size, expected", + [ + (np.array([-1, 1]), np.array([0.4, 0.6]), None, 0.2), + ( + np.array([-4, -1, 3, 9, 19]), + np.array([0.1, 0.15, 0.2, 0.25, 0.3]), + None, + 1.5458937198067635, + ), + ( + np.array([-22, -4, 0, 8, 13]), + np.tile(1 / 5, 5), + (5, 3), + np.full((5, 3), -0.14285714285714296), + ), + ( + np.arange(-100, 10), + np.arange(1, 111) / 6105, + (2, 5, 3), + np.full((2, 5, 3), -27.584097859327223), + ), + ], +) +def test_interpolated_moment(x_points, pdf_points, size, expected): + with Model() as model: + Interpolated("x", x_points=x_points, pdf_points=pdf_points, size=size) + assert_moment_is_expected(model, expected) + + @pytest.mark.parametrize( "mu, cov, size, expected", [