{ "metadata": { "name": "", "signature": "sha256:07ba974f31862131b4ffabef94c15c28a40b2b137ac3040902fd0d4eaa072877" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hierarchical Bayesian Inference of Binomial Probabilities (BDA 5.3) #\n", "\n", "This notebook attempts to reproduce analysis in Chapter 5.3 of the book 'Bayesian Data Analysis' by Gelman et al (3rd Edition). \n", "\n", "Here, we are given $J=71$ observations of binomial outcomes $(y_1,N_1), \\ldots, (y_J,N_J)$ where $y_j \\sim \\text{Bin}(N_j, \\theta_j)$. We assume conjugate prior $\\theta_j \\sim \\text{Beta}(\\alpha,\\beta)$, and a \"reasonable\" hyperprior density $p(\\alpha,\\beta) \\propto (\\alpha+\\beta)^{-5/2}$; refer to the chapter regarding why this is a reasonable choice. Given this hierarchical model, we perform fully Bayesian analysis." ] }, { "cell_type": "code", "collapsed": false, "input": [ "% pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# load data from Andrew's homepage\n", "from pandas import *\n", "tumor_df = read_csv(\"http://www.stat.columbia.edu/~gelman/book/data/rats.asc\",\n", " index_col=False, skiprows=2, delim_whitespace=True)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# the data roughly looks like follows\n", "tumor_df.head(20)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " | y | \n", "N | \n", "
---|---|---|
0 | \n", "0 | \n", "20 | \n", "
1 | \n", "0 | \n", "20 | \n", "
2 | \n", "0 | \n", "20 | \n", "
3 | \n", "0 | \n", "20 | \n", "
4 | \n", "0 | \n", "20 | \n", "
5 | \n", "0 | \n", "20 | \n", "
6 | \n", "0 | \n", "20 | \n", "
7 | \n", "0 | \n", "19 | \n", "
8 | \n", "0 | \n", "19 | \n", "
9 | \n", "0 | \n", "19 | \n", "
10 | \n", "0 | \n", "19 | \n", "
11 | \n", "0 | \n", "18 | \n", "
12 | \n", "0 | \n", "18 | \n", "
13 | \n", "0 | \n", "17 | \n", "
14 | \n", "1 | \n", "20 | \n", "
15 | \n", "1 | \n", "20 | \n", "
16 | \n", "1 | \n", "20 | \n", "
17 | \n", "1 | \n", "20 | \n", "
18 | \n", "1 | \n", "19 | \n", "
19 | \n", "1 | \n", "19 | \n", "