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
namestr
dist_paramsTuple

A sequence of the distribution’s parameter. These will be converted into Aesara tensors internally. These parameters could be other TensorVariable instances created from , optionally created via RandomVariable ``Op``s.

class_namestr

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.

logpOptional[Callable]

A callable that calculates the log density of some given observed value conditioned on certain distribution parameter values. It must have the following signature: logp(value, *dist_params), where value is an Aesara tensor that represents the observed value, and dist_params are the tensors that hold the values of the distribution parameters. This function must return an Aesara tensor. If None, a NotImplemented error will be raised when trying to compute the distribution’s logp.

logcdfOptional[Callable]

A callable that calculates the log cummulative probability of some given observed value conditioned on certain distribution parameter values. It must have the following signature: logcdf(value, *dist_params), where value is an Aesara tensor that represents the observed value, and dist_params are the tensors that hold the values of the distribution parameters. This function must return an Aesara tensor. If None, a NotImplemented error will be raised when trying to compute the distribution’s logcdf.

randomOptional[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 the DensityDist is constructed. The keyword arguments are rnd, which will provide the random variable’s associated Generator, and size, that will represent the desired size of the random draw. If None, a NotImplemented error will be raised when trying to draw random samples from the distribution’s prior or posterior predictive.

momentOptional[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’s RandomVariable is passed as the first argument rv. size is the random variable’s size implied by the dims, size and parameters supplied to the distribution. Finally, rv_inputs is the sequence of the distribution parameters, in the same order as they were supplied when the DensityDist was created. If None, a default moment function will be assigned that will always return 0, or an array of zeros.

ndim_suppint

The number of dimensions in the support of the distribution. Defaults to assuming a scalar distribution, i.e. ndim_supp = 0.

ndims_paramsOptional[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.

dtypestr

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.

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, ...)