Skip to content

Commit 444d66a

Browse files
Add Beta moment and tests for #5078 (#5145)
Co-authored-by: Ricardo Vieira <[email protected]>
1 parent 5f9a2e3 commit 444d66a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pymc/distributions/continuous.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,12 @@ def dist(cls, alpha=None, beta=None, mu=None, sigma=None, sd=None, *args, **kwar
11901190

11911191
return super().dist([alpha, beta], **kwargs)
11921192

1193+
def get_moment(rv, size, alpha, beta):
1194+
mean = alpha / (alpha + beta)
1195+
if not rv_size_is_none(size):
1196+
mean = at.full(size, mean)
1197+
return mean
1198+
11931199
@classmethod
11941200
def get_alpha_beta(self, alpha=None, beta=None, mu=None, sigma=None):
11951201
if (alpha is not None) and (beta is not None):

pymc/tests/test_distributions_moments.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
from pymc import Bernoulli, Flat, HalfFlat, Normal, TruncatedNormal, Uniform
5-
from pymc.distributions import HalfNormal
5+
from pymc.distributions import Beta, HalfNormal
66
from pymc.distributions.shape_utils import rv_size_is_none
77
from pymc.initial_point import make_initial_point_fn
88
from pymc.model import Model
@@ -142,3 +142,18 @@ def test_bernoulli_moment(p, size, expected):
142142
with Model() as model:
143143
Bernoulli("x", p=p, size=size)
144144
assert_moment_is_expected(model, expected)
145+
146+
147+
@pytest.mark.parametrize(
148+
"alpha, beta, size, expected",
149+
[
150+
(1, 1, None, 0.5),
151+
(1, 1, 5, np.full(5, 0.5)),
152+
(1, np.arange(1, 6), None, 1 / np.arange(2, 7)),
153+
(1, np.arange(1, 6), (2, 5), np.full((2, 5), 1 / np.arange(2, 7))),
154+
],
155+
)
156+
def test_beta_moment(alpha, beta, size, expected):
157+
with Model() as model:
158+
Beta("x", alpha=alpha, beta=beta, size=size)
159+
assert_moment_is_expected(model, expected)

0 commit comments

Comments
 (0)