Grid algorithm for a logitnormal-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 are the posterior distributions of the hyperparameters
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
The grid priors
The joint distribution of hyperparameters
Joint prior of hyperparameters, and x
We can speed this up by computing 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.