Model log-density#
|
Build the graph of the joint log-density of an Aesara graph. |
|
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 tojoint_logprob
. Here, the value variablessigma2_vv
andY_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 valuey_obs
mapped toY_rv
in the conditional log-density graphs it produces, so thatlogprob
corresponds to the density \(\operatorname{P}\left(\sigma^2 \mid Y=y\right)\) when \(y\) and \(Y\) correspond toy_obs
andY_rv
, respectively.- Parameters:
- Return type:
- 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 ifrandom_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)\) andlogprobs[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 toconditional_logprob
. Here, the value variablessigma2_vv
andY_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
) – Alist
of random variables for which we need to return a conditional log-probability graph.realized (
Dict
[TensorVariable
,TensorVariable
]) – Adict
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 ifrandom_variables
is empty.