aeppl package¶
Submodules¶
aeppl.abstract module¶
- class aeppl.abstract.MeasurableVariable¶
Bases:
abc.ABC
A variable that can be assigned a measure/log-probability
- aeppl.abstract.get_measurable_outputs(op, node)¶
- aeppl.abstract.get_measurable_outputs(op: aesara.tensor.random.op.RandomVariable, node)
Return only the outputs that are measurable.
- aeppl.abstract.randomvariable_moutputs(op, node)¶
aeppl.joint_logprob module¶
- aeppl.joint_logprob.joint_logprob(vars: Union[aesara.tensor.var.TensorVariable, Iterable[aesara.tensor.var.TensorVariable]], rv_values: Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable], warn_missing_rvs: bool = True, extra_rewrites: Optional[Union[aesara.graph.opt.GlobalOptimizer, aesara.graph.opt.LocalOptimizer]] = None, **kwargs) aesara.tensor.var.TensorVariable ¶
Create a graph representing the joint log-probability/measure of a graph.
The input var determines which graph is used and rv_values specifies the resulting measure-space graph’s input parameters.
For example, consider the following
import aesara.tensor as at Y_rv = at.random.normal(0, at.sqrt(sigma2_rv)) sigma2_rv = at.random.invgamma(0.5, 0.5)
This graph for
Y_rv
is equivalent to the following hierarchical model:\[\begin{split}Y \sim& \operatorname{N}(0, \sigma^2) \\ \sigma^2 \sim& \operatorname{InvGamma}(0.5, 0.5)\end{split}\]If we create a value variable for
Y_rv
, i.e.y = at.scalar("y")
, the graph ofjoint_logprob(Y_rv, {Y_rv: y})
is equivalent to the conditional probability \(\log p(Y = y \mid \sigma^2)\). If we specify a value variable forsigma2_rv
, i.e.s = at.scalar("s2")
, thenjoint_logprob(Y_rv, {Y_rv: y, sigma2_rv: s})
yields the joint log-probability\[\log p(Y = y, \sigma^2 = s) = \log p(Y = y \mid \sigma^2 = s) + \log p(\sigma^2 = s)\]- vars
The graphs containing the stochastic/RandomVariable elements for which we want to compute a joint log-probability. This graph effectively represents a statistical model. When multiple elements are given, their log-probabilities are summed, producing their joint log-probability.
- rv_values
A
dict
of variables that maps stochastic elements (e.g. RandomVariables) to symbolic Variables representing their values in a log-probability.- warn_missing_rvs
When
True
, issue a warning when a RandomVariable is found in the graph and doesn’t have a corresponding value variable specified in rv_values.- extra_rewrites
Extra rewrites to be applied (e.g. reparameterizations, transforms, etc.)
aeppl.logprob module¶
- aeppl.logprob.bernoulli_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.beta_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.betabinomial_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.betaln(x, y)¶
- aeppl.logprob.binomial_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.binomln(n, k)¶
- aeppl.logprob.categorical_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.cauchy_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.chisquare_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.dirichlet_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.exponential_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.gamma_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.geometric_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.gumbel_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.halfcauchy_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.halfnormal_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.hypergeometric_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.invgamma_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.laplace_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.logistic_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.lognormal_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.logprob(rv_var, rv_value, **kwargs)¶
Create a graph for the log-probability of a
RandomVariable
.
- aeppl.logprob.multinomial_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.mvnormal_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.nbinom_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.normal_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.pareto_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.poisson_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.triangular_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.uniform_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.vonmises_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.wald_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.weibull_logprob(op, value, *inputs, **kwargs)¶
- aeppl.logprob.xlogy0(m, x)¶
aeppl.mixture module¶
- class aeppl.mixture.MixtureRV(inputs, outputs, inline=False, lop_overrides='default', grad_overrides='default', rop_overrides='default', connection_pattern=None, name=None, **kwargs)¶
Bases:
aesara.compile.builders.OpFromGraph
A placeholder used to specify a log-likelihood for a mixture sub-graph.
- classmethod create_node(node, indices, mixture_rvs)¶
- aeppl.mixture.get_stack_mixture_vars(node: aesara.graph.basic.Apply) Optional[List[aesara.tensor.var.TensorVariable]] ¶
Extract the mixture terms from a *Subtensor* applied to stacked RandomVariables.
- aeppl.mixture.logprob_MixtureRV(op, value, *inputs, name=None, **kwargs)¶
- aeppl.mixture.rv_pull_down(x: aesara.tensor.var.TensorVariable, dont_touch_vars=None) aesara.tensor.var.TensorVariable ¶
Pull a
RandomVariable
Op
down through a graph, when possible.
aeppl.opt module¶
- class aeppl.opt.PreserveRVMappings(rv_values: Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable])¶
Bases:
aesara.graph.features.Feature
Keep track of random variable replacements in a map.
When a Variable that is replaced by optimizations, this Feature updates the key entries in a map to reflect the new transformed Variables.
- rv_values
Mappings between random variables and their value variables. The keys of this map are what this Feature keeps updated. The
dict
is updated in-place.
- on_attach(fgraph)¶
Called by FunctionGraph.attach_feature, the method that attaches the feature to the FunctionGraph. Since this is called after the FunctionGraph is initially populated, this is where you should run checks on the initial contents of the FunctionGraph.
The on_attach method may raise the AlreadyThere exception to cancel the attach operation if it detects that another Feature instance implementing the same functionality is already attached to the FunctionGraph.
The feature has great freedom in what it can do with the fgraph: it may, for example, add methods to it dynamically.
- on_change_input(fgraph, node, i, r, new_r, reason=None)¶
Called whenever
node.inputs[i]
is changed from var to new_var. At the moment the callback is done, the change has already taken place.If you raise an exception in this function, the state of the graph might be broken for all intents and purposes.
aeppl.printing module¶
- class aeppl.printing.GenericSubtensorPrinter¶
Bases:
object
- process(r: aesara.graph.basic.Variable, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
- class aeppl.printing.NormalRVPrinter¶
Bases:
aeppl.printing.RandomVariablePrinter
Create a RandomVariablePrinter.
- name: str (optional)
A fixed name to use for the random variables printed by this printer. If not specified, use RandomVariable.name.
- process_param(idx, sform, pstate)¶
Perform special per-parameter post-formatting.
This can be used, for instance, to change a std. dev. into a variance.
- idx: int
The index value of the parameter.
- sform: str
The pre-formatted string form of the parameter.
- pstate: object
The printer state.
- class aeppl.printing.PreamblePPrinter(*args, pstate_defaults: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]] = None, preamble_dict: Optional[collections.abc.Mapping] = None, **kwargs)¶
Bases:
aesara.printing.PPrinter
Pretty printer that displays a preamble.
Preambles are put into an OrderedDict of categories (determined by printers that use the preamble). The order can be set by preempting the category names within an OrderedDict passed to the constructor via the preamble_dict keyword. The lines accumulated in each category are comma-separated up to a fixed length given by PreamblePPrinter.max_preamble_width, after which a newline is appended and process repeats.
>>> import aesara.tensor as at >>> from aeppl.printing import pprint >>> X_rv = at.random.normal(at.scalar('\\mu'), at.scalar('\\sigma'), name='X') >>> print(pprint(X_rv)) \\mu in R \\sigma in R X ~ N(\\mu, \\sigma**2), X in R X
XXX: Not thread-safe!
Create a PreamblePPrinter.
- pstate_defaults: dict (optional)
Default printer state parameters.
- preamble_dict: OrderedDict (optional)
Default preamble dictionary. Use this to pre-set the print-out ordering of preamble categories/keys.
- create_state(pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
- max_preamble_width = 40¶
- process(r: aesara.graph.basic.Variable, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]] = None)¶
- process_graph(inputs, outputs, updates=None, display_inputs=False)¶
- class aeppl.printing.RandomVariablePrinter(name: Optional[str] = None)¶
Bases:
object
Pretty print random variables.
Ops are able to specify their ascii and LaTeX formats via a “print_name” property. Op.print_name should be a tuple or list that specifies the plain text/ascii and LaTeX name, respectively.
Also, distribution parameters can be formatted distinctly by overriding the RandomVariablePrinter.process_param method.
Create a RandomVariablePrinter.
- name: str (optional)
A fixed name to use for the random variables printed by this printer. If not specified, use RandomVariable.name.
- process(output, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
- process_param(idx: int, sform: str, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
Perform special per-parameter post-formatting.
This can be used, for instance, to change a std. dev. into a variance.
- idx: int
The index value of the parameter.
- sform: str
The pre-formatted string form of the parameter.
- pstate: object
The printer state.
- class aeppl.printing.VariableWithShapePrinter¶
Bases:
object
Print variable shape info in the preamble.
Also uses readable character names for un-named variables.
Constant arrays are only printed when their size is below a threshold set by
max_line_width * max_line_height
- available_names = {'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None, 'G': None, 'H': None, 'I': None, 'J': None, 'K': None, 'L': None, 'M': None, 'N': None, 'O': None, 'P': None, 'Q': None, 'R': None, 'S': None, 'T': None, 'U': None, 'V': None, 'W': None, 'X': None, 'Y': None, 'Z': None, 'a': None, 'b': None, 'c': None, 'd': None, 'e': None, 'f': None, 'g': None, 'h': None, 'i': None, 'j': None, 'k': None, 'l': None, 'm': None, 'n': None, 'o': None, 'p': None, 'q': None, 'r': None, 's': None, 't': None, 'u': None, 'v': None, 'w': None, 'x': None, 'y': None, 'z': None}¶
- default_printer = <aesara.printing.DefaultPrinter object>¶
- max_line_height = 20¶
- max_line_width = 40¶
- classmethod process(output: aesara.graph.basic.Variable, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
- classmethod process_shape_info(output: aesara.graph.basic.Variable, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
- classmethod process_variable_name(output: aesara.graph.basic.Variable, pstate: Optional[Union[collections.abc.MutableMapping, aesara.printing.PrinterState]])¶
Take a variable name from the available ones.
This function also initializes the available names by removing all the manually specified names within the FunctionGraph being printed (if available). Doing so removes the potential for name collisions.
- aeppl.printing.latex_print_array(data)¶
aeppl.transforms module¶
- class aeppl.transforms.CircularTransform¶
Bases:
aeppl.transforms.RVTransform
- backward(value, *inputs)¶
Invert the transformation.
- forward(value, *inputs)¶
Apply the transformation.
- log_jac_det(value, *inputs)¶
Construct the log of the absolute value of the Jacobian determinant.
- name = 'circular'¶
- class aeppl.transforms.DefaultTransformSentinel¶
Bases:
object
- class aeppl.transforms.DistributionMeta(name, bases, clsdict)¶
Bases:
aesara.graph.utils.MetaType
- class aeppl.transforms.IntervalTransform(args_fn)¶
Bases:
aeppl.transforms.RVTransform
- backward(value, *inputs)¶
Invert the transformation.
- forward(value, *inputs)¶
Apply the transformation.
- log_jac_det(value, *inputs)¶
Construct the log of the absolute value of the Jacobian determinant.
- name = 'interval'¶
- class aeppl.transforms.LogOddsTransform¶
Bases:
aeppl.transforms.RVTransform
- backward(value, *inputs)¶
Invert the transformation.
- forward(value, *inputs)¶
Apply the transformation.
- log_jac_det(value, *inputs)¶
Construct the log of the absolute value of the Jacobian determinant.
- name = 'logodds'¶
- class aeppl.transforms.LogTransform¶
Bases:
aeppl.transforms.RVTransform
- backward(value, *inputs)¶
Invert the transformation.
- forward(value, *inputs)¶
Apply the transformation.
- log_jac_det(value, *inputs)¶
Construct the log of the absolute value of the Jacobian determinant.
- name = 'log'¶
- class aeppl.transforms.RVTransform¶
Bases:
abc.ABC
- abstract backward(value: aesara.tensor.var.TensorVariable, *inputs: aesara.graph.basic.Variable) aesara.tensor.var.TensorVariable ¶
Invert the transformation.
- abstract forward(value: aesara.tensor.var.TensorVariable, *inputs: aesara.graph.basic.Variable) aesara.tensor.var.TensorVariable ¶
Apply the transformation.
- log_jac_det(value: aesara.tensor.var.TensorVariable, *inputs) aesara.tensor.var.TensorVariable ¶
Construct the log of the absolute value of the Jacobian determinant.
- class aeppl.transforms.StickBreaking¶
Bases:
aeppl.transforms.RVTransform
- backward(value, *inputs)¶
Invert the transformation.
- forward(value, *inputs)¶
Apply the transformation.
- log_jac_det(value, *inputs)¶
Construct the log of the absolute value of the Jacobian determinant.
- name = 'stickbreaking'¶
- class aeppl.transforms.TransformValuesMapping(values_to_transforms)¶
Bases:
aesara.graph.features.Feature
A Feature that maintains a map between value variables and their transforms.
- on_attach(fgraph)¶
Called by FunctionGraph.attach_feature, the method that attaches the feature to the FunctionGraph. Since this is called after the FunctionGraph is initially populated, this is where you should run checks on the initial contents of the FunctionGraph.
The on_attach method may raise the AlreadyThere exception to cancel the attach operation if it detects that another Feature instance implementing the same functionality is already attached to the FunctionGraph.
The feature has great freedom in what it can do with the fgraph: it may, for example, add methods to it dynamically.
- class aeppl.transforms.TransformValuesOpt(values_to_transforms: Dict[aesara.tensor.var.TensorVariable, Optional[Union[aeppl.transforms.RVTransform, aeppl.transforms.DefaultTransformSentinel]]])¶
Bases:
aesara.graph.opt.GlobalOptimizer
Transforms value variables according to a map and/or per-RandomVariable defaults.
- values_to_transforms
Mapping between value variables and their transformations. Each value variable can be assigned one of RVTransform,
DEFAULT_TRANSFORM
, orNone
. If a transform is not specified for a specific value variable it will not be transformed.
- add_requirements(fgraph)¶
Add features to fgraph that are required to apply the optimization.
For example:
fgraph.attach_feature(History()) fgraph.attach_feature(MyFeature()) # etc.
- apply(fgraph: aesara.graph.fg.FunctionGraph)¶
Apply the optimization to a FunctionGraph.
It may use all the methods defined by the FunctionGraph. If the GlobalOptimizer needs to use a certain tool, such as an InstanceFinder, it can do so in its add_requirements method.
- default_transform_opt = <aesara.graph.opt.TopoOptimizer object>¶
- class aeppl.transforms.TransformedRV(name=None, ndim_supp=None, ndims_params=None, dtype=None, inplace=None)¶
Bases:
aesara.tensor.random.op.RandomVariable
A base class for transformed RandomVariables.
Create a random variable Op.
- name: str
The Op’s display name.
- ndim_supp: int
Total number of dimensions for a single draw of the random variable (e.g. a multivariate normal draw is 1D, so
ndim_supp = 1
).- ndims_params: list of int
Number of dimensions for each distribution parameter when the parameters only specify a single drawn of the random variable (e.g. a multivariate normal’s mean is 1D and covariance is 2D, so
ndims_params = [1, 2]
).- dtype: str (optional)
The dtype of the sampled output. If the value
"floatX"
is given, thendtype
is set toaesara.config.floatX
. IfNone
(the default), the dtype keyword must be set when RandomVariable.make_node is called.- inplace: boolean (optional)
Determine whether or not the underlying rng state is updated in-place or not (i.e. copied).
- aeppl.transforms.create_default_transformed_rv_op(rv_op: aesara.graph.op.Op, transform: aeppl.transforms.RVTransform, *, default: bool = True, cls_dict_extra: Optional[Dict] = None) Type[aeppl.transforms.TransformedRV] ¶
Create a new TransformedRV given a base RandomVariable Op
- rv_op
The RandomVariable for which we want to construct a TransformedRV.
- transform
The RVTransform for rv_op.
- default
If
False
do not make transform the default transform for rv_op.- cls_dict_extra
Additional class members to add to the constructed TransformedRV.
- aeppl.transforms.transformed_logprob(op, value, *inputs, name=None, **kwargs)¶
Compute the log-likelihood graph for a TransformedRV.
We assume that the value variable was back-transformed to be on the natural support of the respective random variable.
aeppl.utils module¶
- aeppl.utils.change_rv_size(rv_var: aesara.tensor.var.TensorVariable, new_size: Union[int, numpy.ndarray, Tuple[Union[int, aesara.graph.basic.Variable], ...], List[Union[int, aesara.graph.basic.Variable]], aesara.graph.basic.Variable], expand: Optional[bool] = False) aesara.tensor.var.TensorVariable ¶
Change or expand the size of a RandomVariable.
- rv_var
The RandomVariable output.
- new_size
The new size.
- expand:
Expand the existing size by new_size.
- aeppl.utils.convert_indices(indices, entry)¶
- aeppl.utils.extract_obs_data(x: aesara.tensor.var.TensorVariable) numpy.ndarray ¶
Extract data from observed symbolic variables.
TypeError
- aeppl.utils.extract_rv_and_value_vars(var: aesara.tensor.var.TensorVariable) Tuple[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable] ¶
Return a random variable and its observations, value variable, or
None
.- var
A variable corresponding to a
MeasurableVariable
.
The first value in the tuple is the
MeasurableVariable
and the second is the observations variable or the measure/log-probability value variable.
- aeppl.utils.get_constant_value(x)¶
Use constant folding to get a constant value.
- aeppl.utils.indices_from_subtensor(idx_list, indices)¶
Compute a useable index tuple from the inputs of a *Subtensor** Op.
- aeppl.utils.replace_rvs_in_graphs(graphs: Iterable[aesara.tensor.var.TensorVariable], replacement_fn: Callable[[aesara.tensor.var.TensorVariable, Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable]], Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable]], initial_replacements: Optional[Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable]] = None, **kwargs) Tuple[aesara.tensor.var.TensorVariable, Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable]] ¶
Replace random variables in graphs.
This will not recompute test values.
- graphs
The graphs in which random variables are to be replaced.
A
tuple
containing the transformed graphs and adict
of the replacements that were made.
- aeppl.utils.rvs_to_value_vars(graphs: Iterable[aesara.tensor.var.TensorVariable], initial_replacements: Optional[Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable]] = None, **kwargs) Tuple[aesara.tensor.var.TensorVariable, Dict[aesara.tensor.var.TensorVariable, aesara.tensor.var.TensorVariable]] ¶
Replace random variables in graphs with their value variables.
This will not recompute test values in the resulting graphs.
- graphs
The graphs in which to perform the replacements.
- initial_replacements
A
dict
containing the initial replacements to be made.
- aeppl.utils.walk_model(graphs: Iterable[aesara.tensor.var.TensorVariable], walk_past_rvs: bool = False, stop_at_vars: Optional[Set[aesara.tensor.var.TensorVariable]] = None, expand_fn: Callable[[aesara.tensor.var.TensorVariable], List[aesara.tensor.var.TensorVariable]] = <function <lambda>>) Generator[aesara.tensor.var.TensorVariable, None, None] ¶
Walk model graphs and yield their nodes.
By default, these walks will not go past MeasurableVariable nodes.
- graphs
The graphs to walk.
- walk_past_rvs
If
True
, the walk will not terminate at MeasurableVariables.- stop_at_vars
A list of variables at which the walk will terminate.
- expand_fn
A function that returns the next variable(s) to be traversed.