|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "18ced5a7", |
| 5 | + "id": "4480886e", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | 8 | "# Posterior Distributions for AR(1) Parameters" |
9 | 9 | ] |
10 | 10 | }, |
11 | 11 | { |
12 | 12 | "cell_type": "markdown", |
13 | | - "id": "3c79813b", |
| 13 | + "id": "4550bc58", |
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | 16 | "# GPU\n", |
17 | 17 | "\n", |
18 | | - "This lecture was built using a machine with the latest CUDA and CUDANN frameworks installed with access to a GPU.\n", |
| 18 | + "This lecture was built using a machine with access to a GPU.\n", |
19 | 19 | "\n", |
20 | | - "To run this lecture on [Google Colab](https://colab.research.google.com/), click on the “play” icon top right, select Colab, and set the runtime environment to include a GPU.\n", |
| 20 | + "[Google Colab](https://colab.research.google.com/) has a free tier with GPUs\n", |
| 21 | + "that you can access as follows:\n", |
21 | 22 | "\n", |
22 | | - "To run this lecture on your own machine, you need to install the software listed following this notice." |
| 23 | + "1. Click on the “play” icon top right \n", |
| 24 | + "1. Select Colab \n", |
| 25 | + "1. Set the runtime environment to include a GPU " |
23 | 26 | ] |
24 | 27 | }, |
25 | 28 | { |
26 | 29 | "cell_type": "code", |
27 | 30 | "execution_count": null, |
28 | | - "id": "697c1e14", |
| 31 | + "id": "bc11defe", |
29 | 32 | "metadata": { |
30 | 33 | "hide-output": false |
31 | 34 | }, |
|
36 | 39 | }, |
37 | 40 | { |
38 | 41 | "cell_type": "markdown", |
39 | | - "id": "510ed42f", |
| 42 | + "id": "4d1620ca", |
40 | 43 | "metadata": {}, |
41 | 44 | "source": [ |
42 | 45 | "In addition to what’s included in base Anaconda, we need to install the following packages" |
|
45 | 48 | { |
46 | 49 | "cell_type": "code", |
47 | 50 | "execution_count": null, |
48 | | - "id": "0ba760f9", |
| 51 | + "id": "8387d646", |
49 | 52 | "metadata": { |
50 | 53 | "hide-output": false |
51 | 54 | }, |
|
56 | 59 | }, |
57 | 60 | { |
58 | 61 | "cell_type": "markdown", |
59 | | - "id": "979aadb2", |
| 62 | + "id": "9492ed94", |
60 | 63 | "metadata": {}, |
61 | 64 | "source": [ |
62 | 65 | "We’ll begin with some Python imports." |
|
65 | 68 | { |
66 | 69 | "cell_type": "code", |
67 | 70 | "execution_count": null, |
68 | | - "id": "747e3f8f", |
| 71 | + "id": "d989aa29", |
69 | 72 | "metadata": { |
70 | 73 | "hide-output": false |
71 | 74 | }, |
|
89 | 92 | }, |
90 | 93 | { |
91 | 94 | "cell_type": "markdown", |
92 | | - "id": "b0d50bc4", |
| 95 | + "id": "806e7cad", |
93 | 96 | "metadata": {}, |
94 | 97 | "source": [ |
95 | 98 | "This lecture uses Bayesian methods offered by [pymc](https://www.pymc.io/projects/docs/en/stable/) and [numpyro](https://num.pyro.ai/en/stable/) to make statistical inferences about two parameters of a univariate first-order autoregression.\n", |
|
185 | 188 | { |
186 | 189 | "cell_type": "code", |
187 | 190 | "execution_count": null, |
188 | | - "id": "f863c76f", |
| 191 | + "id": "d4d988b3", |
189 | 192 | "metadata": { |
190 | 193 | "hide-output": false |
191 | 194 | }, |
|
215 | 218 | { |
216 | 219 | "cell_type": "code", |
217 | 220 | "execution_count": null, |
218 | | - "id": "88e84ff6", |
| 221 | + "id": "270e075d", |
219 | 222 | "metadata": { |
220 | 223 | "hide-output": false |
221 | 224 | }, |
|
227 | 230 | }, |
228 | 231 | { |
229 | 232 | "cell_type": "markdown", |
230 | | - "id": "3cc71268", |
| 233 | + "id": "0eb5e83e", |
231 | 234 | "metadata": {}, |
232 | 235 | "source": [ |
233 | 236 | "Now we shall use Bayes’ law to construct a posterior distribution, conditioning on the initial value of $ y_0 $.\n", |
|
239 | 242 | }, |
240 | 243 | { |
241 | 244 | "cell_type": "markdown", |
242 | | - "id": "c51a7933", |
| 245 | + "id": "599ab857", |
243 | 246 | "metadata": {}, |
244 | 247 | "source": [ |
245 | 248 | "## PyMC Implementation\n", |
|
251 | 254 | { |
252 | 255 | "cell_type": "code", |
253 | 256 | "execution_count": null, |
254 | | - "id": "cfd857b6", |
| 257 | + "id": "2de368d6", |
255 | 258 | "metadata": { |
256 | 259 | "hide-output": false |
257 | 260 | }, |
|
274 | 277 | }, |
275 | 278 | { |
276 | 279 | "cell_type": "markdown", |
277 | | - "id": "8fbecca2", |
| 280 | + "id": "330d418a", |
278 | 281 | "metadata": {}, |
279 | 282 | "source": [ |
280 | 283 | "[pmc.sample](https://www.pymc.io/projects/docs/en/v5.10.0/api/generated/pymc.sample.html#pymc-sample) by default uses the NUTS samplers to generate samples as shown in the below cell:" |
|
283 | 286 | { |
284 | 287 | "cell_type": "code", |
285 | 288 | "execution_count": null, |
286 | | - "id": "f474ffc2", |
| 289 | + "id": "3c53fefc", |
287 | 290 | "metadata": { |
288 | 291 | "hide-output": false |
289 | 292 | }, |
|
296 | 299 | { |
297 | 300 | "cell_type": "code", |
298 | 301 | "execution_count": null, |
299 | | - "id": "c29dc97d", |
| 302 | + "id": "5037b8bf", |
300 | 303 | "metadata": { |
301 | 304 | "hide-output": false |
302 | 305 | }, |
|
308 | 311 | }, |
309 | 312 | { |
310 | 313 | "cell_type": "markdown", |
311 | | - "id": "7853e601", |
| 314 | + "id": "67b66300", |
312 | 315 | "metadata": {}, |
313 | 316 | "source": [ |
314 | 317 | "Evidently, the posteriors aren’t centered on the true values of $ .5, 1 $ that we used to generate the data.\n", |
|
323 | 326 | { |
324 | 327 | "cell_type": "code", |
325 | 328 | "execution_count": null, |
326 | | - "id": "1fe6b4fc", |
| 329 | + "id": "66f6f117", |
327 | 330 | "metadata": { |
328 | 331 | "hide-output": false |
329 | 332 | }, |
|
337 | 340 | }, |
338 | 341 | { |
339 | 342 | "cell_type": "markdown", |
340 | | - "id": "890ff987", |
| 343 | + "id": "f42ea1fd", |
341 | 344 | "metadata": {}, |
342 | 345 | "source": [ |
343 | 346 | "Now we shall compute a posterior distribution after seeing the same data but instead assuming that $ y_0 $ is drawn from the stationary distribution.\n", |
|
354 | 357 | { |
355 | 358 | "cell_type": "code", |
356 | 359 | "execution_count": null, |
357 | | - "id": "a789393a", |
| 360 | + "id": "3251a3cd", |
358 | 361 | "metadata": { |
359 | 362 | "hide-output": false |
360 | 363 | }, |
|
380 | 383 | { |
381 | 384 | "cell_type": "code", |
382 | 385 | "execution_count": null, |
383 | | - "id": "f6a4866b", |
| 386 | + "id": "51a0687e", |
384 | 387 | "metadata": { |
385 | 388 | "hide-output": false |
386 | 389 | }, |
|
395 | 398 | { |
396 | 399 | "cell_type": "code", |
397 | 400 | "execution_count": null, |
398 | | - "id": "8b333c4f", |
| 401 | + "id": "f7aec957", |
399 | 402 | "metadata": { |
400 | 403 | "hide-output": false |
401 | 404 | }, |
|
408 | 411 | { |
409 | 412 | "cell_type": "code", |
410 | 413 | "execution_count": null, |
411 | | - "id": "55720f12", |
| 414 | + "id": "e1fd2d60", |
412 | 415 | "metadata": { |
413 | 416 | "hide-output": false |
414 | 417 | }, |
|
422 | 425 | }, |
423 | 426 | { |
424 | 427 | "cell_type": "markdown", |
425 | | - "id": "1fcc5325", |
| 428 | + "id": "70752930", |
426 | 429 | "metadata": {}, |
427 | 430 | "source": [ |
428 | 431 | "Please note how the posterior for $ \\rho $ has shifted to the right relative to when we conditioned on $ y_0 $ instead of assuming that $ y_0 $ is drawn from the stationary distribution.\n", |
|
439 | 442 | }, |
440 | 443 | { |
441 | 444 | "cell_type": "markdown", |
442 | | - "id": "d09281f4", |
| 445 | + "id": "bab7fe1b", |
443 | 446 | "metadata": {}, |
444 | 447 | "source": [ |
445 | 448 | "## Numpyro Implementation" |
|
448 | 451 | { |
449 | 452 | "cell_type": "code", |
450 | 453 | "execution_count": null, |
451 | | - "id": "17e665d1", |
| 454 | + "id": "31bbfec8", |
452 | 455 | "metadata": { |
453 | 456 | "hide-output": false |
454 | 457 | }, |
|
484 | 487 | { |
485 | 488 | "cell_type": "code", |
486 | 489 | "execution_count": null, |
487 | | - "id": "bd045277", |
| 490 | + "id": "813ca322", |
488 | 491 | "metadata": { |
489 | 492 | "hide-output": false |
490 | 493 | }, |
|
505 | 508 | { |
506 | 509 | "cell_type": "code", |
507 | 510 | "execution_count": null, |
508 | | - "id": "4b4336f6", |
| 511 | + "id": "58463276", |
509 | 512 | "metadata": { |
510 | 513 | "hide-output": false |
511 | 514 | }, |
|
525 | 528 | { |
526 | 529 | "cell_type": "code", |
527 | 530 | "execution_count": null, |
528 | | - "id": "a8df7780", |
| 531 | + "id": "f0fc347e", |
529 | 532 | "metadata": { |
530 | 533 | "hide-output": false |
531 | 534 | }, |
|
537 | 540 | { |
538 | 541 | "cell_type": "code", |
539 | 542 | "execution_count": null, |
540 | | - "id": "6eccfecd", |
| 543 | + "id": "b8e9e35a", |
541 | 544 | "metadata": { |
542 | 545 | "hide-output": false |
543 | 546 | }, |
|
548 | 551 | }, |
549 | 552 | { |
550 | 553 | "cell_type": "markdown", |
551 | | - "id": "85fd70d5", |
| 554 | + "id": "b3d5a9f3", |
552 | 555 | "metadata": {}, |
553 | 556 | "source": [ |
554 | 557 | "Next, we again compute the posterior under the assumption that $ y_0 $ is drawn from the stationary distribution, so that\n", |
|
563 | 566 | { |
564 | 567 | "cell_type": "code", |
565 | 568 | "execution_count": null, |
566 | | - "id": "99221a9d", |
| 569 | + "id": "58dac77b", |
567 | 570 | "metadata": { |
568 | 571 | "hide-output": false |
569 | 572 | }, |
|
588 | 591 | { |
589 | 592 | "cell_type": "code", |
590 | 593 | "execution_count": null, |
591 | | - "id": "4e718266", |
| 594 | + "id": "08c6f462", |
592 | 595 | "metadata": { |
593 | 596 | "hide-output": false |
594 | 597 | }, |
|
608 | 611 | { |
609 | 612 | "cell_type": "code", |
610 | 613 | "execution_count": null, |
611 | | - "id": "7d5cb384", |
| 614 | + "id": "8897e861", |
612 | 615 | "metadata": { |
613 | 616 | "hide-output": false |
614 | 617 | }, |
|
620 | 623 | { |
621 | 624 | "cell_type": "code", |
622 | 625 | "execution_count": null, |
623 | | - "id": "9532bfb1", |
| 626 | + "id": "97a6b1bb", |
624 | 627 | "metadata": { |
625 | 628 | "hide-output": false |
626 | 629 | }, |
|
631 | 634 | }, |
632 | 635 | { |
633 | 636 | "cell_type": "markdown", |
634 | | - "id": "4bff71c8", |
| 637 | + "id": "8c7ba5ad", |
635 | 638 | "metadata": {}, |
636 | 639 | "source": [ |
637 | 640 | "Look what happened to the posterior!\n", |
|
646 | 649 | } |
647 | 650 | ], |
648 | 651 | "metadata": { |
649 | | - "date": 1764378007.2089982, |
| 652 | + "date": 1764547274.0689824, |
650 | 653 | "filename": "ar1_bayes.md", |
651 | 654 | "kernelspec": { |
652 | 655 | "display_name": "Python", |
|
0 commit comments