pymc.DensityDist#
- class pymc.DensityDist(name, *args, **kwargs)[source]#
A distribution that can be used to wrap black-box log density functions.
Creates a Distribution and registers the supplied log density function to be used for inference. It is also possible to supply a random method in order to be able to sample from the prior or posterior predictive distributions.
- Parameters
- name
str - dist_params
Tuple A sequence of the distribution’s parameter. These will be converted into Aesara tensors internally. These parameters could be other
TensorVariableinstances created from , optionally created viaRandomVariable``Op``s.- class_name
str Name for the RandomVariable class which will wrap the DensityDist methods. When not specified, it will be given the name of the variable.
Warning
New DensityDists created with the same class_name will override the methods dispatched onto the previous classes. If using DensityDists with different methods across separate models, be sure to use distinct class_names.
- logp
Optional[Callable] A callable that calculates the log density of some given observed
valueconditioned on certain distribution parameter values. It must have the following signature:logp(value, *dist_params), wherevalueis an Aesara tensor that represents the observed value, anddist_paramsare the tensors that hold the values of the distribution parameters. This function must return an Aesara tensor. IfNone, aNotImplementederror will be raised when trying to compute the distribution’s logp.- logcdf
Optional[Callable] A callable that calculates the log cummulative probability of some given observed
valueconditioned on certain distribution parameter values. It must have the following signature:logcdf(value, *dist_params), wherevalueis an Aesara tensor that represents the observed value, anddist_paramsare the tensors that hold the values of the distribution parameters. This function must return an Aesara tensor. IfNone, aNotImplementederror will be raised when trying to compute the distribution’s logcdf.- random
Optional[Callable] A callable that can be used to generate random draws from the distribution. It must have the following signature:
random(*dist_params, rng=None, size=None). The distribution parameters are passed as positional arguments in the same order as they are supplied when theDensityDistis constructed. The keyword arguments arernd, which will provide the random variable’s associatedGenerator, andsize, that will represent the desired size of the random draw. IfNone, aNotImplementederror will be raised when trying to draw random samples from the distribution’s prior or posterior predictive.- moment
Optional[Callable] A callable that can be used to compute the moments of the distribution. It must have the following signature:
moment(rv, size, *rv_inputs). The distribution’sRandomVariableis passed as the first argumentrv.sizeis the random variable’s size implied by thedims,sizeand parameters supplied to the distribution. Finally,rv_inputsis the sequence of the distribution parameters, in the same order as they were supplied when the DensityDist was created. IfNone, a defaultmomentfunction will be assigned that will always return 0, or an array of zeros.- ndim_supp
int The number of dimensions in the support of the distribution. Defaults to assuming a scalar distribution, i.e.
ndim_supp = 0.- ndims_params
Optional[Sequence[int]] The list of number of dimensions in the support of each of the distribution’s parameters. If
None, it is assumed that all parameters are scalars, hence the number of dimensions of their support will be 0.- dtype
str The dtype of the distribution. All draws and observations passed into the distribution will be casted onto this dtype.
- kwargs
Extra keyword arguments are passed to the parent’s class
__new__method.
- name
Examples
def logp(value, mu): return -(value - mu)**2 with pm.Model(): mu = pm.Normal('mu',0,1) pm.DensityDist( 'density_dist', mu, logp=logp, observed=np.random.randn(100), ) idata = pm.sample(100)
def logp(value, mu): return -(value - mu)**2 def random(mu, rng=None, size=None): return rng.normal(loc=mu, scale=1, size=size) with pm.Model(): mu = pm.Normal('mu', 0 , 1) dens = pm.DensityDist( 'density_dist', mu, logp=logp, random=random, observed=np.random.randn(100, 3), size=(100, 3), ) prior = pm.sample_prior_predictive(10).prior_predictive['density_dist'] assert prior.shape == (1, 10, 100, 3)
Methods
DensityDist.__init__(*args, **kwargs)DensityDist.dist(*dist_params, class_name[, ...])Creates a tensor variable corresponding to the cls distribution.
DensityDist.rv_op(*dist_params, class_name, ...)