Bayesian Regression for Survey Data with PyMC

Bayesian Regression for Survey Data with PyMC#

Allen Downey and Alexander Fengler

In this tutorial, we explore Bayesian regression using PyMC – the primary library for Bayesian sampling in Python – focusing on survey data and other datasets with categorical outcomes. Starting with logistic regression, we’ll build up to categorical and ordered logistic regression, showcasing how Bayesian approaches provide versatile tools for developing and evaluating complex models. Participants will leave with practical skills for implementing Bayesian regression models in PyMC, along with a deeper appreciation for the power of Bayesian inference in real-world data analysis.

The examples use data from the General Social Survey (GSS). We presented this tutorial at ODSC East 2025. Its companion, Survey Data with Pandas, uses the same GSS data.

Level: intermediate. You should be comfortable with Python, NumPy and pandas, and basic statistics. You don’t need any experience with Bayesian methods or PyMC.

Length: about 90 minutes.

The notebooks#

The pages on this site show the notebooks with solutions; click on a hidden cell to see it. To do the exercises, run the versions with blanks on Colab:

Slides for this tutorial are here.

Running the notebooks#

The easiest way to run the notebooks is on Colab: click one of the links above. You need a Google account, but you don’t have to install anything. The notebooks install nutpie, the sampler they use, and download the data they need.

To run them on your own computer, clone the repository:

git clone https://github.com/AllenDowney/SurveyDataPyMC.git
cd SurveyDataPyMC

Then install the packages with pip:

pip install -r requirements.txt jupyter

Or create a conda environment with mamba (or conda):

mamba env create -f environment.yml
conda activate SurveyDataPyMC

Then run jupyter lab and open the notebooks in the notebooks folder.

Next steps#

The code is available under the MIT License. The text is available under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.