Skip to main content

PPL tools for Aesara

Project description

Tests Status Coverage Join the chat at https://gitter.im/aesara-devs/aeppl

aeppl provides tools for a[e]PPL written in Aesara.

Features

  • Convert graphs containing Aesara RandomVariables into joint log-probability graphs

  • Transforms for RandomVariables that map constrained support spaces to unconstrained spaces (e.g. the extended real numbers), and a rewrite that automatically applies these transformations throughout a graph

  • Tools for traversing and transforming graphs containing RandomVariables

  • RandomVariable-aware pretty printing and LaTeX output

Examples

Using aeppl, one can create a joint log-probability graph from a graph containing Aesara RandomVariables:

import aesara
from aesara import tensor as at

from aeppl import joint_logprob, pprint


# A simple scale mixture model
S_rv = at.random.invgamma(0.5, 0.5)
Y_rv = at.random.normal(0.0, at.sqrt(S_rv))

# Compute the joint log-probability
y = at.scalar("y")
s = at.scalar("s")
logprob = joint_logprob({Y_rv: y, S_rv: s})

Log-probability graphs are standard Aesara graphs, so we can compute values with them:

logprob_fn = aesara.function([y, s], logprob)

logprob_fn(-0.5, 1.0)
# array(-2.46287705)

Graphs can also be pretty printed:

from aeppl import pprint, latex_pprint


# Print the original graph
print(pprint(Y_rv))
# b ~ invgamma(0.5, 0.5) in R, a ~ N(0.0, sqrt(b)**2) in R
# a

print(latex_pprint(Y_rv))
# \begin{equation}
#   \begin{gathered}
#     b \sim \operatorname{invgamma}\left(0.5, 0.5\right)\,  \in \mathbb{R}
#     \\
#     a \sim \operatorname{N}\left(0.0, {\sqrt{b}}^{2}\right)\,  \in \mathbb{R}
#   \end{gathered}
#   \\
#   a
# \end{equation}

# Simplify the graph so that it's easier to read
from aesara.graph.rewriting.utils import rewrite_graph
from aesara.tensor.rewriting.basic import topo_constant_folding


logprob = rewrite_graph(logprob, custom_rewrite=topo_constant_folding)


print(pprint(logprob))
# s in R, y in R
# (switch(s >= 0.0,
#         ((-0.9189385175704956 +
#           switch(s == 0, -inf, (-1.5 * log(s)))) - (0.5 / s)),
#         -inf) +
#  ((-0.9189385332046727 + (-0.5 * ((y / sqrt(s)) ** 2))) - log(sqrt(s))))

Joint log-probabilities can be computed for some terms that are derived from RandomVariables, as well:

# Create a switching model from a Bernoulli distributed index
Z_rv = at.random.normal([-100, 100], 1.0, name="Z")
I_rv = at.random.bernoulli(0.5, name="I")

M_rv = Z_rv[I_rv]
M_rv.name = "M"

z = at.vector("z")
i = at.lscalar("i")
m = at.scalar("m")
# Compute the joint log-probability for the mixture
logprob = joint_logprob({M_rv: m, Z_rv: z, I_rv: i})


logprob = rewrite_graph(logprob, custom_rewrite=topo_constant_folding)

print(pprint(logprob))
# i in Z, m in R, a in Z
# (switch((0 <= i and i <= 1), -0.6931472, -inf) +
#  ((-0.9189385332046727 + (-0.5 * (((m - [-100  100][a]) / [1. 1.][a]) ** 2))) -
#   log([1. 1.][a])))

Installation

The latest release of aeppl can be installed from PyPI using pip:

pip install aeppl

The current development branch of aeppl can be installed from GitHub, also using pip:

pip install git+https://github.com/aesara-devs/aeppl

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aeppl-0.0.37.tar.gz (63.4 kB view details)

Uploaded Source

Built Distribution

aeppl-0.0.37-py3-none-any.whl (52.1 kB view details)

Uploaded Python 3

File details

Details for the file aeppl-0.0.37.tar.gz.

File metadata

  • Download URL: aeppl-0.0.37.tar.gz
  • Upload date:
  • Size: 63.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for aeppl-0.0.37.tar.gz
Algorithm Hash digest
SHA256 4bdc7c6df2622e9678fa67ab85c284d7ee6b67ac9a4bc17d873655cf1d560de6
MD5 b013118901e64a1fa9c5a2c456273803
BLAKE2b-256 1eb555359382b06a935d61c1cccc148d32f9094599313d6586de429216fe55d5

See more details on using hashes here.

File details

Details for the file aeppl-0.0.37-py3-none-any.whl.

File metadata

  • Download URL: aeppl-0.0.37-py3-none-any.whl
  • Upload date:
  • Size: 52.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for aeppl-0.0.37-py3-none-any.whl
Algorithm Hash digest
SHA256 81d13a770a7dc7c78c5e59a736e7b8e94608353fd34bd3ffde384dad67445458
MD5 5ee9cbed91adf78d8c5d7a2ddc683bfb
BLAKE2b-256 fa226c984e4039ff2f4947198ab63164d600fcd27fb09b92e7461a2c01b38ccf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page