-
Notifications
You must be signed in to change notification settings - Fork 23
/
3_3_ST-bayes_lin.tex
1204 lines (678 loc) · 30.7 KB
/
3_3_ST-bayes_lin.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%\documentclass[mathserif]{beamer}
\documentclass[handout]{beamer}
%\usetheme{Goettingen}
\usetheme{Warsaw}
%\usetheme{Singapore}
%\usetheme{Frankfurt}
%\usetheme{Copenhagen}
%\usetheme{Szeged}
%\usetheme{Montpellier}
%\usetheme{CambridgeUS}
%\usecolortheme{}
%\setbeamercovered{transparent}
\usepackage[english, activeacute]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage{dsfont}
\usepackage{graphics}
\usepackage{cases}
\usepackage{graphicx}
\usepackage{pgf}
\usepackage{epsfig}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{amstext}
\usepackage[ruled,vlined,lined]{algorithm2e}
\usepackage{amsmath}
\usepackage{epic}
\usepackage{epsfig}
\usepackage{fontenc}
\usepackage{framed,color}
\usepackage{palatino, url, multicol}
\usepackage{listings}
%\algsetup{indent=2em}
\vspace{-0.5cm}
\title{Bayesian Linear Regression}
\vspace{-0.5cm}
\author[Felipe Bravo Márquez]{\footnotesize
%\author{\footnotesize
\textcolor[rgb]{0.00,0.00,1.00}{Felipe José Bravo Márquez}}
\date{ \today }
\begin{document}
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Bayesian Linear Regression}
\scriptsize{
\begin{itemize}
\item In this class, which is mostly based on chapter 4 of \cite{mcelreath2020statistical}, we are going to revisit the linear regression model from a Bayesian point of view.
\item The idea is the same: to model the relationship of a numerical dependent variable $\mathbf{y}$ with $m$ independent variables $\mathbf{x}_1, \mathbf{x}_2, \dots, \mathbf{x}_m$ from a dataset $d$.
\item The response variable $\mathbf{y}$ is again modeled with a Gaussian distribution: $y_i \sim N(\mu_i,\sigma^2)$.
\item We also maintain the assumption that each attribute has a linear relationship to the mean of the outcome.
\begin{displaymath}
\mu_i = \beta_0 + \beta_1 x_i + \dots \beta_m x_m
\end{displaymath}
\item However, we are not going to use least squares or maximum likelihood to obtain point estimates of the parameters.
\item Instead, we are going to estimate the joint posterior distribution of all the parameters of the model:
\begin{displaymath}
f(\theta|d)= f(\beta_0,\beta_1,\dots,\beta_m,\sigma|d)
\end{displaymath}
\end{itemize}
}
\end{frame}
\begin{frame}{Bayesian Linear Models}
\scriptsize{
\begin{itemize}
\item The Bayesian linear regression is more flexible than least squares as it allows incorporating prior information.
\item It also allows to interpret the uncertainty of the model in a much richer way.
\item Notice that the parameters of the model are $\beta_0,\beta_1,\dots,\beta_m$ and $\sigma$ but not $\mu_i$.
\item This is because $\mu_i$ it is determined deterministically from the linear model's coefficients.
\item In order to build our posterior we need to define a likelihood function:
\begin{displaymath}
f(d|\beta_0,\beta_1,\cdots,\beta_m,\sigma) =\prod_{i=1}^n f(d_i|\beta_0,\beta_1,\cdots,\beta_m,\sigma)
\end{displaymath}
\item Where $d_i$ corresponds to each data point in the dataset $d$ containing $n$ values for $y$ and $x_1,\dots,x_m$ (IID assumption).
\item The likelihood of each point is modeled with a Gaussian distribution:
\begin{displaymath}
f(d_i|\beta_0,\beta_1,\cdots,\beta_m,\sigma)= N(\mu_i, \sigma^2)
\end{displaymath}
\end{itemize}
}
\end{frame}
\begin{frame}{Bayesian Linear Models}
\scriptsize{
\begin{itemize}
\item Now we need a joint prior density:
\begin{displaymath}
f(\theta)= f(\beta_0,\beta_1,\dots,\beta_m,\sigma)
\end{displaymath}
\item And the posterior gets specified as follows:
\begin{displaymath}
f(\theta|d)= \frac{ \prod_{i=1}^n f(d_i|\beta_0,\beta_1,\cdots,\beta_m,\sigma)*f(\beta_0,\beta_1,\dots,\beta_m,\sigma)}{f(d)}
\end{displaymath}
\item The evidence is expressed by a multiple integral:
\begin{displaymath}
f(d) = \int \int \dots \int \prod_{i=1}^n f(d_i|\beta_0,\beta_1,\cdots,\beta_m,\sigma)* f(\beta_0,\beta_1,\dots,\beta_m,\sigma) d\beta_0 d\beta_1 \cdots d\beta_md\sigma
\end{displaymath}
\item In most cases, the priors are specified independently for each parameter, which is equivalent to assuming:
\begin{displaymath}
f(\beta_0,\beta_1,\cdots,\beta_m,\sigma)=f(\beta_0)*f(\beta_1)*\dots*f(\beta_m)*f(\sigma).
\end{displaymath}
\end{itemize}
}
\end{frame}
\begin{frame}{A model of height revisited}
\scriptsize{
\begin{itemize}
\item To understand this more concretely, we will rebuild the linear model relating the height and weight of the !Kung San people using a Bayesian approach.
\item We will refer to each person's height and weight as $y_i$ and $x_i$ respectively.
\item Our probabilistic model specifying all components of a Bayesian model is defined as follows:
\vspace{0.3cm}
\begin{table}
\centering
\begin{tabular}{lr}
$y_i \sim N(\mu_i,\sigma)$ & [likelihood] \\
$\mu_i = \beta_0 + \beta_1 x_i$ & [linear model] \\
$\beta_0 \sim N(150,50)$ & [$\beta_0$ prior] \\
$\beta_1 \sim N(0,1)$ & [$\beta_1$ prior] \\
$\sigma \sim $ Uniform$(0,50)$ & [$\sigma$ prior] \\
\end{tabular}
\end{table}
\vspace{0.3cm}
\item Parameters $\beta_0$ and $\beta_1$ are the intercept and the slope of our linear model.
\item The parameter $\sigma$ is the standard deviation of all the heights.
\item Note that we are setting the same $\sigma$ for all observations, which is equivalent to the Homoscedasticity property of the standard linear regression.
\end{itemize}
}
\end{frame}
\begin{frame}{A model of height revisited}
\scriptsize{
\begin{itemize}
\item Our priors were set independently for each parameter which implies that the joint prior density $f(\beta_0,\beta_1,\sigma)$ can be expressed as $f(\beta_0)*f(\beta_1)*f(\sigma)$.
\item It should be kept in mind that the choice of priors is subjective and should be evaluated accordingly.\footnote{A guide to prior choice recommendations can be found here: \url{https://github.com/stan-dev/stan/wiki/Prior-Choice-Recommendations}}
\item Let's try to justify our choice a bit:
\begin{enumerate}
\scriptsize{
\item The Gaussian prior for $\beta_0$ (intercept), centered on 150cm with a standard variation of 50, covers a huge range of plausible mean heights for human populations while giving very little chance for negative heights. \vspace{0.2cm}
\item The Gaussian prior for $\beta_1$ (slope), centered on 0 with a standard variation of 1, acts as a \textbf{regularizer} to prevent the model from \textbf{overfitting} the data by assigning extreme values to $\beta_1$.\footnote{Regularization and overfitting will be discussed later in the course.} \vspace{0.2cm}
\item The uniform prior for the standard deviation $\sigma$ between 0 and 50 prohibits obtaining negative standard deviations. The upper bound (50 cm) would imply that 95\% of individual heights lie within 100cm of the average height. That's a very large range. \vspace{0.2cm}
}
\end{enumerate}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Fitting the Model}
\scriptsize{
\begin{itemize}
\item Now we need to fit the model to the data to build the posterior distribution.
\item Grid approximation is not a valid option, as setting up a grid for 3 parameters would be too computationally expensive.
\item We will use Laplace approximation instead.
\item In this approach we obtain the MAP estimates for each parameter using a hill-climbing \textbf{optimization} method.
\item Then we fit a \textbf{multivariate Gaussian distribution} centered on these values.
\item This distribution is the multidimensional extension to the standard Gaussian.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{The multivariate Gaussian distribution}
\scriptsize{
\begin{itemize}
\item The multivariate Gaussian distribution in $m$-dimensions is defined by the following density function (PDF):
\begin{displaymath}
f_x = \frac{1}{(2\pi)^{m/2}|\Sigma|^{1/2}}\exp \left( -\frac{1}{2}(\vec{x}-\vec{\mu})^T \Sigma^{-1}(\vec{x}-\mu)\right)
\end{displaymath}
\item This density function allows working with a $m$-dimensional vector of random variables $\vec{X}$.
\item The first parameter of this distributions is a mean vector $\vec{\mu} \in \mathcal{R}^m$ with the mean value of each dimension.
\item The second parameter is a covariance matrix $\Sigma \in R^{m\times m}$.
\item This matrix contains the variance of each variable in the diagonal and the covariance of variables $X_i$ and $X_j$ in the other cells $\Sigma_{i,j}$:
\begin{displaymath}
Cov(X) = \Sigma
\end{displaymath}
\item The matrix $\Sigma$ is symmetric and positive semi-definite.
\item The multivariate Gaussian $N(\vec{\mu},\Sigma)$ is a very convenient distribution for modeling multidimensional random variables.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{The multivariate Gaussian distribution}
\scriptsize{
\begin{itemize}
\item Here are some examples taken from \cite{ng2008generative} of what the density of a multivariate Gaussian distribution looks like:
\begin{figure}[h!]
\centering
\includegraphics[scale=0.4]{pics/mgaussian1.png}
\end{figure}
\item The left-most figure shows a Gaussian with mean zero (that is, the 2x1 zero-vector) and covariance matrix $\Sigma = I$ (the $2\times2$ identity matrix).
\item A Gaussian with zero mean and identity covariance is also called the standard normal distribution.
\item The middle figure shows the density of a Gaussian with zero mean and $\Sigma = 0.6I$.
\item The rightmost figure shows one with $\Sigma = 2I$.
\item We see that as $\Sigma$ becomes larger, the Gaussian becomes more ``spread-out'', and as it becomes smaller, the distribution becomes more ``compressed''.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{The multivariate Gaussian distribution}
\scriptsize{
\begin{figure}[h!]
\centering
\includegraphics[scale=0.4]{pics/mgaussian2.png}
\end{figure}
\begin{itemize}
\item The figures above show Gaussians with mean 0, and with covariance matrices respectively
\begin{figure}[h!]
\centering
\includegraphics[scale=0.35]{pics/mgaussian4.png}
\end{figure}
\item The leftmost figure shows the familiar standard normal distribution, and we see that as we increase the off-diagonal entry in $\Sigma$, the density becomes more ``compressed'' towards the $45\cdot$ line (given by $x_1 = x_2$).
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{The multivariate Gaussian distribution}
\scriptsize{
\begin{itemize}
\item As our last set of examples, fixing $\Sigma = I$, by varying $\vec{\mu}$ we can also move the mean of the density around.
\begin{figure}[h!]
\centering
\includegraphics[scale=0.25]{pics/mgaussian3.png}
\end{figure}
\item The figures above were generated using $\Sigma = I$, and respectively
\begin{figure}[h!]
\centering
\includegraphics[scale=0.3]{pics/mgaussian5.png}
\end{figure}
\end{itemize}
}
\end{frame}
\begin{frame}{Laplace approximation}
\scriptsize{
\begin{itemize}
\item In Laplace approximation we assume that the joint posterior follows a multivariate Gaussian distribution $f(\theta_1,\dots,\theta_m|d) = N(\vec{\mu},\Sigma)$.
\item According to \cite{gelman2013bayesian}, this approximation is convenient for unimodal and roughly symmetric posterior distributions.
\item There are also a theoretical asymptotic argument in favor of this approximation: ``if the dataset is large enough, a posterior distribution can be approximated by a Gaussian'' \cite{gelman2013bayesian}.
\item The values of $\vec{\mu}$ are obtained from the posterior mode of each parameter (MAP):
\begin{displaymath}
\vec{\mu} = \vec{\theta}_{MAP}
\end{displaymath}
\item Which are obtained using numeric optimization techniques.
\end{itemize}
}
\end{frame}
\begin{frame}{Laplace approximation}
\scriptsize{
\begin{itemize}
\item The values of $\Sigma$ are obtained from the curvature near these values, which are obtained from the second derivatives of the posterior:
\begin{displaymath}
\Sigma = [I({\theta}_{MAP})]^{-1}
\end{displaymath}
where \begin{displaymath}
I(\theta) = - \frac{d^2}{d\theta^2} \log f(\theta|d)
\end{displaymath}
\item Notice that both $\vec{\mu}$ and $\Sigma$ can be calculated from the unnormalized posterior
$f(d|\theta)*f(\theta)$
\item We don't need to calculate the evidence $f(d)$ to perform Laplace approximation \cite{laplaceApp}.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Fitting the Model}
\scriptsize{
\begin{itemize}
\item Laplace approximation is implemented in the \textbf{quap} function from the \textbf{rethinking} package.
\item The model for height defined above can be implemented as follows:
\begin{verbatim}
library(rethinking)
data(Howell1)
d <- Howell1
d2 <- d[ d$age >= 18 , ]
b.reg1 <- quap(
alist(
height ~ dnorm( mu, sigma ),
mu <- b0 + b1*weight,
b0 ~ dnorm( 150 , 50 ) ,
b1 ~ dnorm( 0 , 1) ,
sigma ~ dunif( 0 , 50 )
) , data=d2 )
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Fitting the Model}
\scriptsize{
\begin{itemize}
\item We can summarize this posterior with the command \textbf{precis}:
\begin{verbatim}
> precis( b.reg1, prob=0.95 )
mean sd 2.5% 97.5%
b0 113.99 1.90 110.26 117.71
b1 0.90 0.04 0.82 0.98
sigma 5.07 0.19 4.70 5.45
\end{verbatim}
\item These numbers provide Gaussian approximations for each parameter's \textbf{marginal} posterior distribution.
\item The marginal of multivariate distribution is the univariate distribution of a single parameter $\theta_i$ after integrating (averaging) over all the other parameters $\theta_j \ \forall j \neq i$:
\begin{displaymath}
f(\theta_i |d) = \int \dots \int f(\theta_1,\dots,\theta_m|d)d\theta_1,\dots,d\theta_{i-1},d\theta_{i+1},\dots,d\theta_{m}
\end{displaymath}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Fitting the Model}
\scriptsize{
\begin{itemize}
\item In a multivariate Gaussian $N(\vec{\mu},\Sigma)$, the marginal distribution of $\theta_i$ is a univariate Gaussian $N(\vec{\mu}_i,\Sigma_{ii})$.
\item So, the marginal distribution of $\beta_1$ is a Gaussian distribution with $\mu=0.9$ and $\sigma=0.04$.
\item The last two columns of the summary table show the lower and upper limits of a 95\% equal-tailed credible interval for each parameter.
\item For the case of $\beta_1$, 95\% of the posterior probability lies between 0.82 and 0.98.
\item That suggests that $\beta_1$ values close to zero or greatly above one are highly incompatible with these data and this model.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Fitting the Model}
\scriptsize{
\begin{itemize}
\item Let's compare the mean and standard deviation of $\beta_0$ and $\beta_1$ with what we obtained using least squares in previous lecture:
\begin{verbatim}
> summary(reg1)
...
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 113.87939 1.91107 59.59 <2e-16 ***
weight 0.90503 0.04205 21.52 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 5.086 on 350 degrees of freedom
....
\end{verbatim}
\item These values are almost the same!
\item Recall that maximum likelihood or least squares estimators are identical to MAP estimators with uniform priors.
\item This indicates that our priors did not have a significant impact in the resulting posterior.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Covariance Matrix}
\scriptsize{
\begin{itemize}
\item We can also get the covariance matrix $\Sigma$ of our multivariate Gaussian obtained with Laplace approximation:
\begin{verbatim}
> vcov( b.reg1 )
b0 b1 sigma
b0 3.620141920 -7.884254e-02 1.765417e-03
b1 -0.078842537 1.752477e-03 -3.830179e-05
sigma 0.001765417 -3.830179e-05 3.654305e-02
\end{verbatim}
\item The matrix tell us how each parameter relates to every other parameter in the posterior distribution.
\item It is better to turn them into correlations for easier interpretation:
\begin{verbatim}
> cov2cor( vcov( b.reg1 ) )
b0 b1 sigma
b0 1.000000000 -0.989855066 0.004853802
b1 -0.989855066 1.000000000 -0.004786196
sigma 0.004853802 -0.004786196 1.000000000
\end{verbatim}
\item Notice that $\beta_0$ and $\beta_1$ are almost perfectly negatively correlated.
\item This means that these two parameters carry the same information: as we change the slope of the line, the best intercept changes to match it.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Centering}
\scriptsize{
\begin{itemize}
\item In more complex models, strong correlations like this can make it difficult to fit the model to the data.
\item A useful trick to avoid this problem is centering: subtract the mean of a variable from each value.
\begin{verbatim}
d2$weight.c <- d2$weight - mean(d2$weight)
b.reg2 <- quap(
alist(
height ~ dnorm( b0 + b1*weight.c, sigma ),
b0 ~ dnorm( 150 , 50 ) ,
b1 ~ dnorm( 0 , 1) ,
sigma ~ dunif( 0 , 50 )
) , data=d2 )
> cov2cor( vcov( b.reg2 ) )
b0 b1 sigma
b0 1.000000e+00 6.901851e-08 -2.435168e-05
b1 6.901851e-08 1.000000e+00 -2.860675e-03
sigma -2.435168e-05 -2.860675e-03 1.000000e+00
\end{verbatim}
\item The correlations among parameters are now all very close to zero.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Sampling from the Posterior}
\scriptsize{
\begin{itemize}
\item In the remainder of this class we will work with samples from our multi-dimensional posterior.
\item The rethinking package provides the \textbf{extract.samples} function to obtain them:
\begin{verbatim}
> post <- extract.samples( b.reg1, n= 1e4 )
> head(post)
b0 b1 sigma
1 112.6949 0.9241772 5.379502
2 114.0312 0.9049003 5.213418
3 114.5932 0.8867978 5.057051
4 111.9088 0.9522044 5.021026
5 111.7631 0.9496914 4.886909
6 116.6987 0.8367835 5.256882
\end{verbatim}
\item Each sample is a different line relating height and weight.
\item Bear in mind that each line is sampled in proportion to the posterior probabilities, which represent our model's beliefs.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Sampling from the Posterior}
\scriptsize{
\begin{itemize}
\item We can see that the mean of each variable in the sample is almost identical to the corresponding MAP estimate:
\begin{verbatim}
> sapply(post,mean)
b0 b1 sigma
114.0026648 0.9023462 5.0693261
\end{verbatim}
\item Since our posterior was approximated with Laplace approximation, we can obtain equivalent samples directly from a multi-variate Gaussian using the \textbf{mvrnorm} function:
\begin{verbatim}
> library(MASS)
> post2 <- mvrnorm( n=1e4,mu=coef(b.reg1),
Sigma=vcov(b.reg1 ) )
> post2<-as.data.frame(post2)
> sapply(post2,mean)
b0 b1 sigma
113.9997660 0.9023735 5.0691332
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Plotting posterior inference against the data}
\scriptsize{
\begin{itemize}
\item In truth, tables of estimates are usually insufficient for understanding the information contained in the posterior distribution.
\item It's almost always much more useful to plot the posterior inference against the data.
\item We’re going to start with a simple version of that task, superimposing just the MAP values over the height and weight data.
\begin{verbatim}
plot( height ~ weight , data=d2 , col=rangi2 )
b0_map <- mean(post$b0)
b1_map <- mean(post$b1)
curve( b0_map + b1_map*x, add=TRUE )
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}{Plotting posterior inference against the data}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.65]{pics/height_points.pdf}
\end{figure}
\end{frame}
\begin{frame}[fragile]{Plotting regression intervals}
\scriptsize{
\begin{itemize}
\item The previous plot doesn't incorporate the uncertainty embodied in the posterior.
\item This can be done by by plotting a credible interval around the MAP regression line.
\item To understand how it works, let's focus for the moment on a single weight value, say 50 kilograms.
\item We can quickly make a list of 10,000 values of $\mu$ for an individual who weighs 50 kilograms, by using our samples from the posterior:
\begin{verbatim}
mu_at_50 <- post$b0 + post$b1 * 50
\end{verbatim}
\item It might be helpful at this point to actually plot the density for this vector of means:
\begin{verbatim}
dens( mu_at_50 , col=rangi2 , lwd=2 , xlab="mu|weight=50" )
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}{Plotting regression intervals}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.45]{pics/mu50.pdf}
\end{figure}
\end{frame}
\begin{frame}[fragile]{Plotting regression intervals}
\scriptsize{
\begin{itemize}
\item Since the components of $\mu$ have distributions ($\beta_0,\beta_1$), so too does $\mu$.
\item And since the distributions of $\beta_0$ and $\beta_1$ are Gaussian, so to is the distribution of $\mu$.
\item Adding Gaussian distributions always produces a Gaussian distribution.
\item Since the posterior for $\mu$ is a distribution, you can find intervals for it, just like for any posterior distribution.
\item To find the 95\% highest posterior density interval of $\mu$ at 50 kg, we just use the HPDI command as usual:
\begin{verbatim}
> HPDI( mu_at_50 , prob=0.95 )
|0.95 0.95|
158.4883 159.8061
\end{verbatim}
\item Now, we need to repeat the above calculation for every weight value on the horizontal axis, not just when it is 50 kg.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Plotting regression intervals}
\scriptsize{
\begin{itemize}
\item We first define a sequence of weights to compute predictions for:
\begin{verbatim}
weight.seq <- seq( from=25 , to=70 , by=1 )
\end{verbatim}
\item These values will be on the horizontal axis of our plot.
\item Now, for each of these weights we will compute a posterior distribution of the average height $\mu$ using samples from the posterior:
\begin{verbatim}
mu.link <- function(weight) post$b0 + post$b1*weight
\end{verbatim}
\item The function above will use the $10,000$ different sampled regression lines to compute various values of $\mu$ for a given weight.
\item We can apply this function to all our 46 weights (from 25 to 70) and obtain a matrix of $10,000 \times 46$:
\begin{verbatim}
> mu <- sapply( weight.seq , mu.link )
> dim(mu)
[1] 10000 46
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Plotting regression intervals}
\scriptsize{
\begin{itemize}
\item Now, we can get a MAP value of $\mu$ for each weight, simply by by averaging the rows of the matrix:
\begin{verbatim}
> mu.mean <- apply( mu , 2 , mean )
> head(mu.mean)
[1] 136.5526 137.4555 138.3583 139.2612 140.1641 141.0669
\end{verbatim}
\item And we can get our 95\% HPDIs analogously:
\begin{verbatim}
mu.HPDI <- apply( mu , 2 , HPDI , prob=0.95 )
\end{verbatim}
\item The variable mu.HPDI contains 95\% lower and upper bounds for each weight value.
\item Now we can plot these intervals as shaded regions around the MAP estimates as follows:
\begin{verbatim}
plot( height ~ weight , data=d2 , col=col.alpha(rangi2,0.5) )
# plot the MAP line, aka the mean mu for each weight
lines( weight.seq , mu.mean )
# plot a shaded region for 95% HPPDI
shade( mu.HPDI , weight.seq )
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}{Plotting regression intervals}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.9]{pics/height_interval.pdf}
\end{figure}
\end{frame}
\begin{frame}[fragile]{Plotting regression intervals}
\scriptsize{
\begin{itemize}
\item In case of working with more complex linear models (i.e., multiple attributes, interactions) we can use the \textbf{link} function from the rethinking package:
\begin{verbatim}
mu <- link(b.reg1 ,data=data.frame(weight=weight.seq), n=1e4)
\end{verbatim}
\item This function will generate distributions of posterior values for $\mu$ using the formula declared in \textbf{quap} (e.g., $\mu= \beta_0 + \beta_1 * x$).
\item The default behavior of link is to use the original data.
\item So we have to pass it a list of new horizontal axis values we want to plot posterior predictions across.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Prediction Intervals}
\scriptsize{
\begin{itemize}
\item Now let’s walk through generating prediction intervals for simulated heights $\tilde{y}$ using the posterior predictive distribution.
\item What we did before was to use samples from the posterior to visualize the uncertainty in $\mu_i$ using the linear model of the mean: $\mu_i=\beta_0+\beta_1*x_i$.
\item But actual predictions of heights $\tilde{y}$ depend also upon the stochastic definition of $y$ given in the likelihood function: $y_i\sim N(\mu_i,\sigma)$.
\item The Gaussian distribution of the likelihood tells us that the model expects observed heights $\tilde{y}$ to be distributed around $\mu$, not right on top of it.
\item And the spread around $\mu$ is governed by $\sigma$.
\item All of this suggests we need to incorporate $\sigma$ in the predictions somehow.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Prediction Intervals}
\scriptsize{
\begin{itemize}
\item The posterior predictive distribution introduced in previous class can exactly do that:
\begin{displaymath}
f(\tilde{y}|y) = \int_{\theta}f(\tilde{y}|\theta)f(\theta|y)d\theta
\end{displaymath}
\item For our height regression model it would take the following form: