|
39 | 39 | "1. What metric would you rank by? \n", |
40 | 40 | "1. Would your metric be reasonable for rookie players?\n", |
41 | 41 | "\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." |
45 | 43 | ] |
46 | 44 | }, |
47 | 45 | { |
|
129 | 127 | " like = pm.Binomial('likelihood', n=df['AB'], p=thetas, observed=df['H'])" |
130 | 128 | ] |
131 | 129 | }, |
| 130 | + { |
| 131 | + "cell_type": "markdown", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "Now, sample from the posterior" |
| 135 | + ] |
| 136 | + }, |
132 | 137 | { |
133 | 138 | "cell_type": "code", |
134 | 139 | "execution_count": null, |
|
139 | 144 | " baseline_trace = pm.sample(2000)" |
140 | 145 | ] |
141 | 146 | }, |
| 147 | + { |
| 148 | + "cell_type": "markdown", |
| 149 | + "metadata": {}, |
| 150 | + "source": [ |
| 151 | + "Plot the traceplots to check for convergence." |
| 152 | + ] |
| 153 | + }, |
142 | 154 | { |
143 | 155 | "cell_type": "code", |
144 | 156 | "execution_count": null, |
|
149 | 161 | "despine_traceplot(traceplot)" |
150 | 162 | ] |
151 | 163 | }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "Visualize the posterior distribution using forest plots." |
| 169 | + ] |
| 170 | + }, |
152 | 171 | { |
153 | 172 | "cell_type": "code", |
154 | 173 | "execution_count": null, |
155 | 174 | "metadata": {}, |
156 | 175 | "outputs": [], |
157 | 176 | "source": [ |
158 | 177 | "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])" |
160 | 179 | ] |
161 | 180 | }, |
162 | 181 | { |
|
219 | 238 | " like = pm.Binomial('like', n=df['AB'], p=thetas, observed=df['H'])" |
220 | 239 | ] |
221 | 240 | }, |
| 241 | + { |
| 242 | + "cell_type": "markdown", |
| 243 | + "metadata": {}, |
| 244 | + "source": [ |
| 245 | + "Sample from the posterior." |
| 246 | + ] |
| 247 | + }, |
222 | 248 | { |
223 | 249 | "cell_type": "code", |
224 | 250 | "execution_count": null, |
|
229 | 255 | " trace = pm.sample(2000, nuts_kwargs={'target_accept': 0.95})" |
230 | 256 | ] |
231 | 257 | }, |
| 258 | + { |
| 259 | + "cell_type": "markdown", |
| 260 | + "metadata": {}, |
| 261 | + "source": [ |
| 262 | + "Visualize the trace plots to check for convergence." |
| 263 | + ] |
| 264 | + }, |
232 | 265 | { |
233 | 266 | "cell_type": "code", |
234 | 267 | "execution_count": null, |
|
239 | 272 | "despine_traceplot(ax_arr)" |
240 | 273 | ] |
241 | 274 | }, |
| 275 | + { |
| 276 | + "cell_type": "markdown", |
| 277 | + "metadata": {}, |
| 278 | + "source": [ |
| 279 | + "Visualize the posterior distributions using forestplots." |
| 280 | + ] |
| 281 | + }, |
242 | 282 | { |
243 | 283 | "cell_type": "code", |
244 | 284 | "execution_count": null, |
245 | 285 | "metadata": {}, |
246 | 286 | "outputs": [], |
247 | 287 | "source": [ |
248 | 288 | "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])" |
250 | 290 | ] |
251 | 291 | }, |
252 | 292 | { |
|
301 | 341 | "- 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)." |
302 | 342 | ] |
303 | 343 | }, |
| 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 | + }, |
304 | 354 | { |
305 | 355 | "cell_type": "markdown", |
306 | 356 | "metadata": {}, |
|
0 commit comments