Skip to content

Commit c12ece6

Browse files
committed
Add Weibull moment
1 parent d944333 commit c12ece6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pymc/distributions/continuous.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,12 @@ def dist(cls, alpha, beta, *args, **kwargs):
25142514

25152515
return super().dist([alpha, beta], *args, **kwargs)
25162516

2517+
def get_moment(rv, size, alpha, beta):
2518+
mean = beta * at.gamma(1 + 1 / alpha)
2519+
if not rv_size_is_none(size):
2520+
mean = at.full(size, mean)
2521+
return mean
2522+
25172523
def logcdf(value, alpha, beta):
25182524
r"""
25192525
Compute the log of the cumulative distribution function for Weibull distribution

pymc/tests/test_distributions_moments.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from scipy import special
5+
46
from pymc import Bernoulli, Flat, HalfFlat, Normal, TruncatedNormal, Uniform
57
from pymc.distributions import (
68
Beta,
@@ -13,6 +15,7 @@
1315
Laplace,
1416
LogNormal,
1517
StudentT,
18+
Weibull,
1619
)
1720
from pymc.distributions.shape_utils import rv_size_is_none
1821
from pymc.initial_point import make_initial_point_fn
@@ -303,3 +306,23 @@ def test_gamma_moment(alpha, beta, size, expected):
303306
with Model() as model:
304307
Gamma("x", alpha=alpha, beta=beta, size=size)
305308
assert_moment_is_expected(model, expected)
309+
310+
311+
@pytest.mark.parametrize(
312+
"alpha, beta, size, expected",
313+
[
314+
(1, 1, None, 1),
315+
(1, 1, 5, np.full(5, 1)),
316+
(np.arange(1, 6), 1, None, special.gamma(1 + 1 / np.arange(1, 6))),
317+
(
318+
np.arange(1, 6),
319+
np.arange(2, 7),
320+
(2, 5),
321+
np.full((2, 5), np.arange(2, 7) * special.gamma(1 + 1 / np.arange(1, 6))),
322+
),
323+
],
324+
)
325+
def test_weibull_moment(alpha, beta, size, expected):
326+
with Model() as model:
327+
Weibull("x", alpha=alpha, beta=beta, size=size)
328+
assert_moment_is_expected(model, expected)

0 commit comments

Comments
 (0)