{"id":3761,"library":"pymc","title":"PyMC","description":"PyMC (formerly PyMC3) is a Python package for Bayesian statistical modeling and probabilistic machine learning. It focuses on advanced Markov chain Monte Carlo (MCMC) and variational inference (VI) algorithms, offering flexibility and extensibility for a wide range of problems. It is currently at version 5.28.4 and maintains a frequent release cadence, with minor updates and bug fixes released regularly.","status":"active","version":"5.28.4","language":"en","source_language":"en","source_url":"https://github.com/pymc-devs/pymc","tags":["probabilistic programming","bayesian","mcmc","statistics","machine learning","pytensor"],"install":[{"cmd":"pip install pymc","lang":"bash","label":"pip"},{"cmd":"conda install -c conda-forge pymc","lang":"bash","label":"Conda"}],"dependencies":[{"reason":"PyMC uses PyTensor as its computational backend, a fork of Aesara, for symbolic graph optimization and compilation.","package":"pytensor"},{"reason":"ArviZ is used for posterior analysis, visualization, and diagnostics of PyMC models.","package":"arviz"},{"reason":"Fundamental library for numerical operations and array manipulation.","package":"numpy"}],"imports":[{"note":"PyMC was renamed from PyMC3 starting with version 4.0. The 'pymc3' import is for older versions and will not work for PyMC v4+ projects.","wrong":"import pymc3 as pm","symbol":"pm","correct":"import pymc as pm"},{"note":"Commonly used within a 'with' statement for model definition.","symbol":"Model","correct":"from pymc import Model"},{"note":"Example distribution; many other distributions are available directly from the top-level 'pymc' package.","symbol":"Normal","correct":"from pymc import Normal"}],"quickstart":{"code":"import numpy as np\nimport pymc as pm\nimport arviz as az\n\n# 1. Simulate some data\nnp.random.seed(42)\nsize = 100\nalpha_true, beta_true = 1, [1, 2.5]\nsigma_true = 1\n\nX1 = np.random.randn(size)\nX2 = np.random.randn(size) * 0.2\nY = alpha_true + beta_true[0] * X1 + beta_true[1] * X2 + np.random.normal(size=size) * sigma_true\n\n# 2. Define the PyMC model\nwith pm.Model() as linear_model:\n    # Priors for unknown model parameters\n    alpha = pm.Normal('alpha', mu=0, sigma=10)\n    beta = pm.Normal('beta', mu=0, sigma=10, shape=2)\n    sigma = pm.HalfNormal('sigma', sigma=1)\n\n    # Expected value of outcome\n    mu = alpha + beta[0] * X1 + beta[1] * X2\n\n    # Likelihood (sampling distribution) of observations\n    Y_obs = pm.Normal('Y_obs', mu=mu, sigma=sigma, observed=Y)\n\n    # 3. Perform MCMC sampling\n    idata = pm.sample(draws=1000, tune=1000, cores=2, random_seed=42)\n\n# 4. Analyze results (e.g., print summary)\nprint(az.summary(idata, var_names=['alpha', 'beta', 'sigma']))\n\n# To visualize, you would typically use:\n# import matplotlib.pyplot as plt\n# az.plot_trace(idata)\n# plt.show()","lang":"python","description":"This quickstart demonstrates how to define a simple Bayesian linear regression model, perform Markov chain Monte Carlo (MCMC) sampling using `pm.sample()`, and analyze the posterior results with ArviZ."},"warnings":[{"fix":"Upgrade Python to >=3.11 and NumPy to >=2.0. If using conda, create a new environment with `conda create -c conda-forge -n pymc_env \"pymc>=5\" python=3.11`.","message":"PyMC v5.26.0 dropped support for Python 3.10 and NumPy versions older than 2.0. Ensure your Python environment is 3.11 or newer and NumPy is 2.0 or newer.","severity":"breaking","affected_versions":">=5.26.0"},{"fix":"Replace `import pymc3 as pm` with `import pymc as pm`.","message":"PyMC was renamed from `PyMC3` to `PyMC` with the release of version 4.0. All `import pymc3 as pm` statements must be updated to `import pymc as pm`.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Review any code directly interacting with `VarName` objects and adapt to use string representations of variable names.","message":"With PyMC v5.27.0, the `VarName` class was replaced by Python's built-in `str`. Code relying on `VarName` objects for variable introspection or manipulation may need adjustment.","severity":"breaking","affected_versions":">=5.27.0"},{"fix":"Update calls to `Model.compile_logp` to provide all necessary model variables as inputs.","message":"From PyMC v5.26.0, `Model.compile_logp` now expects *all* model variables as input, not just a subset of the logp terms. This changes the API for functions that infer graph inputs.","severity":"breaking","affected_versions":">=5.26.0"},{"fix":"Install a C++ compiler (e.g., `conda install m2w64-toolchain` on Windows with Conda, or `sudo apt-get install build-essential` on Debian/Ubuntu).","message":"PyMC relies on PyTensor's ability to compile C implementations for performance. If `g++` (or an equivalent C++ compiler) is not detected in your environment, PyTensor will fall back to slower Python implementations, severely degrading performance.","severity":"gotcha","affected_versions":"All v4+"},{"fix":"Access sampling results using `idata.posterior`, `idata.sample_stats`, etc., and use ArviZ plotting and summary functions which expect `InferenceData`.","message":"Since PyMC3 version 3.9 (and all PyMC v4+), `pm.sample()` defaults to returning an `arviz.InferenceData` object instead of a `MultiTrace` object. Ensure your analysis code is adapted for `InferenceData`.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}