16
16
import aesara .tensor as at
17
17
import numpy as np
18
18
19
- from aesara .tensor .random .basic import bernoulli , binomial , categorical , nbinom , poisson
19
+ from aesara .tensor .random .basic import (
20
+ RandomVariable ,
21
+ bernoulli ,
22
+ binomial ,
23
+ categorical ,
24
+ nbinom ,
25
+ poisson ,
26
+ )
20
27
from scipy import stats
21
28
22
29
from pymc3 .aesaraf import floatX , intX , take_along_axis
@@ -434,6 +441,22 @@ def _distr_parameters_for_repr(self):
434
441
return ["p" ]
435
442
436
443
444
+ class DiscreteWeibullRV (RandomVariable ):
445
+ name = "discrete_weibull"
446
+ ndim_supp = 0
447
+ ndims_params = [0 , 0 ]
448
+ dtype = "int64"
449
+ _print_name = ("dWeibull" , "\\ operatorname{dWeibull}" )
450
+
451
+ @classmethod
452
+ def rng_fn (cls , rng , q , beta , size ):
453
+ p = rng .uniform (size = size )
454
+ return np .ceil (np .power (np .log (1 - p ) / np .log (q ), 1.0 / beta )) - 1
455
+
456
+
457
+ discrete_weibull = DiscreteWeibullRV ()
458
+
459
+
437
460
class DiscreteWeibull (Discrete ):
438
461
R"""Discrete Weibull log-likelihood
439
462
@@ -473,51 +496,15 @@ def DiscreteWeibull(q, b, x):
473
496
Variance :math:`2 \sum_{x = 1}^{\infty} x q^{x^{\beta}} - \mu - \mu^2`
474
497
======== ======================
475
498
"""
499
+ rv_op = discrete_weibull
476
500
477
- def __init__ (self , q , beta , * args , ** kwargs ):
478
- super ().__init__ (* args , defaults = ("median" ,), ** kwargs )
479
-
480
- self .q = at .as_tensor_variable (floatX (q ))
481
- self .beta = at .as_tensor_variable (floatX (beta ))
482
-
483
- self .median = self ._ppf (0.5 )
484
-
485
- def _ppf (self , p ):
486
- r"""
487
- The percentile point function (the inverse of the cumulative
488
- distribution function) of the discrete Weibull distribution.
489
- """
490
- q = self .q
491
- beta = self .beta
492
-
493
- return (at .ceil (at .power (at .log (1 - p ) / at .log (q ), 1.0 / beta )) - 1 ).astype ("int64" )
494
-
495
- def _random (self , q , beta , size = None ):
496
- p = np .random .uniform (size = size )
497
-
498
- return np .ceil (np .power (np .log (1 - p ) / np .log (q ), 1.0 / beta )) - 1
499
-
500
- def random (self , point = None , size = None ):
501
- r"""
502
- Draw random values from DiscreteWeibull distribution.
503
-
504
- Parameters
505
- ----------
506
- point: dict, optional
507
- Dict of variable values on which random values are to be
508
- conditioned (uses default point if not specified).
509
- size: int, optional
510
- Desired size of random sample (returns one sample if not
511
- specified).
512
-
513
- Returns
514
- -------
515
- array
516
- """
517
- # q, beta = draw_values([self.q, self.beta], point=point, size=size)
518
- # return generate_samples(self._random, q, beta, dist_shape=self.shape, size=size)
501
+ @classmethod
502
+ def dist (cls , q , beta , * args , ** kwargs ):
503
+ q = at .as_tensor_variable (floatX (q ))
504
+ beta = at .as_tensor_variable (floatX (beta ))
505
+ return super ().dist ([q , beta ], ** kwargs )
519
506
520
- def logp (self , value ):
507
+ def logp (value , q , beta ):
521
508
r"""
522
509
Calculate log-probability of DiscreteWeibull distribution at specified value.
523
510
@@ -531,8 +518,6 @@ def logp(self, value):
531
518
-------
532
519
TensorVariable
533
520
"""
534
- q = self .q
535
- beta = self .beta
536
521
return bound (
537
522
at .log (at .power (q , at .power (value , beta )) - at .power (q , at .power (value + 1 , beta ))),
538
523
0 <= value ,
@@ -541,7 +526,7 @@ def logp(self, value):
541
526
0 < beta ,
542
527
)
543
528
544
- def logcdf (self , value ):
529
+ def logcdf (value , q , beta ):
545
530
"""
546
531
Compute the log of the cumulative distribution function for Discrete Weibull distribution
547
532
at the specified value.
@@ -556,9 +541,6 @@ def logcdf(self, value):
556
541
-------
557
542
TensorVariable
558
543
"""
559
- q = self .q
560
- beta = self .beta
561
-
562
544
return bound (
563
545
at .log1p (- at .power (q , at .power (value + 1 , beta ))),
564
546
0 <= value ,
0 commit comments