Grid algorithm for a beta-binomial hierarchical model
Copyright 2021 Allen B. Downey
License: Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
Heart Attack Data
This example is based on Chapter 10 of Probability and Bayesian Modeling; it uses data on death rates due to heart attack for patients treated at various hospitals in New York City.
We can use Pandas to read the data into a DataFrame.
The columns we need are Cases, which is the number of patients treated at each hospital, and Deaths, which is the number of those patients who died.
Hospital Data with PyMC
Here's a hierarchical model that estimates the death rate for each hospital, and simultaneously estimates the distribution of rates across hospitals.
Here's the graph representation of the model, showing that the observable is an array of 13 values.
Here are the posterior distributions of alpha and beta.
And we can extract the posterior distributions of the xs.
As an example, here's the posterior distribution of x for the first hospital.
Just one update
Here's the graphical representation of the model.
The grid priors
The joint distribution of hyperparameters
Joint prior of alpha, beta, and x
We can speed this up by computing just and skipping the terms that don't depend on x
The following function computes the marginal distributions.
And let's confirm that the marginal distributions are what they are supposed to be.