import numpy as np
import pymc as pm
import arviz as az
rng = np.random.default_rng(6420)
4. More about PyMC#
Basic workflow#
In Unit 4, the Ten Coin Flips example, we observed 10 tails in 10 flips, with a \(\operatorname{Beta}(500,500)\) prior on the probability of heads. We previously found the posterior using the conjugate table as \(\operatorname{Beta}(500,510)\), with a mean of approximately 0.495.
with pm.Model() as model:
p = pm.Beta("p", alpha=500, beta=500)
pm.Binomial("y", n=10, p=p, observed=0)
trace = pm.sample(draws=2000, tune=1000, random_seed=rng)
pm.sample() draws samples from the posterior.
See the pm.sample documentation to learn about the various options. For the most part, the arguments above are all you’ll need for this course, but we may write more about initial value generation and other things as they come up. The tune argument defines how many samples are burned (those samples are also used to tune the algorithm itself).
Summarizing results#
The ArviZ library is for analyzing our posterior samples stored in the trace object.
az.summary(trace, ci_prob=0.95, ci_kind="hdi")
| mean | sd | hdi95_lb | hdi95_ub | ess_bulk | ess_tail | r_hat | mcse_mean | mcse_sd | |
|---|---|---|---|---|---|---|---|---|---|
| p | 0.495 | 0.0156 | 0.47 | 0.53 | 3573 | 4742 | 1.00 | 0.00026 | 0.00018 |
The default credible interval method is now equi-tailed with Arviz 1.0. You can switch to HDI and change the credibility level with the arguments above.
See the ArviZ summary documentation.
Mathematical Operations with PyMC#
PyMC uses PyTensor to build and optimize mathematical expressions and calculate derivatives. When performing any mathematical operation within your model, use pm.math or PyTensor functions rather than NumPy whenever possible.
For example, use pm.math.log(p) instead of np.log(p).
The usual arithmetic operators (+, -, *, /) work directly with PyMC variables.
See the PyMC Math API and PyTensor documentation.
Overview of the Unit 6 Demos#
To be added.