Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Getting Started

Setting Up Python for Bayesian Work

Meet the Python tools used in this course: NumPy random generators, SciPy distributions, helper functions for posterior summaries, and how PyMC and ArviZ fit in.

You do not need a big framework to do Bayesian statistics. NumPy and SciPy are enough to learn every idea honestly, and the code you write transfers directly to PyMC, Stan or NumPyro later. This lesson introduces the small toolkit we use in the browser, a few helper functions you will reuse, and where the big libraries fit.

What runs in this course

Every example in the browser runs real Python (Pyodide) with:

  • NumPy for arrays and random number generation.
  • SciPy (scipy.stats) for distributions: PDFs, CDFs, quantiles and random draws.
  • pandas for tables of draws and summaries.

PyMC, Stan and NumPyro need compilers and native extensions that the browser does not provide. Chapter 8 teaches PyMC-style workflows, so the lessons there show PyMC code as read-only snippets and run the same model with hand-written samplers you will build in chapter 4. We say so each time, so you always know what is real and what is a stand-in.

Random numbers with a seed

Modern NumPy uses a Generator. Always seed it so your results are reproducible.

Python Reproducible random draws

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Drawing random samples from a distribution is the bridge between math and computation: a few thousand draws from a posterior let you estimate any mean, quantile or probability by simple counting.

Distributions in SciPy

scipy.stats gives each distribution as an object. You create a "frozen" distribution with its parameters and then ask it questions.

Python Frozen distributions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The names to remember: pdf/pmf (density), cdf (cumulative), ppf (inverse CDF, i.e. quantile), rvs (random draws), mean, std, interval. The Beta, Gamma, Normal, Binomial and Poisson distributions cover most of this course.

Parameterization traps

Different libraries parameterize the same distribution differently, a classic source of bugs.

  • Normal: SciPy uses scale = standard deviation (not variance, not precision).
  • Gamma: SciPy uses shape a and scale; many textbooks use shape and rate, where scale = 1 / rate.
  • Exponential: scale = 1 / rate.
Python Gamma shape-rate vs scale

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Always check by computing the mean or variance against the formula.

Helper functions we will reuse

Two small functions summarize draws. The highest density interval (HDI) is the narrowest interval holding a chosen probability, which we explain fully in Posterior Summaries.

Python A summary helper for posterior draws

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

We will paste this helper at the top of lessons that need it, so every lesson stays self-contained.

Plotting

Matplotlib works in the browser, but most lessons print numbers so you can follow the results as text. When a picture helps, we describe it in words or draw a diagram. If you plot locally, the same code runs unchanged.

Where PyMC, ArviZ and Stan fit

Tool Role In this course
PyMC Write models in Python, sample with NUTS Read-only snippets, runnable hand-written stand-ins
ArviZ Diagnostics and plots for posterior draws Same ideas implemented by hand (R-hat, ESS, HDI)
Stan / CmdStanPy Own modeling language, fast HMC Mentioned for comparison
NumPyro JAX-based, very fast Mentioned for comparison

Everything these libraries do, from priors to NUTS, is built on ideas you are about to implement yourself, so the leap later is small.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should you seed a NumPy random generator?

    The same seed reproduces the same sequence, which makes analyses repeatable.
  2. 2. In scipy.stats, what does the ppf method compute?

    ppf is the percent-point function, the quantile function.
  3. 3. A Gamma distribution has shape 3 and rate 2. What scale do you pass to SciPy?

    SciPy uses scale = 1 / rate = 0.5.
  4. 4. What does the HDI helper return?

    The highest density interval is the narrowest interval holding the requested probability mass.

Summary

  • NumPy and SciPy are enough to learn Bayesian methods; the code runs in your browser.
  • Seed a default_rng, and use frozen scipy.stats distributions (pdf, cdf, ppf, rvs).
  • Watch out for scale versus rate and standard deviation versus variance.
  • A small hdi and summarize helper covers posterior reporting.
  • PyMC, ArviZ and Stan build on the same ideas; they appear as read-only code with runnable stand-ins.

Next: Bayes' Theorem