Skip to content

Commit 685a065

Browse files
author
Larry Dong
committed
Weibull RandomVariable refactoring: an example
1 parent 7aca78c commit 685a065

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

pymc3/distributions/continuous.py

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
normal,
3737
pareto,
3838
uniform,
39+
weibull,
3940
)
4041
from aesara.tensor.random.op import RandomVariable
4142
from aesara.tensor.var import TensorVariable
@@ -2690,46 +2691,23 @@ class Weibull(PositiveContinuous):
26902691
beta: float
26912692
Scale parameter (beta > 0).
26922693
"""
2694+
rv_op = weibull
26932695

2694-
def __init__(self, alpha, beta, *args, **kwargs):
2695-
super().__init__(*args, **kwargs)
2696-
self.alpha = alpha = at.as_tensor_variable(floatX(alpha))
2697-
self.beta = beta = at.as_tensor_variable(floatX(beta))
2698-
self.mean = beta * at.exp(gammaln(1 + 1.0 / alpha))
2699-
self.median = beta * at.exp(gammaln(at.log(2))) ** (1.0 / alpha)
2700-
self.variance = beta ** 2 * at.exp(gammaln(1 + 2.0 / alpha)) - self.mean ** 2
2701-
self.mode = at.switch(
2702-
alpha >= 1, beta * ((alpha - 1) / alpha) ** (1 / alpha), 0
2703-
) # Reference: https://en.wikipedia.org/wiki/Weibull_distribution
2696+
@classmethod
2697+
def dist(cls, alpha, beta, *args, **kwargs):
2698+
alpha = at.as_tensor_variable(floatX(alpha))
2699+
beta = at.as_tensor_variable(floatX(beta))
27042700

27052701
assert_negative_support(alpha, "alpha", "Weibull")
27062702
assert_negative_support(beta, "beta", "Weibull")
27072703

2708-
def random(self, point=None, size=None):
2709-
"""
2710-
Draw random values from Weibull distribution.
2711-
2712-
Parameters
2713-
----------
2714-
point: dict, optional
2715-
Dict of variable values on which random values are to be
2716-
conditioned (uses default point if not specified).
2717-
size: int, optional
2718-
Desired size of random sample (returns one sample if not
2719-
specified).
2720-
2721-
Returns
2722-
-------
2723-
array
2724-
"""
2725-
# alpha, beta = draw_values([self.alpha, self.beta], point=point, size=size)
2726-
#
2727-
# def _random(a, b, size=None):
2728-
# return b * (-np.log(np.random.uniform(size=size))) ** (1 / a)
2729-
#
2730-
# return generate_samples(_random, alpha, beta, dist_shape=self.shape, size=size)
2704+
return super().dist([alpha, beta], **kwargs)
27312705

2732-
def logp(self, value):
2706+
def logp(
2707+
value: Union[float, np.ndarray, TensorVariable],
2708+
alpha: Union[float, np.ndarray, TensorVariable],
2709+
beta: Union[float, np.ndarray, TensorVariable],
2710+
) -> RandomVariable:
27332711
"""
27342712
Calculate log-probability of Weibull distribution at specified value.
27352713
@@ -2743,8 +2721,7 @@ def logp(self, value):
27432721
-------
27442722
TensorVariable
27452723
"""
2746-
alpha = self.alpha
2747-
beta = self.beta
2724+
27482725
return bound(
27492726
at.log(alpha)
27502727
- at.log(beta)
@@ -2755,7 +2732,11 @@ def logp(self, value):
27552732
beta > 0,
27562733
)
27572734

2758-
def logcdf(self, value):
2735+
def logcdf(
2736+
value: Union[float, np.ndarray, TensorVariable],
2737+
alpha: Union[float, np.ndarray, TensorVariable],
2738+
beta: Union[float, np.ndarray, TensorVariable],
2739+
):
27592740
r"""
27602741
Compute the log of the cumulative distribution function for Weibull distribution
27612742
at the specified value.
@@ -2770,8 +2751,7 @@ def logcdf(self, value):
27702751
-------
27712752
TensorVariable
27722753
"""
2773-
alpha = self.alpha
2774-
beta = self.beta
2754+
27752755
a = (value / beta) ** alpha
27762756
return bound(
27772757
log1mexp(a),

0 commit comments

Comments
 (0)