MarginalModel#

class pymc_experimental.MarginalModel(*args, **kwargs)[source]#

Subclass of PyMC Model that implements functionality for automatic marginalization of variables in the logp transformation

After defining the full Model, the marginalize method can be used to indicate a subset of variables that should be marginalized

Notes

Marginalization functionality is still very restricted. Only finite discrete variables can be marginalized. Deterministics and Potentials cannot be conditionally dependent on the marginalized variables.

Furthermore, not all instances of such variables can be marginalized. If a variable has batched dimensions, it is required that any conditionally dependent variables use information from an individual batched dimension. In other words, the graph connecting the marginalized variable(s) to the dependent variable(s) must be composed strictly of Elemwise Operations. This is necessary to ensure an efficient logprob graph can be generated. If you want to bypass this restriction you can separate each dimension of the marginalized variable into the scalar components and then stack them together. Note that such graphs will grow exponentially in the number of marginalized variables.

For the same reason, it’s not possible to marginalize RVs with multivariate dependent RVs.

Examples

Marginalize over a single variable

import pymc as pm
from pymc_experimental import MarginalModel

with MarginalModel() as m:
    p = pm.Beta("p", 1, 1)
    x = pm.Bernoulli("x", p=p, shape=(3,))
    y = pm.Normal("y", pm.math.switch(x, -10, 10), observed=[10, 10, -10])

    m.marginalize([x])

    idata = pm.sample()
__init__(*args, **kwargs)[source]#

Methods

__init__(*args, **kwargs)

add_coord(name[, values, mutable, length])

Registers a dimension coordinate with the model.

add_coords(coords, *[, lengths])

Vectorized version of Model.add_coord.

add_named_variable(var[, dims])

Add a random graph variable to the named variables of the model.

check_start_vals(start)

Check that the starting values for MCMC do not cause the relevant log probability to evaluate to something invalid (e.g. Inf or NaN).

clone()

compile_d2logp([vars, jacobian])

Compiled log probability density hessian function.

compile_dlogp([vars, jacobian])

Compiled log probability density gradient function.

compile_fn(outs, *[, inputs, mode, point_fn])

Compiles an PyTensor function

compile_logp([vars, jacobian, sum])

Compiled log probability density function.

create_value_var(rv_var, transform[, value_var])

Create a TensorVariable that will be used as the random variable's "value" in log-likelihood graphs.

d2logp([vars, jacobian])

Hessian of the models log-probability w.r.t.

debug([point, fn, verbose])

Debug model function at point.

dlogp([vars, jacobian])

Gradient of the models log-probability w.r.t.

eval_rv_shapes()

Evaluates shapes of untransformed AND transformed free variables.

initial_point([random_seed])

Computes the initial point of the model.

logp([vars])

Elemwise log-probability of the model.

logp_dlogp_function([grad_vars, tempered])

Compile an PyTensor function that computes logp and gradient.

make_obs_var(rv_var, data, dims, transform, ...)

Create a TensorVariable for an observed random variable.

marginalize(rvs_to_marginalize)

name_for(name)

Checks if name has prefix and adds if needed

name_of(name)

Checks if name has prefix and deletes if needed

point_logps([point, round_vals])

Computes the log probability of point for all random variables in the model.

profile(outs, *[, n, point, profile])

Compiles and profiles an PyTensor function which returns outs and takes values of model vars as a dict as an argument.

recover_marginals(idata[, var_names, ...])

Computes posterior log-probabilities and samples of marginalized variables conditioned on parameters of the model given InferenceData with posterior group

register_rv(rv_var, name[, observed, ...])

Register an (un)observed random variable with the model.

replace_rvs_by_values(graphs, **kwargs)

Clone and replace random variables in graphs with their value variables.

set_data(name, values[, coords])

Changes the values of a data variable in the model.

set_dim(name, new_length[, coord_values])

Update a mutable dimension.

set_initval(rv_var, initval)

Sets an initial value (strategy) for a random variable.

shape_from_dims(dims)

to_graphviz(*[, var_names, formatting, ...])

Produce a graphviz Digraph from a PyMC model.

unmarginalize(rvs_to_unmarginalize)

update_start_vals(a, b)

Update point a with b, without overwriting existing keys.

Attributes

basic_RVs

List of random variables the model is defined in terms of (which excludes deterministics).

continuous_value_vars

All the continuous value variables in the model

coords

Coordinate values for model dimensions.

datalogp

PyTensor scalar of log-probability of the observed variables and potential terms

dim_lengths

The symbolic lengths of dimensions in the model.

discrete_value_vars

All the discrete value variables in the model

isroot

model

observedlogp

PyTensor scalar of log-probability of the observed variables

parent

potentiallogp

PyTensor scalar of log-probability of the Potential terms

prefix

root

unobserved_RVs

List of all random variables, including deterministic ones.

unobserved_value_vars

List of all random variables (including untransformed projections), as well as deterministics used as inputs and outputs of the model's log-likelihood graph

value_vars

List of unobserved random variables used as inputs to the model's log-likelihood (which excludes deterministics).

varlogp

PyTensor scalar of log-probability of the unobserved random variables (excluding deterministic).

varlogp_nojac

PyTensor scalar of log-probability of the unobserved random variables (excluding deterministic) without jacobian term.