PyMC¶
A PyMC model is a graph of random variables and deterministic expressions, built inside a
with pymc.Model() context, from which PyMC compiles functions that evaluate the joint log density
(Salvatier et al., 2016). The renderer must keep which variables are observed, which are latent, and which
coordinates belong to one joint event (Transpilation correctness). PyMC's documentation is at
pymc.io.
How PyMC evaluates a log density¶
Each constructor pymc.<Distribution>(name, ..., dims=..., observed=...) in the model's context adds a
random variable, and pymc.Potential
adds a term to the log density. The joint log density is the sum of the log densities of the random
variables and the potentials. The test probe sums
pymc.logp over every free and
observed variable, at the free variables' constrained values and the observed data.
Change of variables¶
PyMC samples a constrained variable in unconstrained coordinates, through a transform such as
log for a positive
variable, and Model.compile_logp
expects transformed values and adds the Jacobian. The probe does not use compile_logp: it evaluates
pymc.logp at constrained values, without a Jacobian, which is the convention of the QVR density, so
\(\Psi_{\mathsf{PyMC}} = \mathrm{id}\) for the comparison.
Family parameterizations¶
PyMC's distributions use their own argument names:
| QVR argument | PyMC argument |
|---|---|
loc |
mu |
scale |
sigma |
concentration (Dirichlet) |
a |
concentration1 (Beta) |
alpha |
concentration0 (Beta) |
beta |
probs |
p |
total_count (Binomial) |
n |
df (StudentT) |
nu |
They are recorded in FAMILY_META[F].arg_aliases["pymc"]. The renaming changes no value, so
\(c_{F, \mathsf{PyMC}} = 0\) for every family PyMC has.
Emitted constructs¶
sample and observe. <name> = pymc.<Family>("<name>", **args, dims=(<plate axes>), observed=<obs>)
inside with pymc.Model(coords=...) as model:. dims names the batch axes, and coords gives each axis
its labels.
Plates. The independence of the draws comes from the distribution's batch shape and the broadcasting
of its arguments. A scalar Normal with batch size ten is ten independent scalar events, and an MvNormal
with event size ten is one joint event whose covariance couples its coordinates.
marginalize. A supported block is enumerated, and its log density, with the variable summed out, is added with a
pymc.Potential. The rows of each group are summed before the reduction.
score, let, return. pymc.Potential("name", expr) for a score, an assignment for a let, and
pymc.Deterministic("<name>_value", ...) for each returned value. The emitted build_model returns the
model.
Evidence¶
- Level 1. Every emitted module has a
def build_model(...)with awith pymc.Model(coords=...) as model:body, onepymc.<Family>(...)call for each draw and observation, andreturn model, and calling the stages directly or composed gives the same output. - Level 2. Python's
astparser accepts the emitted source. - Level 3. The summed
pymc.logpis compared with the QVR density on the selected fixture grids.
Cited sources¶
John Salvatier, Thomas V. Wiecki, and Christopher Fonnesbeck. Probabilistic Programming in Python Using PyMC3. PeerJ Computer Science, 2:e55, 2016. URL: https://peerj.com/articles/cs-55/, doi:10.7717/peerj-cs.55.