Skip to content

About

Bayesian decision analysis in Python, from Bayes's theorem to Bayesian bandits: a self-study tutorial with notebooks that run on Colab

Topics

Resources

Stars

78 stars

Watchers

6 watching

Forks

Repository files navigation

Bayesian Decision Analysis

tests

Allen Downey

This is self-study material: the workshop is not offered live now, but the notebooks are maintained and run on Colab. For a newer version that uses PyMC, see Bayesian Decision Analysis with PyMC.

This tutorial is a hands-on introduction to Bayesian Decision Analysis (BDA), which is a framework for using probability to guide decision-making under uncertainty. I start with Bayes's Theorem, which is the foundation of Bayesian statistics, and work toward the Bayesian bandit strategy, which is used for A/B testing, medical tests, and related applications. For each step, I provide a Jupyter notebook where you can run Python code and work on exercises. In addition to the bandit strategy, I summarize two other applications of BDA, optimal bidding and deriving a decision rule. Finally, I suggest resources you can use to learn more.

The examples in this tutorial are from my book, Think Bayes.

This is one of my workshops and tutorials.

Level: introductory. You should be familiar with Python at an intermediate level, and with Jupyter notebooks. We'll use NumPy, SciPy, and pandas, but I'll explain what you need to know as we go. You should be familiar with basic probability, but you don't need to know anything about Bayesian statistics.

Length: about 90 minutes.

Outline

  • Problem statement: A/B testing, medical tests, and the Bayesian bandit problem
  • Prerequisites and goals
  • Bayes's theorem and the five urn problem
  • Using pandas to represent a PMF
  • Estimating proportions
  • From belief to strategy
  • Implementing and testing Thompson sampling
  • More generally: two other examples of BDA
  • Resources and next steps

The workshop notebook comes in two versions: one with blanks for the exercises, and one with solutions.

Notebook Exercises Solutions
Bayesian Decision Analysis Run on Colab Run on Colab

Slides for this tutorial are here.

Extended version

An earlier, longer version of the workshop covers the same material in four notebooks, starting with the cookie problem and the Euro problem. Slides for the extended version are here.

Notebook Exercises Solutions
1. Bayes's Theorem Run on Colab Run on Colab
2. Distributions Run on Colab Run on Colab
3. Bayesian Statistics Run on Colab Run on Colab
4. Bayesian Bandits Run on Colab Run on Colab

Recordings

I presented this tutorial at ODSC East 2022 and PyData Global 2022. The code in the video is not the current version; the tag after the link is the version of the code that goes with it.

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.

To run them on your own computer, you need Python 3 with Jupyter, NumPy, pandas, SciPy, and Matplotlib. You can download the code in this zip file, or clone the repository:

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

Then install the packages with pip:

pip install -r requirements.txt

Or, if you use conda or mamba, create an environment:

mamba env create -f environment.yml
mamba activate BayesianDecisionAnalysis

Then run jupyter lab and open bda_workshop.ipynb.

Next steps

For a newer version of this workshop that uses PyMC, see Bayesian Decision Analysis with PyMC. To go further, read Think Bayes, which is free online, or see my other workshops.

License

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

About

Bayesian decision analysis in Python, from Bayes's theorem to Bayesian bandits: a self-study tutorial with notebooks that run on Colab

Topics

Resources

Stars

78 stars

Watchers

6 watching

Forks

Releases

Packages

Contributors

Languages