# Model log-density#

 joint_logprob Build the graph of the joint log-density of an Aesara graph. conditional_logprob Create a map between random variables and their conditional log-probabilities.

AePPL produces log-density graphs from Aesara graphs containing random variables (i.e. model graphs). The function aeppl.joint_logprob is the main entry-point for this functionality.

Not all Aesara model graphs are currently supported, but AePPL takes a very generalizable approach to producing log-density graphs, so support for mathematically well-defined models via basic Aesara operations is expected. If you find a model graph that isn’t supported, feel free to create a Discussion or issue.

A list of supported random variables can be found in Random variables and the list of supported operators in Transforms and Scan.

In some applications (like Gibbs sampling) the graphs that represent each individual conditional log-density may be needed; AePPL can generate such graphs via aeppl.conditional_logprob.

## Joint log-density#

aeppl.joint_logprob.joint_logprob(*random_variables, realized={}, **kwargs)[source]#

Build the graph of the joint log-density of an Aesara graph.

Consider the following Aesara model:

import aesara.tensor as at

srng = at.random.RandomStream()
sigma2_rv = srng.invgamma(0.5, 0.5)
Y_rv = srng.normal(0, at.sqrt(sigma2_rv))


Which represents the following mathematical model:

$\begin{split}\sigma^2 \sim& \operatorname{InvGamma}(0.5, 0.5) \\ Y \sim& \operatorname{N}\left(0, \sigma^2\right)\end{split}$

We can generate the graph that computes the joint log-density associated with this model:

import aeppl

logprob, (sigma2_vv, Y_vv) = aeppl.joint_logprob(sigma2_rv, Y_rv)


To build the joint log-density graph, joint_logprob must generate the value variable associated with each random variable. They are returned along with the graph in the same order as the random variables were passed to joint_logprob. Here, the value variables sigma2_vv and Y_vv correspond to the values $$s$$ and $$y$$ taken by $$\sigma^2$$ and $$Y$$, respectively.

It is also possible to call joint_logprob omitting some of the random variables in the graph:

logprob, (Y_vv,) = aeppl.joint_logprob(Y_rv)


In this case, logprob corresponds to the joint log-density $$\operatorname{P}\left(Y, \sigma^2\right)$$ where $$\sigma^2$$ is a stochastic variable.

Another important case is when one of the variables is already realized. For instance, if Y_rv is observed we can include its realized value directly in the log-density graphs:

y_obs = Y_rv.copy()
logprob, (sigma2_vv,) = aeppl.joint_logprob(sigma2_rv, realized={Y_rv: y_obs})


In this case, joint_logprob uses the value y_obs mapped to Y_rv in the conditional log-density graphs it produces, so that logprob corresponds to the density $$\operatorname{P}\left(\sigma^2 \mid Y=y\right)$$ when $$y$$ and $$Y$$ correspond to y_obs and Y_rv, respectively.

Parameters:
• random_variables (List[TensorVariable]) – A list of random variables for which we need to return a conditional log-probability graph.

• realized (Dict[TensorVariable, TensorVariable]) – A dict that maps random variables to their realized value.

Return type:

Optional[Tuple[TensorVariable, Tuple[TensorVariable, ...]]]

Returns:

• logprob – A TensorVariable that represents the joint log-probability of the graph implicitly defined by the random variables passed as arguments. None if a log-density cannot be computed.

• value_variables – A list of the created valued variables in the same order as the order in which their corresponding random variables were passed as arguments. Empty if random_variables is empty.

## Conditional log-densities#

aeppl.joint_logprob.conditional_logprob(*random_variables, realized={}, ir_rewriter=None, extra_rewrites=None, **kwargs)[source]#

Create a map between random variables and their conditional log-probabilities.

Consider the following Aesara model:

import aesara.tensor as at

srng = at.random.RandomStream()

sigma2_rv = srng.invgamma(0.5, 0.5)
Y_rv = srng.normal(0, at.sqrt(sigma2_rv))


Which represents the following mathematical model:

$\begin{split}\sigma^2 \sim& \operatorname{InvGamma}(0.5, 0.5) \\ Y \sim& \operatorname{N}\left(0, \sigma^2\right)\end{split}$

We can generate the graph that computes the conditional log-density associated with each random variable with:

import aeppl

logprobs, (sigma2_vv, Y_vv) = aeppl.conditional_logprob(sigma2_rv, Y_rv)


The list of random variables passed to conditional_logprob implicitly defines a joint density that factorized according to the graphical model represented by the Aesara model. Here, logprobs[sigma2_rv] corresponds to the conditional log-density $$\operatorname{P}\left(sigma^2=s \mid Y\right)$$ and logprobs[Y_rv] to $$\operatorname{P}\left(Y=y \mid \sigma^2\right)$$.

To build the log-density graphs, conditional_logprob must generate the value variable associated with each random variable. They are returned along with the graph in the same order as the random variables were passed to conditional_logprob. Here, the value variables sigma2_vv and Y_vv correspond to $$s$$ and $$y$$ in the previous expressions respectively.

It is also possible to call conditional_logprob omitting some of the random variables in the graph:

logprobs, (Y_vv,) = aeppl.conditional_logprob(Y_rv)


In this case, logprobs[Y_rv] corresponds to the conditional log-density $$\operatorname{P}\left(Y=y \mid \sigma^2\right)$$ where $$\sigma^2$$ is a stochastic variable.

Another important case is when one the variables is already realized. For instance, if Y_rv is observed we can include its realized value directly in the log-density graphs:

y_obs = Y_rv.copy()
logprobs, (sigma2_vv,) = aeppl.conditional_logprob(sigma2_rv, realized={Y_rv: y_obs})


In this case, conditional_logprob uses the value variable passed in the conditional log-density graphs it produces.

Parameters:
• random_variables (TensorVariable) – A list of random variables for which we need to return a conditional log-probability graph.

• realized (Dict[TensorVariable, TensorVariable]) – A dict that maps realized random variables to their realized values. These values used in the generated conditional log-density graphs.

• ir_rewriter (Optional[GraphRewriter]) – Rewriter that produces the intermediate representation of measurable variables.

• extra_rewrites (Union[GraphRewriter, NodeRewriter, None]) – Extra rewrites to be applied (e.g. reparameterizations, transforms, etc.)

Return type:

Tuple[Dict[TensorVariable, TensorVariable], Tuple[TensorVariable, ...]]

Returns:

• conditional_logprobs – A dict that maps each random variable to the graph that computes their conditional log-density implicitly defined by the random variables passed as arguments. None if a log-density cannot be computed.

• value_variables – A list of the created valued variables in the same order as the order in which their corresponding random variables were passed as arguments. Empty if random_variables is empty.