Skip to content

Commit 04e3058

Browse files
committed
[FINALIZED] nb6 is ready for prime time
1 parent 8f0c4b7 commit 04e3058

2 files changed

Lines changed: 451 additions & 5 deletions

File tree

notebooks/07-hierarchical-baseball.ipynb renamed to notebooks/06-instructor-hierarchical-baseball.ipynb

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
"1. What metric would you rank by? \n",
4040
"1. Would your metric be reasonable for rookie players?\n",
4141
"\n",
42-
"### Exercise\n",
43-
"\n",
44-
"Read in the data, which is located at `../data/baseballdb/core/Batting.csv`."
42+
"Let's read in the data."
4543
]
4644
},
4745
{
@@ -129,6 +127,13 @@
129127
" like = pm.Binomial('likelihood', n=df['AB'], p=thetas, observed=df['H'])"
130128
]
131129
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"Now, sample from the posterior"
135+
]
136+
},
132137
{
133138
"cell_type": "code",
134139
"execution_count": null,
@@ -139,6 +144,13 @@
139144
" baseline_trace = pm.sample(2000)"
140145
]
141146
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {},
150+
"source": [
151+
"Plot the traceplots to check for convergence."
152+
]
153+
},
142154
{
143155
"cell_type": "code",
144156
"execution_count": null,
@@ -149,14 +161,21 @@
149161
"despine_traceplot(traceplot)"
150162
]
151163
},
164+
{
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"Visualize the posterior distribution using forest plots."
169+
]
170+
},
152171
{
153172
"cell_type": "code",
154173
"execution_count": null,
155174
"metadata": {},
156175
"outputs": [],
157176
"source": [
158177
"ylabels = ylabels = \"AB: \" + df['AB'].astype(str) + ', H: ' + df['H'].astype('str')\n",
159-
"pm.forestplot(baseline_trace, ylabels=ylabels)"
178+
"pm.forestplot(baseline_trace, ylabels=ylabels, xlim=[0, 1])"
160179
]
161180
},
162181
{
@@ -219,6 +238,13 @@
219238
" like = pm.Binomial('like', n=df['AB'], p=thetas, observed=df['H'])"
220239
]
221240
},
241+
{
242+
"cell_type": "markdown",
243+
"metadata": {},
244+
"source": [
245+
"Sample from the posterior."
246+
]
247+
},
222248
{
223249
"cell_type": "code",
224250
"execution_count": null,
@@ -229,6 +255,13 @@
229255
" trace = pm.sample(2000, nuts_kwargs={'target_accept': 0.95})"
230256
]
231257
},
258+
{
259+
"cell_type": "markdown",
260+
"metadata": {},
261+
"source": [
262+
"Visualize the trace plots to check for convergence."
263+
]
264+
},
232265
{
233266
"cell_type": "code",
234267
"execution_count": null,
@@ -239,14 +272,21 @@
239272
"despine_traceplot(ax_arr)"
240273
]
241274
},
275+
{
276+
"cell_type": "markdown",
277+
"metadata": {},
278+
"source": [
279+
"Visualize the posterior distributions using forestplots."
280+
]
281+
},
242282
{
243283
"cell_type": "code",
244284
"execution_count": null,
245285
"metadata": {},
246286
"outputs": [],
247287
"source": [
248288
"ylabels = \"AB: \" + df['AB'].astype(str) + ', H: ' + df['H'].astype('str')\n",
249-
"pm.forestplot(trace, varnames=['thetas'], ylabels=ylabels)"
289+
"pm.forestplot(trace, varnames=['thetas'], ylabels=ylabels, xlim=[0, 1])"
250290
]
251291
},
252292
{
@@ -301,6 +341,16 @@
301341
"- Under the assumption of hierarchical structure holding true, we will not get estimates that one may consider to be absurd (e.g. long-run batting probability estimated to be zero or one, on the basis onf few observations)."
302342
]
303343
},
344+
{
345+
"cell_type": "markdown",
346+
"metadata": {},
347+
"source": [
348+
"# Further Exercises\n",
349+
"\n",
350+
"- Change the priors. How do they change the posteriors?\n",
351+
"- Change the parameterization of the model. How do they change your inference?"
352+
]
353+
},
304354
{
305355
"cell_type": "markdown",
306356
"metadata": {},

0 commit comments

Comments
 (0)