Skip to content

Commit 69ad6e2

Browse files
committed
[FINALIZED] nb7 is ready for prime time
1 parent f8b4ca5 commit 69ad6e2

2 files changed

Lines changed: 256 additions & 2 deletions

File tree

notebooks/08-hierarchical-finches.ipynb renamed to notebooks/07-instructor-hierarchical-finches.ipynb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"import pandas as pd\n",
1919
"import pymc3 as pm\n",
2020
"from data import load_finches_2012\n",
21+
"from utils import despine_traceplot\n",
2122
"\n",
2223
"%load_ext autoreload\n",
2324
"%autoreload 2\n",
@@ -114,13 +115,28 @@
114115
" trace = pm.sample(2000, nuts_kwargs={'target_accept': 0.95})"
115116
]
116117
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {},
121+
"source": [
122+
"Visualize the traceplots to check for convergence."
123+
]
124+
},
117125
{
118126
"cell_type": "code",
119127
"execution_count": null,
120128
"metadata": {},
121129
"outputs": [],
122130
"source": [
123-
"pm.traceplot(trace, varnames=['mean'])"
131+
"traces = pm.traceplot(trace, varnames=['mean'])\n",
132+
"despine_traceplot(traces)"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"Visualize the posterior distributions using the `plot_posterior` or `forestplot` functions."
124140
]
125141
},
126142
{
@@ -178,6 +194,16 @@
178194
" trace = pm.sample(2000, nuts_kwargs={'target_accept': 0.95})"
179195
]
180196
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"metadata": {},
201+
"outputs": [],
202+
"source": [
203+
"traces = pm.traceplot(trace)\n",
204+
"despine_traceplot(traces)"
205+
]
206+
},
181207
{
182208
"cell_type": "code",
183209
"execution_count": null,
@@ -194,7 +220,7 @@
194220
"cell_type": "markdown",
195221
"metadata": {},
196222
"source": [
197-
"## Discuss\n",
223+
"**Discuss:** \n",
198224
"\n",
199225
"- Are the estimates for the unknown species' beak depth and beak length more reasonable? "
200226
]
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Introduction\n",
8+
"\n",
9+
"This notebook is designed to be the \"exercise\" notebook for you to practice defining hierarchical models. We will do this with the finches dataset again."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import pandas as pd\n",
19+
"import pymc3 as pm\n",
20+
"from data import load_finches_2012\n",
21+
"\n",
22+
"%load_ext autoreload\n",
23+
"%autoreload 2\n",
24+
"%matplotlib inline\n",
25+
"%config InlineBackend.figure_format = 'retina'"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"df = load_finches_2012()\n",
35+
"df.groupby('species').size()"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"df.sample(5)"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"df.groupby('species')['beak_depth'].describe()"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"fortis_filter = df['species'] == 'fortis'\n",
63+
"scandens_filter = df['species'] == 'scandens'\n",
64+
"unknown_filter = df['species'] == 'unknown'"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"**Exercise:** Define a hierarchical model for the finches beak depths. For bonus points, use NumPy-like fancy indexing!"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"with pm.Model() as beak_depth_model:\n",
81+
" # SD can only be positive, therefore it is reasonable to constrain to >0\n",
82+
" # Likewise for betas.\n",
83+
" sd_hyper = pm._________('sd_hyper', _________)\n",
84+
" beta_hyper = pm._________('beta_hyper', _________)\n",
85+
" \n",
86+
" # Beaks cannot be of \"negative\" mean, therefore, HalfNormal is \n",
87+
" # a reasonable, constrained prior.\n",
88+
" mean = pm._________('mean', _________, shape=_________)\n",
89+
" sd = pm._________('sd', _________, shape=_________)\n",
90+
" nu = pm._________('nu', _________) + 1\n",
91+
" \n",
92+
" # Define the likelihood distribution for the data.\n",
93+
" "
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"Sample from the posterior distribution!"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"# Your code below.\n"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"Visualize the traceplots to check for convergence."
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": null,
122+
"metadata": {},
123+
"outputs": [],
124+
"source": [
125+
"# Your code below\n"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"Visualize the posterior distributions using the `plot_posterior` or `forestplot` functions."
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": null,
138+
"metadata": {},
139+
"outputs": [],
140+
"source": [
141+
"ax1, ax2, ax3 = pm.plot_posterior(trace, varnames=['mean'])\n",
142+
"ax1.set_title('fortis')\n",
143+
"ax2.set_title('scandens')\n",
144+
"ax3.set_title('unknown')"
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {},
150+
"source": [
151+
"Now, repeat the model specification for beak length."
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {},
158+
"outputs": [],
159+
"source": [
160+
"# Model definition"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"# Sample from posterior"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"# Check for convergence"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": null,
184+
"metadata": {},
185+
"outputs": [],
186+
"source": [
187+
"# Plot posterior distribution"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"**Discuss:** \n",
195+
"\n",
196+
"- Are the estimates for the unknown species' beak depth and beak length more reasonable? "
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"metadata": {},
203+
"outputs": [],
204+
"source": []
205+
}
206+
],
207+
"metadata": {
208+
"kernelspec": {
209+
"display_name": "bayesian-modelling-tutorial",
210+
"language": "python",
211+
"name": "bayesian-modelling-tutorial"
212+
},
213+
"language_info": {
214+
"codemirror_mode": {
215+
"name": "ipython",
216+
"version": 3
217+
},
218+
"file_extension": ".py",
219+
"mimetype": "text/x-python",
220+
"name": "python",
221+
"nbconvert_exporter": "python",
222+
"pygments_lexer": "ipython3",
223+
"version": "3.6.6"
224+
}
225+
},
226+
"nbformat": 4,
227+
"nbformat_minor": 2
228+
}

0 commit comments

Comments
 (0)