PyMC Documentation

repository·main·Indexed 27 days ago

https://github.com/pymc-devs/pymc

A Python library for Bayesian statistical modeling providing advanced MCMC, Variational Inference, and Sequential Monte Carlo (SMC) algorithms. Built on PyTensor for high-performance computation, PyMC allows users to define probabilistic graphs for posterior inference and random sampling. The ecosystem integrates with ArviZ for diagnostics and plotting, and supports high-level model building via Bambi for Generalized Linear Models.

Tokens
17.1K
Snippets
14
Records
128
Agent score
95%

What's inside PyMC

  1. Overview of PyMC features

    main

    PyMC is a Python package for Bayesian statistical modeling. Key features include:

    • Intuitive model specification: Uses syntax like x ~ N(0,1) which translates to x = Normal('x',0,1).
    • Powerful sampling algorithms: Includes the No U-Turn Sampler (NUTS) for complex models with many parameters.
    • Variational inference: Supports ADVI for fast approximate posterior estimation and mini-batch ADVI for large datasets.
    • PyTensor backend: Leverages PyTensor for computation optimization, dynamic C/JAX compilation, NumPy broadcasting, and linear algebra.
    • Missing value imputation: Transparent support for handling missing data.
  2. Use PyMC timeseries distributions

    main

    PyMC provides several distribution classes for modeling time-series data. These include random walk processes, autoregressive models, and volatility models.

    Available timeseries distributions include:

    • AR: Autoregressive models.
    • EulerMaruyama: Discretization of stochastic differential equations using the Euler-Maruyama method.
    • GARCH11: Generalized Autoregressive Conditional Heteroskedasticity (1,1) models.
    • GaussianRandomWalk: Univariate Gaussian random walk.
    • MvGaussianRandomWalk: Multivariate Gaussian random walk.
    • MvStudentTRandomWalk: Multivariate Student-T random walk.
  3. Understand the role of the BDFL

    main

    The BDFL (Benevolent Dictator for Life), currently Chris Fonnesbeck, holds the authority to make all final decisions for PyMC.

    Key Characteristics:

    • Benevolence: In practice, the BDFL prefers to defer authority to community consensus and the Steering Council.
    • Overriding Vote: The BDFL's final authority is typically used only during Steering Council deadlocks or when specifically requested by the Council.
    • Succession: The BDFL can appoint a successor (consulting the Steering Council), but if they are unable to, the Steering Council suggests candidates to the Main NumFOCUS Board, which makes the final decision.
  4. Specify dimensionality for Random Variables

    main
    PyMC includes various methods and syntactic sugar to easily specify the dimensionality of Random Variables during modeling. This allows for more intuitive handling of multi-dimensional data and parameters.
  5. Use FunctionGraph for model manipulation

    main

    The pymc.model.fgraph module provides utilities for interacting with the underlying functional graph of a PyMC model. You can use these tools to convert between standard PyMC models and their functional graph representations, or to clone models.

    Key functions include:

    • clone_model: Creates a copy of a model.
    • fgraph_from_model: Extracts the functional graph from an existing PyMC model.
    • model_from_fgraph: Reconstructs a PyMC model from a functional graph.
  6. Understand the PyMC ecosystem and dependencies

    main

    PyMC is a probabilistic programming library used to define probabilistic graphs for posterior inference or random sampling. It relies on two primary external libraries for core functionality:

    • PyTensor: Handles low-level tensor operations, gradient computation, random number generation, and operation graphs.
    • ArviZ: Used for Bayesian data storage, manipulation, plotting (Trace plots, rank plots, posterior plots), MCMC diagnostics (Rhat, Effective Sample Size), and model comparison (e.g., leave-one-out cross-validation).

    PyMC itself focuses on inference techniques like Markov chain Monte Carlo (MCMC), Variational Inference, and Sequential Monte Carlo (SMC).

  7. Use the experimental Dims submodule for explicit dimension operations

    main

    The dims submodule provides functions for defining distributions and mathematical operations that utilize explicit dimensions.

    Warning: This module is experimental and may contain critical breaks. API changes are expected in future releases.

    This submodule is organized into the following functional areas:

    • dims.model: Model-related dimension definitions.
    • dims.math: Mathematical operations using explicit dimensions.
    • dims.distributions: Distributions that support explicit dimensions.
    • dims.transforms: Transformations involving explicit dimensions.
  8. Use Gaussian Process covariance functions in PyMC

    main

    PyMC provides a variety of covariance functions (kernels) within the pymc.gp.cov module for defining Gaussian Process models. These functions define the relationship between points in the input space.

    Available covariance functions include:

    • Constant: Models a constant variance.
    • WhiteNoise: Adds independent noise to the process.
    • ExpQuad: Squared exponential (Radial Basis Function) kernel.
    • RatQuad: Rational Quadratic kernel.
    • Exponential: Exponential kernel.
    • Matern52: Matern kernel with $\nu=5/2$.
    • Matern32: Matern kernel with $\nu=3/2$.
    • Linear: Linear kernel.
    • Polynomial: Polynomial kernel.
    • Cosine: Cosine kernel.
    • Periodic: Periodic kernel.
    • WarpedInput: Warped input kernel.
    • Gibbs: Gibbs kernel.
    • Coregion: Coregionalization kernel for multi-output GPs.
    • ScaledCov: Scales a covariance function.
    • Kron: Kronecker product of two covariance functions.
  9. Understand the role of the Steering Council

    main

    The Steering Council is responsible for the long-term technical and community well-being of PyMC. They work with the BDFL and take input from the community.

    Key Responsibilities:

    • Defining overall scope, vision, and direction.
    • Managing strategic collaborations with organizations or individuals.
    • Making decisions on technical issues, features, bugs, and pull requests.
    • Managing project Services.
    • Resolving issues when community discussion fails to reach consensus.

    In everyday activities (code review, discussions), Council members act as peers and do not have special privileges. Their authority is primarily exercised in strategic or deadlock situations.

  10. Understand PyMC Team Organization and Roles

    main

    The PyMC project is organized into dedicated teams and specific contributor roles. While the project generally acts as a single unit, work is distributed across three main teams:

    • Developer Team: Focuses on the PyMC probabilistic programming library.
    • Documentation Team: Focuses on library documentation, infrastructure, and Bayesian statistics learning resources.
    • Community Team: Focuses on nurturing the user community, moderating Discourse, and organizing events like PyMCon.

    Contributor roles determine the level of access and responsibility:

    • Recurring Contributors: Typically have triage-level permissions.
    • Core Contributors: Typically have write-level permissions and higher-level access.
    • Steering Council: Provides leadership and has administrative rights across the organization.
  11. Explore the PyMC ecosystem

    main

    PyMC has a broad ecosystem of related tools categorized by general purpose and domain-specific applications:

    General Purpose Tools

    • Bambi: Bayesian Model-Building Interface (BAMBI) in Python.
    • calibr8: Toolbox for constructing detailed observation models for use as likelihoods in PyMC.
    • gumbi: High-level interface for building Gaussian Process (GP) models.
    • SunODE: Fast ODE solver designed to be significantly faster than the default PyMC solver.
    • pymc-learn: Custom PyMC models built on top of pymc3_models/scikit-learn API.

    Domain Specific Tools

    • Exoplanet: Toolkit for modeling transit and/or radial velocity observations of exoplanets and astronomical time series.
    • beat: Bayesian Earthquake Analysis Tool.
    • CausalPy: Package focused on causal inference in quasi-experimental settings.
    • PyMC-Marketing: Bayesian marketing toolbox for marketing mix modeling, customer lifetime value, and more.
  12. Use continuous probability distributions in PyMC

    main

    PyMC provides a wide range of continuous probability distributions for modeling. These distributions can be used to define priors or likelihoods within a pm.Model context.

    Commonly used continuous distributions include:

    • Normal: Gaussian distribution.
    • StudentT: Student's t-distribution.
    • Exponential: Exponential distribution.
    • Gamma: Gamma distribution.
    • LogNormal: Log-normal distribution.
    • Uniform: Continuous uniform distribution.
    • Cauchy: Cauchy distribution.
    • HalfNormal, HalfStudentT, HalfCauchy: Half-distributions (truncated at zero).
    • Beta: Beta distribution.
    • Weibull: Weibull distribution.
    • Laplace: Laplace distribution.
    • Logistic: Logistic distribution.
    • TruncatedNormal: Normal distribution truncated to a specific range.