|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "492c603b", |
| 5 | + "id": "76edc254", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | 8 | "$$\n", |
|
13 | 13 | }, |
14 | 14 | { |
15 | 15 | "cell_type": "markdown", |
16 | | - "id": "c9174d55", |
| 16 | + "id": "f2e7c6ff", |
17 | 17 | "metadata": {}, |
18 | 18 | "source": [ |
19 | 19 | "# Posterior Distributions for AR(1) Parameters" |
20 | 20 | ] |
21 | 21 | }, |
22 | 22 | { |
23 | 23 | "cell_type": "markdown", |
24 | | - "id": "1b35cf83", |
| 24 | + "id": "10cd8d32", |
25 | 25 | "metadata": {}, |
26 | 26 | "source": [ |
27 | 27 | "# GPU\n", |
|
39 | 39 | { |
40 | 40 | "cell_type": "code", |
41 | 41 | "execution_count": null, |
42 | | - "id": "1d20718f", |
| 42 | + "id": "869961fc", |
43 | 43 | "metadata": { |
44 | 44 | "hide-output": false |
45 | 45 | }, |
|
50 | 50 | }, |
51 | 51 | { |
52 | 52 | "cell_type": "markdown", |
53 | | - "id": "e99889cf", |
| 53 | + "id": "35bb07b8", |
54 | 54 | "metadata": {}, |
55 | 55 | "source": [ |
56 | 56 | "In addition to what’s included in base Anaconda, we need to install the following packages" |
|
59 | 59 | { |
60 | 60 | "cell_type": "code", |
61 | 61 | "execution_count": null, |
62 | | - "id": "35448d27", |
| 62 | + "id": "caff5467", |
63 | 63 | "metadata": { |
64 | 64 | "hide-output": false |
65 | 65 | }, |
|
70 | 70 | }, |
71 | 71 | { |
72 | 72 | "cell_type": "markdown", |
73 | | - "id": "cc1b1723", |
| 73 | + "id": "78bebf16", |
74 | 74 | "metadata": {}, |
75 | 75 | "source": [ |
76 | 76 | "We’ll begin with some Python imports." |
|
79 | 79 | { |
80 | 80 | "cell_type": "code", |
81 | 81 | "execution_count": null, |
82 | | - "id": "1d871034", |
| 82 | + "id": "30ba54e6", |
83 | 83 | "metadata": { |
84 | 84 | "hide-output": false |
85 | 85 | }, |
|
103 | 103 | }, |
104 | 104 | { |
105 | 105 | "cell_type": "markdown", |
106 | | - "id": "9814f4ea", |
| 106 | + "id": "0e306b82", |
107 | 107 | "metadata": {}, |
108 | 108 | "source": [ |
109 | 109 | "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", |
|
199 | 199 | { |
200 | 200 | "cell_type": "code", |
201 | 201 | "execution_count": null, |
202 | | - "id": "0811ab8b", |
| 202 | + "id": "4f8a1810", |
203 | 203 | "metadata": { |
204 | 204 | "hide-output": false |
205 | 205 | }, |
|
229 | 229 | { |
230 | 230 | "cell_type": "code", |
231 | 231 | "execution_count": null, |
232 | | - "id": "bf347d83", |
| 232 | + "id": "d5c42c18", |
233 | 233 | "metadata": { |
234 | 234 | "hide-output": false |
235 | 235 | }, |
|
241 | 241 | }, |
242 | 242 | { |
243 | 243 | "cell_type": "markdown", |
244 | | - "id": "aff1c06d", |
| 244 | + "id": "b7c88d3b", |
245 | 245 | "metadata": {}, |
246 | 246 | "source": [ |
247 | 247 | "Now we shall use Bayes’ law to construct a posterior distribution, conditioning on the initial value of $ y_0 $.\n", |
|
253 | 253 | }, |
254 | 254 | { |
255 | 255 | "cell_type": "markdown", |
256 | | - "id": "f5e9259d", |
| 256 | + "id": "17466caf", |
257 | 257 | "metadata": {}, |
258 | 258 | "source": [ |
259 | 259 | "## PyMC Implementation\n", |
|
265 | 265 | { |
266 | 266 | "cell_type": "code", |
267 | 267 | "execution_count": null, |
268 | | - "id": "8291b7c6", |
| 268 | + "id": "7299fa1b", |
269 | 269 | "metadata": { |
270 | 270 | "hide-output": false |
271 | 271 | }, |
|
288 | 288 | }, |
289 | 289 | { |
290 | 290 | "cell_type": "markdown", |
291 | | - "id": "6ff91453", |
| 291 | + "id": "beb566f7", |
292 | 292 | "metadata": {}, |
293 | 293 | "source": [ |
294 | 294 | "[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:" |
|
297 | 297 | { |
298 | 298 | "cell_type": "code", |
299 | 299 | "execution_count": null, |
300 | | - "id": "cb352a71", |
| 300 | + "id": "af8c58f5", |
301 | 301 | "metadata": { |
302 | 302 | "hide-output": false |
303 | 303 | }, |
|
310 | 310 | { |
311 | 311 | "cell_type": "code", |
312 | 312 | "execution_count": null, |
313 | | - "id": "d5b58e39", |
| 313 | + "id": "0cdc0d00", |
314 | 314 | "metadata": { |
315 | 315 | "hide-output": false |
316 | 316 | }, |
|
322 | 322 | }, |
323 | 323 | { |
324 | 324 | "cell_type": "markdown", |
325 | | - "id": "7131dd66", |
| 325 | + "id": "b89cf500", |
326 | 326 | "metadata": {}, |
327 | 327 | "source": [ |
328 | 328 | "Evidently, the posteriors aren’t centered on the true values of $ .5, 1 $ that we used to generate the data.\n", |
|
337 | 337 | { |
338 | 338 | "cell_type": "code", |
339 | 339 | "execution_count": null, |
340 | | - "id": "91d6d808", |
| 340 | + "id": "05eb2dcf", |
341 | 341 | "metadata": { |
342 | 342 | "hide-output": false |
343 | 343 | }, |
|
351 | 351 | }, |
352 | 352 | { |
353 | 353 | "cell_type": "markdown", |
354 | | - "id": "82919756", |
| 354 | + "id": "e278380d", |
355 | 355 | "metadata": {}, |
356 | 356 | "source": [ |
357 | 357 | "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", |
|
368 | 368 | { |
369 | 369 | "cell_type": "code", |
370 | 370 | "execution_count": null, |
371 | | - "id": "a5286de4", |
| 371 | + "id": "ef4112cd", |
372 | 372 | "metadata": { |
373 | 373 | "hide-output": false |
374 | 374 | }, |
|
394 | 394 | { |
395 | 395 | "cell_type": "code", |
396 | 396 | "execution_count": null, |
397 | | - "id": "6b64a6f3", |
| 397 | + "id": "6725d3b0", |
398 | 398 | "metadata": { |
399 | 399 | "hide-output": false |
400 | 400 | }, |
|
409 | 409 | { |
410 | 410 | "cell_type": "code", |
411 | 411 | "execution_count": null, |
412 | | - "id": "f455a3ea", |
| 412 | + "id": "0886972e", |
413 | 413 | "metadata": { |
414 | 414 | "hide-output": false |
415 | 415 | }, |
|
422 | 422 | { |
423 | 423 | "cell_type": "code", |
424 | 424 | "execution_count": null, |
425 | | - "id": "398b46a1", |
| 425 | + "id": "9d2a328c", |
426 | 426 | "metadata": { |
427 | 427 | "hide-output": false |
428 | 428 | }, |
|
436 | 436 | }, |
437 | 437 | { |
438 | 438 | "cell_type": "markdown", |
439 | | - "id": "93d24fa6", |
| 439 | + "id": "8d7c72d0", |
440 | 440 | "metadata": {}, |
441 | 441 | "source": [ |
442 | 442 | "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", |
|
453 | 453 | }, |
454 | 454 | { |
455 | 455 | "cell_type": "markdown", |
456 | | - "id": "c8de54e6", |
| 456 | + "id": "352b8c08", |
457 | 457 | "metadata": {}, |
458 | 458 | "source": [ |
459 | 459 | "## Numpyro Implementation" |
|
462 | 462 | { |
463 | 463 | "cell_type": "code", |
464 | 464 | "execution_count": null, |
465 | | - "id": "6f3a8ed2", |
| 465 | + "id": "2e8f8bf7", |
466 | 466 | "metadata": { |
467 | 467 | "hide-output": false |
468 | 468 | }, |
|
498 | 498 | { |
499 | 499 | "cell_type": "code", |
500 | 500 | "execution_count": null, |
501 | | - "id": "cf8f427d", |
| 501 | + "id": "df1c6a9a", |
502 | 502 | "metadata": { |
503 | 503 | "hide-output": false |
504 | 504 | }, |
|
519 | 519 | { |
520 | 520 | "cell_type": "code", |
521 | 521 | "execution_count": null, |
522 | | - "id": "8c1f2e76", |
| 522 | + "id": "64fd94d4", |
523 | 523 | "metadata": { |
524 | 524 | "hide-output": false |
525 | 525 | }, |
|
539 | 539 | { |
540 | 540 | "cell_type": "code", |
541 | 541 | "execution_count": null, |
542 | | - "id": "8f83a30d", |
| 542 | + "id": "ba679ae9", |
543 | 543 | "metadata": { |
544 | 544 | "hide-output": false |
545 | 545 | }, |
|
551 | 551 | { |
552 | 552 | "cell_type": "code", |
553 | 553 | "execution_count": null, |
554 | | - "id": "62548f26", |
| 554 | + "id": "31bdf728", |
555 | 555 | "metadata": { |
556 | 556 | "hide-output": false |
557 | 557 | }, |
|
562 | 562 | }, |
563 | 563 | { |
564 | 564 | "cell_type": "markdown", |
565 | | - "id": "9645d714", |
| 565 | + "id": "60a2649e", |
566 | 566 | "metadata": {}, |
567 | 567 | "source": [ |
568 | 568 | "Next, we again compute the posterior under the assumption that $ y_0 $ is drawn from the stationary distribution, so that\n", |
|
577 | 577 | { |
578 | 578 | "cell_type": "code", |
579 | 579 | "execution_count": null, |
580 | | - "id": "f141fe08", |
| 580 | + "id": "dfc69b4f", |
581 | 581 | "metadata": { |
582 | 582 | "hide-output": false |
583 | 583 | }, |
|
602 | 602 | { |
603 | 603 | "cell_type": "code", |
604 | 604 | "execution_count": null, |
605 | | - "id": "b3d8d9e6", |
| 605 | + "id": "a64f57ce", |
606 | 606 | "metadata": { |
607 | 607 | "hide-output": false |
608 | 608 | }, |
|
622 | 622 | { |
623 | 623 | "cell_type": "code", |
624 | 624 | "execution_count": null, |
625 | | - "id": "68a444c0", |
| 625 | + "id": "913aee28", |
626 | 626 | "metadata": { |
627 | 627 | "hide-output": false |
628 | 628 | }, |
|
634 | 634 | { |
635 | 635 | "cell_type": "code", |
636 | 636 | "execution_count": null, |
637 | | - "id": "ff301b03", |
| 637 | + "id": "b612eaf4", |
638 | 638 | "metadata": { |
639 | 639 | "hide-output": false |
640 | 640 | }, |
|
645 | 645 | }, |
646 | 646 | { |
647 | 647 | "cell_type": "markdown", |
648 | | - "id": "c7556842", |
| 648 | + "id": "6c43c282", |
649 | 649 | "metadata": {}, |
650 | 650 | "source": [ |
651 | 651 | "Look what happened to the posterior!\n", |
|
660 | 660 | } |
661 | 661 | ], |
662 | 662 | "metadata": { |
663 | | - "date": 1772867702.5119815, |
| 663 | + "date": 1772937187.319557, |
664 | 664 | "filename": "ar1_bayes.md", |
665 | 665 | "kernelspec": { |
666 | 666 | "display_name": "Python", |
|
0 commit comments