|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "4c2ff036", |
| 5 | + "id": "3acdf11f", |
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": "753fbd5d", |
| 13 | + "id": "608b2d44", |
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | 16 | "# GPU\n", |
|
28 | 28 | { |
29 | 29 | "cell_type": "code", |
30 | 30 | "execution_count": null, |
31 | | - "id": "2bcd2529", |
| 31 | + "id": "1176f228", |
32 | 32 | "metadata": { |
33 | 33 | "hide-output": false |
34 | 34 | }, |
|
39 | 39 | }, |
40 | 40 | { |
41 | 41 | "cell_type": "markdown", |
42 | | - "id": "ffc6fe58", |
| 42 | + "id": "b639ca6b", |
43 | 43 | "metadata": {}, |
44 | 44 | "source": [ |
45 | 45 | "In addition to what’s included in base Anaconda, we need to install the following packages" |
|
48 | 48 | { |
49 | 49 | "cell_type": "code", |
50 | 50 | "execution_count": null, |
51 | | - "id": "2fcba788", |
| 51 | + "id": "8b92d31e", |
52 | 52 | "metadata": { |
53 | 53 | "hide-output": false |
54 | 54 | }, |
|
59 | 59 | }, |
60 | 60 | { |
61 | 61 | "cell_type": "markdown", |
62 | | - "id": "c160848c", |
| 62 | + "id": "1d0b4ee3", |
63 | 63 | "metadata": {}, |
64 | 64 | "source": [ |
65 | 65 | "We’ll begin with some Python imports." |
|
68 | 68 | { |
69 | 69 | "cell_type": "code", |
70 | 70 | "execution_count": null, |
71 | | - "id": "dd636b52", |
| 71 | + "id": "0eef2439", |
72 | 72 | "metadata": { |
73 | 73 | "hide-output": false |
74 | 74 | }, |
|
92 | 92 | }, |
93 | 93 | { |
94 | 94 | "cell_type": "markdown", |
95 | | - "id": "c5a4636c", |
| 95 | + "id": "28901b24", |
96 | 96 | "metadata": {}, |
97 | 97 | "source": [ |
98 | 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", |
|
188 | 188 | { |
189 | 189 | "cell_type": "code", |
190 | 190 | "execution_count": null, |
191 | | - "id": "582a28ea", |
| 191 | + "id": "f129e1b4", |
192 | 192 | "metadata": { |
193 | 193 | "hide-output": false |
194 | 194 | }, |
|
218 | 218 | { |
219 | 219 | "cell_type": "code", |
220 | 220 | "execution_count": null, |
221 | | - "id": "d1596287", |
| 221 | + "id": "10017972", |
222 | 222 | "metadata": { |
223 | 223 | "hide-output": false |
224 | 224 | }, |
|
230 | 230 | }, |
231 | 231 | { |
232 | 232 | "cell_type": "markdown", |
233 | | - "id": "e55155d0", |
| 233 | + "id": "9bd0be83", |
234 | 234 | "metadata": {}, |
235 | 235 | "source": [ |
236 | 236 | "Now we shall use Bayes’ law to construct a posterior distribution, conditioning on the initial value of $ y_0 $.\n", |
|
242 | 242 | }, |
243 | 243 | { |
244 | 244 | "cell_type": "markdown", |
245 | | - "id": "1af65dba", |
| 245 | + "id": "9ba227de", |
246 | 246 | "metadata": {}, |
247 | 247 | "source": [ |
248 | 248 | "## PyMC Implementation\n", |
|
254 | 254 | { |
255 | 255 | "cell_type": "code", |
256 | 256 | "execution_count": null, |
257 | | - "id": "3453e8bf", |
| 257 | + "id": "ccb42a21", |
258 | 258 | "metadata": { |
259 | 259 | "hide-output": false |
260 | 260 | }, |
|
277 | 277 | }, |
278 | 278 | { |
279 | 279 | "cell_type": "markdown", |
280 | | - "id": "20ced9e4", |
| 280 | + "id": "b253e05e", |
281 | 281 | "metadata": {}, |
282 | 282 | "source": [ |
283 | 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:" |
|
286 | 286 | { |
287 | 287 | "cell_type": "code", |
288 | 288 | "execution_count": null, |
289 | | - "id": "5c025dba", |
| 289 | + "id": "d84d93d0", |
290 | 290 | "metadata": { |
291 | 291 | "hide-output": false |
292 | 292 | }, |
|
299 | 299 | { |
300 | 300 | "cell_type": "code", |
301 | 301 | "execution_count": null, |
302 | | - "id": "d04f6c90", |
| 302 | + "id": "2e81101e", |
303 | 303 | "metadata": { |
304 | 304 | "hide-output": false |
305 | 305 | }, |
|
311 | 311 | }, |
312 | 312 | { |
313 | 313 | "cell_type": "markdown", |
314 | | - "id": "5558841d", |
| 314 | + "id": "0d2e5a85", |
315 | 315 | "metadata": {}, |
316 | 316 | "source": [ |
317 | 317 | "Evidently, the posteriors aren’t centered on the true values of $ .5, 1 $ that we used to generate the data.\n", |
|
326 | 326 | { |
327 | 327 | "cell_type": "code", |
328 | 328 | "execution_count": null, |
329 | | - "id": "9dfdd777", |
| 329 | + "id": "f6a307d2", |
330 | 330 | "metadata": { |
331 | 331 | "hide-output": false |
332 | 332 | }, |
|
340 | 340 | }, |
341 | 341 | { |
342 | 342 | "cell_type": "markdown", |
343 | | - "id": "9dfb09ea", |
| 343 | + "id": "80565ac4", |
344 | 344 | "metadata": {}, |
345 | 345 | "source": [ |
346 | 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", |
|
357 | 357 | { |
358 | 358 | "cell_type": "code", |
359 | 359 | "execution_count": null, |
360 | | - "id": "8262a6dd", |
| 360 | + "id": "25623234", |
361 | 361 | "metadata": { |
362 | 362 | "hide-output": false |
363 | 363 | }, |
|
383 | 383 | { |
384 | 384 | "cell_type": "code", |
385 | 385 | "execution_count": null, |
386 | | - "id": "d89446b8", |
| 386 | + "id": "d8f79724", |
387 | 387 | "metadata": { |
388 | 388 | "hide-output": false |
389 | 389 | }, |
|
398 | 398 | { |
399 | 399 | "cell_type": "code", |
400 | 400 | "execution_count": null, |
401 | | - "id": "69a4556c", |
| 401 | + "id": "93170dab", |
402 | 402 | "metadata": { |
403 | 403 | "hide-output": false |
404 | 404 | }, |
|
411 | 411 | { |
412 | 412 | "cell_type": "code", |
413 | 413 | "execution_count": null, |
414 | | - "id": "3d0003d0", |
| 414 | + "id": "ac7f00c2", |
415 | 415 | "metadata": { |
416 | 416 | "hide-output": false |
417 | 417 | }, |
|
425 | 425 | }, |
426 | 426 | { |
427 | 427 | "cell_type": "markdown", |
428 | | - "id": "c4e688b2", |
| 428 | + "id": "94f83b3f", |
429 | 429 | "metadata": {}, |
430 | 430 | "source": [ |
431 | 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", |
|
442 | 442 | }, |
443 | 443 | { |
444 | 444 | "cell_type": "markdown", |
445 | | - "id": "1a15c07c", |
| 445 | + "id": "09730117", |
446 | 446 | "metadata": {}, |
447 | 447 | "source": [ |
448 | 448 | "## Numpyro Implementation" |
|
451 | 451 | { |
452 | 452 | "cell_type": "code", |
453 | 453 | "execution_count": null, |
454 | | - "id": "d439bca9", |
| 454 | + "id": "31c13fb0", |
455 | 455 | "metadata": { |
456 | 456 | "hide-output": false |
457 | 457 | }, |
|
487 | 487 | { |
488 | 488 | "cell_type": "code", |
489 | 489 | "execution_count": null, |
490 | | - "id": "824c49ab", |
| 490 | + "id": "adb302a5", |
491 | 491 | "metadata": { |
492 | 492 | "hide-output": false |
493 | 493 | }, |
|
508 | 508 | { |
509 | 509 | "cell_type": "code", |
510 | 510 | "execution_count": null, |
511 | | - "id": "c7a90e6b", |
| 511 | + "id": "0c0544ed", |
512 | 512 | "metadata": { |
513 | 513 | "hide-output": false |
514 | 514 | }, |
|
528 | 528 | { |
529 | 529 | "cell_type": "code", |
530 | 530 | "execution_count": null, |
531 | | - "id": "af313935", |
| 531 | + "id": "cf0a6a05", |
532 | 532 | "metadata": { |
533 | 533 | "hide-output": false |
534 | 534 | }, |
|
540 | 540 | { |
541 | 541 | "cell_type": "code", |
542 | 542 | "execution_count": null, |
543 | | - "id": "e8c0cd59", |
| 543 | + "id": "e92d224e", |
544 | 544 | "metadata": { |
545 | 545 | "hide-output": false |
546 | 546 | }, |
|
551 | 551 | }, |
552 | 552 | { |
553 | 553 | "cell_type": "markdown", |
554 | | - "id": "22fa8cd2", |
| 554 | + "id": "8179219e", |
555 | 555 | "metadata": {}, |
556 | 556 | "source": [ |
557 | 557 | "Next, we again compute the posterior under the assumption that $ y_0 $ is drawn from the stationary distribution, so that\n", |
|
566 | 566 | { |
567 | 567 | "cell_type": "code", |
568 | 568 | "execution_count": null, |
569 | | - "id": "a3c96c64", |
| 569 | + "id": "370c21dd", |
570 | 570 | "metadata": { |
571 | 571 | "hide-output": false |
572 | 572 | }, |
|
591 | 591 | { |
592 | 592 | "cell_type": "code", |
593 | 593 | "execution_count": null, |
594 | | - "id": "8c9a4d14", |
| 594 | + "id": "c7a45f37", |
595 | 595 | "metadata": { |
596 | 596 | "hide-output": false |
597 | 597 | }, |
|
611 | 611 | { |
612 | 612 | "cell_type": "code", |
613 | 613 | "execution_count": null, |
614 | | - "id": "7534b91d", |
| 614 | + "id": "59ddfc53", |
615 | 615 | "metadata": { |
616 | 616 | "hide-output": false |
617 | 617 | }, |
|
623 | 623 | { |
624 | 624 | "cell_type": "code", |
625 | 625 | "execution_count": null, |
626 | | - "id": "9072cde2", |
| 626 | + "id": "f847a997", |
627 | 627 | "metadata": { |
628 | 628 | "hide-output": false |
629 | 629 | }, |
|
634 | 634 | }, |
635 | 635 | { |
636 | 636 | "cell_type": "markdown", |
637 | | - "id": "856b04f8", |
| 637 | + "id": "725d0274", |
638 | 638 | "metadata": {}, |
639 | 639 | "source": [ |
640 | 640 | "Look what happened to the posterior!\n", |
|
649 | 649 | } |
650 | 650 | ], |
651 | 651 | "metadata": { |
652 | | - "date": 1764647436.8831613, |
| 652 | + "date": 1764675747.6672175, |
653 | 653 | "filename": "ar1_bayes.md", |
654 | 654 | "kernelspec": { |
655 | 655 | "display_name": "Python", |
|
0 commit comments