PyMC Extras
PyMC-Extras (version 0.10.0) serves as an experimental extension library for PyMC, providing a sandbox for new probability distributions, advanced model fitting algorithms, and other specialized code not yet integrated into the main PyMC repository. It aims to offer bleeding-edge features to users, with a flexible release cadence tied to the development cycle of its experimental components.
Common errors
-
ModuleNotFoundError: No module named 'pymc_extras'
cause The `pymc-extras` library is not installed in the current Python environment.fixInstall the library using pip: `pip install pymc-extras` -
AttributeError: module 'pymc_extras.distributions' has no attribute 'MyCustomDistribution'
cause Attempted to access a distribution that does not exist or has a different name within `pymc_extras.distributions`.fixVerify the correct spelling and existence of the distribution in the `pymc-extras` documentation or by inspecting the module contents (e.g., `dir(pmx_dist)` if imported as `pmx_dist`). -
TypeError: unsupported operand type(s) for +: 'TensorVariable' and 'NoneType' (or similar PyTensor/Aesara runtime errors when using custom distributions)
cause Often indicates an incompatibility between `pymc-extras` and the installed PyMC/PyTensor versions, or incorrect usage of a distribution's parameters (e.g., passing `None` where a tensor is expected).fixEnsure both PyMC and `pymc-extras` are up-to-date (`pip install --upgrade pymc pymc-extras`). Review the documentation for the specific distribution's parameter requirements. If issues persist, consider isolating the problem in a minimal reproducible example.
Warnings
- gotcha Features in `pymc-extras` are experimental and subject to change or removal without strict adherence to semantic versioning. They may not be stable for production use.
- gotcha Ensure compatibility with your PyMC installation. `pymc-extras` is generally developed against the latest stable PyMC version, and older PyMC versions might lead to API mismatches or runtime errors.
- gotcha Some features from `pymc-extras` might eventually be integrated into core PyMC. While this offers stability, it means the `pymc-extras` version of a feature might become redundant or deprecated.
Install
-
pip install pymc-extras
Imports
- distributions
from pymc_extras.distributions import *
from pymc_extras import distributions
- samplers
from pymc_extras import samplers
Quickstart
import pymc as pm
from pymc_extras import distributions as pmx_dist
import numpy as np
# Define a custom distribution from pymc-extras, e.g., the LogitNormal
with pm.Model() as model:
# Example using LogitNormal, suitable for modeling proportions
proportion_latent = pm.Normal("proportion_latent", mu=0, sigma=1)
proportion = pmx_dist.LogitNormal("proportion", mu=proportion_latent, sigma=1)
# In a real scenario, you would typically add observed data for inference.
# Example: obs = pm.Binomial("obs", n=10, p=proportion, observed=np.array([5, 6, 7]))
# For this quickstart, we just demonstrate model definition.
# trace = pm.sample(draws=1000, tune=1000, chains=2)
print(f"Model created with LogitNormal distribution: {model.named_vars['proportion']}")