58
58
from pymc .vartypes import string_types
59
59
60
60
__all__ = [
61
- "DensityDistRV " ,
61
+ "CustomDist " ,
62
62
"DensityDist" ,
63
63
"Distribution" ,
64
64
"Continuous" ,
@@ -456,15 +456,15 @@ class Continuous(Distribution):
456
456
"""Base class for continuous distributions"""
457
457
458
458
459
- class DensityDistRV (RandomVariable ):
459
+ class CustomDistRV (RandomVariable ):
460
460
"""
461
- Base class for DensityDistRV
461
+ Base class for CustomDistRV
462
462
463
- This should be subclassed when defining custom DensityDist objects.
463
+ This should be subclassed when defining CustomDist objects.
464
464
"""
465
465
466
- name = "DensityDistRV "
467
- _print_name = ("DensityDist " , "\\ operatorname{DensityDist }" )
466
+ name = "CustomDistRV "
467
+ _print_name = ("CustomDist " , "\\ operatorname{CustomDist }" )
468
468
469
469
@classmethod
470
470
def rng_fn (cls , rng , * args ):
@@ -473,7 +473,7 @@ def rng_fn(cls, rng, *args):
473
473
return cls ._random_fn (* args , rng = rng , size = size )
474
474
475
475
476
- class DensityDist (Distribution ):
476
+ class CustomDist (Distribution ):
477
477
"""A distribution that can be used to wrap black-box log density functions.
478
478
479
479
Creates a Distribution and registers the supplied log density function to be used
@@ -489,11 +489,11 @@ class DensityDist(Distribution):
489
489
PyTensor tensors internally. These parameters could be other ``TensorVariable``
490
490
instances created from , optionally created via ``RandomVariable`` ``Op``s.
491
491
class_name : str
492
- Name for the RandomVariable class which will wrap the DensityDist methods.
492
+ Name for the RandomVariable class which will wrap the CustomDist methods.
493
493
When not specified, it will be given the name of the variable.
494
494
495
- .. warning:: New DensityDists created with the same class_name will override the
496
- methods dispatched onto the previous classes. If using DensityDists with
495
+ .. warning:: New CustomDists created with the same class_name will override the
496
+ methods dispatched onto the previous classes. If using CustomDists with
497
497
different methods across separate models, be sure to use distinct
498
498
class_names.
499
499
@@ -517,7 +517,7 @@ class DensityDist(Distribution):
517
517
A callable that can be used to generate random draws from the distribution.
518
518
It must have the following signature: ``random(*dist_params, rng=None, size=None)``.
519
519
The distribution parameters are passed as positional arguments in the
520
- same order as they are supplied when the ``DensityDist `` is constructed.
520
+ same order as they are supplied when the ``CustomDist `` is constructed.
521
521
The keyword arguments are ``rnd``, which will provide the random variable's
522
522
associated :py:class:`~numpy.random.Generator`, and ``size``, that will represent
523
523
the desired size of the random draw. If ``None``, a ``NotImplemented``
@@ -530,7 +530,7 @@ class DensityDist(Distribution):
530
530
as the first argument ``rv``. ``size`` is the random variable's size implied
531
531
by the ``dims``, ``size`` and parameters supplied to the distribution. Finally,
532
532
``rv_inputs`` is the sequence of the distribution parameters, in the same order
533
- as they were supplied when the DensityDist was created. If ``None``, a default
533
+ as they were supplied when the CustomDist was created. If ``None``, a default
534
534
``moment`` function will be assigned that will always return 0, or an array
535
535
of zeros.
536
536
ndim_supp : int
@@ -555,8 +555,8 @@ def logp(value, mu):
555
555
556
556
with pm.Model():
557
557
mu = pm.Normal('mu',0,1)
558
- pm.DensityDist (
559
- 'density_dist ',
558
+ pm.CustomDist (
559
+ 'custom_dist ',
560
560
mu,
561
561
logp=logp,
562
562
observed=np.random.randn(100),
@@ -573,20 +573,19 @@ def random(mu, rng=None, size=None):
573
573
574
574
with pm.Model():
575
575
mu = pm.Normal('mu', 0 , 1)
576
- dens = pm.DensityDist (
577
- 'density_dist ',
576
+ pm.CustomDist (
577
+ 'custom_dist ',
578
578
mu,
579
579
logp=logp,
580
580
random=random,
581
581
observed=np.random.randn(100, 3),
582
582
size=(100, 3),
583
583
)
584
- prior = pm.sample_prior_predictive(10).prior_predictive['density_dist']
585
- assert prior.shape == (1, 10, 100, 3)
584
+ prior = pm.sample_prior_predictive(10)
586
585
587
586
"""
588
587
589
- rv_type = DensityDistRV
588
+ rv_type = CustomDistRV
590
589
591
590
def __new__ (cls , name , * args , ** kwargs ):
592
591
kwargs .setdefault ("class_name" , name )
@@ -676,15 +675,15 @@ def rv_op(
676
675
** kwargs ,
677
676
):
678
677
rv_op = type (
679
- f"DensityDist_ { class_name } " ,
680
- (DensityDistRV ,),
678
+ f"CustomDist_ { class_name } " ,
679
+ (CustomDistRV ,),
681
680
dict (
682
- name = f"DensityDist_ { class_name } " ,
681
+ name = f"CustomDist_ { class_name } " ,
683
682
inplace = False ,
684
683
ndim_supp = ndim_supp ,
685
684
ndims_params = ndims_params ,
686
685
dtype = dtype ,
687
- # Specifc to DensityDist
686
+ # Specifc to CustomDist
688
687
_random_fn = random ,
689
688
),
690
689
)()
@@ -710,9 +709,12 @@ def density_dist_get_moment(op, rv, rng, size, dtype, *dist_params):
710
709
return rv_op (* dist_params , ** kwargs )
711
710
712
711
712
+ DensityDist = CustomDist
713
+
714
+
713
715
def default_not_implemented (rv_name , method_name ):
714
716
message = (
715
- f"Attempted to run { method_name } on the DensityDist '{ rv_name } ', "
717
+ f"Attempted to run { method_name } on the CustomDist '{ rv_name } ', "
716
718
f"but this method had not been provided when the distribution was "
717
719
f"constructed. Please re-build your model and provide a callable "
718
720
f"to '{ rv_name } 's { method_name } keyword argument.\n "
0 commit comments