forked from vasishth/MScStatisticsNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesianDataAnalysis.Rnw
1447 lines (1075 loc) · 39.6 KB
/
BayesianDataAnalysis.Rnw
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[12pt]{article}
%\usepackage[landscape]{geometry}
\usepackage[landscape,hmargin=2cm,vmargin=1.5cm,headsep=0cm]{geometry}
% See geometry.pdf to learn the layout options. There are lots.
\geometry{a4paper} % ... or a4paper or a5paper or ...
%\geometry{landscape} % Activate for for rotated page geometry
%\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{epstopdf}
\usepackage{multicol}
\newcommand{\argmin}{\arg\!\min}
\newcommand{\argmax}{\arg\!\max}
\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}
\newtheorem{fact}{Fact}
\newtheorem{proposition}{Proposition}
% Turn off header and footer
\pagestyle{plain}
% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%x
{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
{-1explus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%
{\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\small\bfseries}}
\makeatother
% Define BibTeX command
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
% Don't print section numbers
\setcounter{secnumdepth}{0}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}
\usepackage{Sweave}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}
%% taken from http://brunoj.wordpress.com/2009/10/08/latex-the-framed-minipage/
\newsavebox{\fmbox}
\newenvironment{fmpage}[1]
{\begin{lrbox}{\fmbox}\begin{minipage}{#1}}
{\end{minipage}\end{lrbox}\fbox{\usebox{\fmbox}}}
\usepackage{mathtools}
\makeatletter
\newcommand{\explain}[2]{\underset{\mathclap{\overset{\uparrow}{#2}}}{#1}}
\newcommand{\explainup}[2]{\overset{\mathclap{\underset{\downarrow}{#2}}}{#1}}
\makeatother
\SweaveOpts{prefix.string=LMMfigs/LMMfig}
\SweaveOpts{cache=TRUE}
\title{Bayesian Data Analysis Summary}
\author{Shravan Vasishth ([email protected])}
%\date{} % Activate to display a given date or no date
\begin{document}
\SweaveOpts{concordance=TRUE}
\footnotesize
\maketitle
\tableofcontents
\newpage
\begin{multicols}{2}
% multicol parameters
% These lengths are set only within the two main columns
%\setlength{\columnseprule}{0.25pt}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}
\begin{center}
\normalsize{Bayesian Data Analysis Summary Sheet} \\
\footnotesize{
Compiled by: Shravan Vasishth ([email protected])\\
Version dated: \today}
\end{center}
<<echo=F>>=
options(width=60)
options(continue=" ")
@
\section{Basic math}
The gamma function is: $\Gamma(\lambda)=\int_0^{\infty} y^{y-1} e^{-y}\,dy
Stirling's approximation for $\Gamma(\lambda+1)$:
\begin{equation}
\Gamma(\lambda+1) \approx \sqrt{2\pi} \lambda^{\lambda+1/2} \exp(-\lambda) \quad \lambda\rightarrow \infty
\end{equation}
\section{Bayes theorem}
\begin{equation}
P(A\mid B) = \frac{P(B\mid A) P(A)}{P(B)}
\end{equation}
\begin{equation}
f(\theta\mid x) = \frac{f(x\mid \theta) f(\theta)}{f(y)}
\quad f(x) = \int f(x,\theta)\, d \theta = \int f(x\mid \theta)f(\theta)\, d \theta
\end{equation}
\section{Independence and conditional independence}
When we sample $x_i \sim N(\mu,\sigma^2)$, frequentists would assume the $x_i$ are independent because the parameters are constants. But in the Bayesian setting, the $x_i$ are conditionally independent: $x_i\mid \theta,\sigma^2$; this is because once we have observed $x_1$, we have learnt something about the parameters, our beliefs about them change, and therefore our beliefs about an unobserved $x_j$ also change. If all parameters are known then the $x_i$ are independent even in the Bayesian setting.
\section{Eliciting priors}
\subsection{Location and dispersion elicitation}
Elicit m, elicit 95\% credible region, i.e., [m-a,m+a], then $a/2$ is SD, and $(a/2)^2$ (assuming normal distrn).
\subsection{Bisection method}
Elicit 25th, 50th, 75th percentile. More generally, to elicit an N(m,v) distribution to someone's beliefs about a parameter, in theory we need two percentiles, say $P(\theta< x_1) = p_1$ and $P(\theta> x_1) = p_2$. We can then find m and v as solutions of
\begin{equation}
\int_{-\infty}^{x_1} \frac{1}{\sqrt{2\pi v}}\exp \left\{ - \frac{1}{2v} (m-\theta)^2 \right\}\, d\theta = p_1
\end{equation}
\begin{equation}
\int_{-\infty}^{x_2} \frac{1}{\sqrt{2\pi v}}\exp \left\{ - \frac{1}{2v} (m-\theta)^2 \right\}\, d\theta = p_1
\end{equation}
Because people are inaccurate in delivering percentiles, eliciting several percentiles and minimizing the following quantity may be better:
\begin{equation}
\sum [\int_{-\infty}^{x_i} \frac{1}{\sqrt{2\pi v}}\exp \left\{ - \frac{1}{2v} (m-\theta)^2 \right\}\, d\theta - p_i]
\end{equation}
\subsection{Example: correlated variables}
\begin{equation}
X, Y \sim MVN\left(
\left[\begin{array}{c}
\mu_X \\
\mu_Y \\
\end{array} \right],
\left[ \begin{array}{cc}
\sigma_X^2 & \rho\\
\rho & \sigma_Y^2 \\
\end{array} \right]
\right)
\end{equation}
Elicit $\mu$ by asking for median and setting it to $m$. Elicit the $\sigma^2$ by obtaining the 95\% percentile p and setting $s^2= ((p-m)/1.65)^2$.
For $\rho$, ask for percentiles of the difference between X and Y. The difference $X-Y$ would also have a normal distribution under the bivariate normal model, and so by eliciting a 50th and 95th percentile we could determine $\sigma_{X-Y}^2=Var(X-Y)$, then use:
\begin{equation}
Var(X-Y) = Var(X) + Var(Y) - 2Cov(X,Y)
\end{equation}
so
\begin{equation}
\rho = \frac{\sigma_X^2 + \sigma_Y^2 - \sigma^2_{X-Y}}{2}
\end{equation}
Use feedback to check if expert agrees with the implications of their priors.
\section{Integrating out an unknown parameter}
Suppose we have a sampling distribution $p(y\mid \theta)$.
This could be, for example, $y\sim N(0,1)$ (here, $\theta$ is a vector representing the mean and variance). Let our certainty about the parameters be expressed by the probability distribution $p(\theta)$. In this situation, we can produce a posterior predictive distribution by ``integrating out the unknown parameter'':
\begin{equation}
p(y)= \int p(y,\theta)\, d\theta = \int p(y\mid \theta) p(\theta)\, d\theta
\end{equation}
This kind of ``integrating out'' can be done in BUGS quite easily. Suppose we have a random variable Y that has a binomial distribution with success probability $\theta$ and sample size $n$, and our uncertainty about $\theta$ is expressed as the distribution Beta(a,b). For specific values of a,b, and n (see code below), we can write the model as:
<<>>=
cat("
model
{
theta ~ dbeta(3,27)
Y ~ dbin(theta,20)
}",
file="JAGSmodels/integratingout.jag")
@
Then we generate samples from $p(Y)$ in the usual way.
An alternative to this is the ``plug-in principle''. See~\pageref{plugin}.
\section{Prior predictive distributions}
The prior predictive distribution is the distribution of observations we expect before we observe any data.
It is useful for assessing whether the choice of prior distribution captures our prior beliefs.
\subsection{Example 1: Gamma-Exponential (Machine failure times)}
Let machine failure times $t_1,\dots,t_n$ be $t_i \mid \lambda \sim Exp(\lambda)$, for $i=1,\dots,n$, and $\lambda \sim Gamma(a,b)$.
Prove that the \textbf{prior predictive density} of $t_1,\dots,t_n$ is
\begin{equation}
f(t_1,\dots,t_n) = \frac{b^a \Gamma(a+n)}{\Gamma(a)(b+\sum t_i)^{a+b}}
\end{equation}
\begin{equation}
\begin{split}
f(t_1,\dots,t_n) =& \int p(t_1)p(t_2)\dots p(t_n) p(\lambda)\,d\lambda\\
=& \int \lambda\exp(-\lambda t_1)\lambda\exp(-\lambda t_2)\dots\lambda\exp(-\lambda t_n) \frac{b^a \lambda^{a-1}\exp(-b\lambda)}{\Gamma(a)} \, d \lambda\\
=& \int \lambda^{n+a-1} \exp(-\lambda \sum t_i - b\lambda) \frac{b^a}{\Gamma(a)}\, d\lambda\\
=& \int \lambda^{n+a-1} \exp(-\lambda( \sum t_i + b)) \frac{b^a}{\Gamma(a)}\, d\lambda
\end{split}
\end{equation}
Now, a $Gamma(n+a,\sum t_i + b)$ is:
\begin{equation}
f(\lambda)=\frac{(\sum t_i + b)^{a+n} \lambda^{a+n-1} \exp(-b\lambda)}{\Gamma(a+n)}
\end{equation}
We can rewrite the above integral as follows.
\begin{equation}
\begin{split}
~& \int \lambda^{n+a-1} \exp(-\lambda( \sum t_i + b)) \frac{b^a}{\Gamma(a)}\, d\lambda\\
=&
\frac{\Gamma(a+n)b^{a}}{\Gamma(a)(b+\sum t)^{a+n}}
\int
\left[
\frac{(\sum t_i + b)^{a+n} \lambda^{a+n-1} \exp(-b\lambda)}{\Gamma(a+n)}
\right]\, d \lambda
\end{split}
\end{equation}
The integral integrates to $1$, leaving us with:
\begin{equation}
\frac{\Gamma(a+n)b^{a}}{\Gamma(a)(b+\sum t)^{a+n}}
\end{equation}
as required.
\section{Posterior predictive distributions}\label{postpredexample}
This section is taken from Lunn et al., Section 3.2; slightly reworded.
Once we have the posterior distribution $f(\theta\mid y)$, we can derive the predictions based on this posterior distribution using the same trick as above.
\begin{equation}
p(y_{pred}\mid y ) = \int p(y_{pred}, \theta\mid y)\, d\theta= \int
p(y_{pred}\mid \theta,y)p(\theta\mid y)\, d\theta
\end{equation}
Assuming that past and future observations are conditionally independent given $\theta$, i.e., $p(y_{pred}\mid \theta,y)= p(y_{pred}\mid \theta)$, we can write:
\begin{equation}
p(y_{pred}\mid y )=\int p(y_{pred}\mid \theta) p(\theta\mid y)\, d\theta
\end{equation}
Note that we are conditioning $y_{pred}$ only on $y$, we do not condition on what we don't know ($\theta$); we integrate out the unknown parameters.
Cf.\ the frequentist approach, which gives only a predictive distribution of $y_{pred}$ given our estimate of $\theta$ (a point value).
We use this in the next example.
\subsection{Example 1: Beta-Binomial}
X: no.\ successes in n trials, $X\sim Bin(n,\theta)$, $\theta$ unknown. Prior: $Beta(a,b)$. Say we observe $X=x$. What is the distribution of $Y$, the number of successes $y$ in a further $m$ trials?
If $m=1$, just take the posterior mean. If $m>1$:
\begin{equation}
p(y_{pred}\mid X=x) = \int_0^1 p(y_{pred}\mid \theta) p(\theta\mid X=x)\, d\theta
\end{equation}
Since $p(y_{pred}\mid \theta) = {m \choose y} \theta^y (1-\theta)^{y-m}$, and $p(\theta\mid X=x)=Beta(a+x,b+n-x)$, just plug in the values, and simplify.
We get the beta-binomial:
\begin{equation}
f(Y=y\mid X=x) = {n \choose k}\frac{B(a+x+y,b+m+n-x-y)}{B(a+x,b+n-x)}
\end{equation}
\subsection{Example 2: Beta-Geometric}
For n RVs from a geometric distribution,
the likelihood is $p(x)=\theta(1-\theta)^{\sum x_i}$, where $x_i=0,1,2,\dots$. Let the prior be: Beta(a,b).
The posterior is:
\begin{equation}
\theta(1-\theta)^x \frac{1}{B(a,b)} \theta^{a-1} (1-\theta)^{b-1}=
\frac{1}{B(a,b)} \theta^{a+1-1} (1-\theta)^{b+\sum x-1}
\end{equation}
Posterior: $Beta(a+1,b+\sum x)$.
Posterior predictive distribution: $P(Y=y\mid X=x)$.
\begin{equation}
\begin{split}
P(Y=y\mid X=x) =& \int_0^1 p(y\mid \theta)p(\theta\mid x)\, d \theta\\
=& \frac{1}{B(a,b)} \int_0^1 \theta (1-\theta)^y \theta^{a+1-1} (1-\theta)^{b+\sum x-1}\, d \theta\\
=& \frac{1}{B(a,b)} \int_0^1
\theta^{a+2-1} (1-\theta)^{b+\sum x+y-1}\, d \theta
\end{split}
\end{equation}
Using the standard trick to get rid of the integral:
\begin{equation}
\begin{split}
~& \frac{1}{B(a,b)} \int_0^1
\theta^{a+2-1} (1-\theta)^{b+\sum x+y-1}\,d \theta \\
=&\frac{B(a+2,b+\sum x+y)}{B(a,b)}
\int_0^1
\frac{\theta^{a+2-1} (1-\theta)^{b+\sum x+y-1}}{B(a+2,b+\sum x+y)}\,d \theta\\
=& \frac{B(a+2,b+\sum x+y)}{B(a,b)}
\end{split}
\end{equation}
The last line arises because the integral on the right, $\int_0^1
\frac{\theta^{a+2-1} (1-\theta)^{b+\sum x+y-1}}{B(a+2,b+\sum x+y)}\,d \theta$, sums to 1 as it is now a pdf.
\subsection{Example 3: Inverse Gamma (from Ex.\ 2)}
Given that $Y,x_1,\dots,x_n \sim N(\mu,\sigma^2)$, where $\mu$ is known.
A prior on $\sigma^2$ is defined as $\sigma^2 \sim IG(d,a)$.
IG above refers to Inverse Gamma, which is defined as:
\begin{equation}
f(\theta) = \int_{-\infty}^{\infty} \frac{a^d\theta^{-(d+1)} \exp\{-\frac{a}{\theta}\} }{\Gamma(d)}\,d\theta
\end{equation}
Show that the posterior distribution is $\sigma^2 \sim IG(d_{*},a_{*})$, where
\begin{equation}
d_{*}=d + \frac{n}{2} \quad a_{*} = a + \frac{\sum_i^n (x_i - \mu)^2}{2}
\end{equation}
Likelihood: $x_1,\dots,x_n \sim N(\mu,\sigma^2)$ (assume $\mu$ known).
Prior: IG(d,a).
Posterior is:
\begin{equation}
\frac{1}{\sqrt{2\pi \sigma^2}} \exp(-\frac{1}{2\sigma^2}\sum(x_i-\mu)^2) \times
\frac{a^d\theta^{-(d+1)} \exp\{-\frac{a}{\theta}\} }{\Gamma(d)}
\end{equation}
Now we just need to collect the terms together and rearrange them a bit to get the posterior as $IG(d_{*},a_{*})$.
Next, derive the posterior predictive distribution given the above information. Let $\theta=\sigma^2$, and let $X=x_1,\dots,x_n$. The posterior predictive distribution is:
\begin{equation}
\begin{split}
f(Y\mid X, \mu, \theta) =& \int f(Y,\theta,\mu\mid X)\, d\theta\\
=& \int f(Y\mid \theta,\mu, X)f(\theta\mid \mu, X)\,d\theta
\end{split}
\end{equation}
Due to the conditional independence of Y and X, we can
write $f(Y\mid \theta,\mu, X)$ as $f(Y\mid \theta,\mu)$.
Thus, we can expand the terms out as follows:
\begin{equation}
\begin{split}
f(Y\mid X, \mu, \theta) =& \int f(Y\mid \theta,\mu)f(\theta\mid \mu, X)\,d\theta \\
=& \int \left[ \frac{1}{\sqrt{2\pi}}\theta^{-1/2}
\exp\{-\frac{(Y-\mu)^2}{2\theta} \} \right]
\left[
\frac{a_{*}^{d_{*}}\theta^{-(d_{*}+1)} \exp\{-\frac{a_{*}}{\theta}\} }{\Gamma(d_{*})}
\right] \,d\theta
\end{split}
\end{equation}
Rearranging terms and simplifying:
\begin{equation}
%\begin{split}
\int \frac{1}{\Gamma(d_{*})}
\frac{a_{*}^{d_{*}}}{\sqrt{2\pi}}
\theta^{-(d_{*} + 1) - \frac{1}{2}}
\exp\left\{ \frac{-(Y-\mu)^2}{2\theta} - \frac{a_{*}}{\theta} \right\} \,d\theta
%\end{split}
\end{equation}
Let $A=\frac{(Y-\mu)^2}{2} + a_{*}$ and let $D=d_{*}+\frac{1}{2}$. These abbreviations allow us to write the above as an inverse gamma PDF (adding the appropriate proportionality constant $\frac{\Gamma(D)}{A^D}$), which sums to 1:
\begin{equation}
\frac{1}{\Gamma(d_{*})}
\frac{a_{*}^{d_{*}}}{\sqrt{2\pi}}
\left[
\frac{\Gamma(D)}{A^D}
\int \frac{A^D}{\Gamma(D)} \theta^{-(D+1)} \exp\left\{ -\frac{A}{\theta} \right\} \,d\theta
\right]
\end{equation}
Since
\begin{equation}
\int \frac{A^D}{\Gamma(D)} \theta^{-(D+1)} \exp\left\{ -\frac{A}{\theta} \right\} \,d\theta=1
\end{equation}
\noindent
we are left with:
\begin{equation}
f(Y\mid X,\mu,\theta)=
\frac{1}{\Gamma(d_{*})}
\frac{a_{*}^{d_{*}}}{\sqrt{2\pi}}
\frac{\Gamma(D)}{A^{D}}
\end{equation}
As an aside, we note that with a little rearrangement, this will look like the t-distribution.
\subsubsection{Posterior predictive density: Lik$\times$Posterior}
The posterior predictive density can be computed using the above formula:
\begin{equation}
f(Y\mid X,\mu,\theta)=
\frac{\Gamma(D)}{\Gamma(d_{*})}
\frac{1}{\sqrt{2\pi}}
\frac{a_{*}^{d_{*}}}{A^{D}}
\end{equation}
\subsubsection{Prior predictive density: Lik$\times$Prior}
The prior predictive density can be computed from the prior and likelihood (here, $\theta$ is $\sigma^2$):
\begin{equation}
p(Y) = \int p(Y\mid\mu, \theta) p(\theta \mid \mu)\, d\theta
\end{equation}
Using the same reasoning as above, this reduces to
\begin{equation}
f(Y\mid \mu,\theta)=
\frac{\Gamma(D)}{\Gamma(d)}
\frac{1}{\sqrt{2\pi}}
\frac{a^{d}}{A^{D}}
\end{equation}
\noindent
where $A= \frac{(Y-\mu)^2}{2}+a$ and $D=d+\frac{1}{2}$.
\subsection{Example 4: Exponential-Gamma}
See later section on Conjugate forms (page~\pageref{conjugateformsexpgamma}).
\subsection{Computing posterior predictive distributions using JAGS}
\label{exampleposteriorpredictive}
Suppose our prior is Beta(2,2) and the data are
Beta(46,54), and the posterior is
Beta(47,55).
We first define the data and model. The data includes the parameters for the prior as well (a,b), and the sample size of the predicted values.
<<>>=
## the data:
data<-list(a=3,b=27,y=0,n=10,n.pred=20)
cat("
model
{
## prior
theta ~ dbeta(a,b)
## likelihood
y ~ dbin(theta,n)
## predicted posterior distribution
y.pred ~ dbin(theta,n.pred)
}",
file="JAGSmodels/predictionexample1.jag" )
@
\section{Conjugate forms}\label{conjugateforms}
Definition of conjugacy from notes:
\begin{quote}
Given the likelihood $f(x\mid \theta)$, if the prior $f(\theta)$ results in a posterior $f(\theta\mid x)$ that has the same form as $f(\theta)$, then we call $f(\theta)$ a conjugate prior.
\end{quote}
Conjugate priors can be found if the likelihood function is a member of the exponential family.
\subsection{Beta-Binomial}
P.\ 38 in notes. Posterior is $Beta(a+x,b+n-x)$.
The posterior predictive distribution of future data $y\mid x$ is (see lecture notes)
\begin{equation}
f(Y=y\mid X=x) = {n \choose k}\frac{B(a+x+y,b+m+n-x-y)}{B(a+x,b+n-x)}
\end{equation}
Posterior parameters as a weighted mean:
\begin{equation}
E[\theta\mid x] = \frac{a}{a+b}\times \frac{w1}{w1+w2} + \frac{x}{n} \times \frac{w2}{w1+w2} \quad
w_1 = a+b, w_2=n
\end{equation}
\subsection{Multinomial-Dirichlet}
p.\ 31 to-do
\subsection{Normal-Normal}
\begin{equation}
v^*=\frac{1}{\frac{1}{v}+ \frac{n}{\sigma^2}}
\quad
m^*= v^* \left( \frac{m}{v} + \frac{n\bar{x}}{\sigma^2} \right)
\end{equation}
\begin{equation}
E[\theta\mid x] = m\times \frac{w1}{w1+w2} + \bar{x} \times \frac{w2}{w1+w2} \quad
w_1 = v^{-1}, w_2=(\sigma^2/n)^{-1}
\end{equation}
\subsection{Two parameter normal-Normal inverse gamma}
p.\ 32 to-do
\subsection{Poisson-Gamma}
\begin{equation}
\begin{split}
L(\theta) =& \prod_{i=1}^n \frac{\exp(-\theta) \theta^{x_i}}{x_i!}\\
=& \frac{\exp(-n\theta) \theta^{\sum_i^{n} x_i}}{\prod_{i=1}^n x_i!}\\
\end{split}
\end{equation}
we can rewrite the right hand side as
\begin{equation}
\hbox{Posterior} = [\frac{\exp(-n\theta) \theta^{\sum_i^{n} x_i}}{\prod_{i=1}^n x_i!} ]
[\frac{b^a \theta^{a-1}\exp(-b\theta)}{\Gamma(a)}]
\end{equation}
Disregarding the terms $x!,\Gamma(a), b^a$, which do not involve $\theta$, we have
\begin{equation}
\begin{split}
\hbox{Posterior} \propto & \exp(-n\theta) \theta^{\sum_i^{n} x_i} \theta^{a-1}\exp(-b\theta)\\
=& \theta^{a-1+\sum_i^{n} x_i} \exp(-\theta (b+n))
\end{split}
\end{equation}
If we equate $a^{*}-1=a-1+\sum_i^{n} x_i$ and $b^{*} = b+n$, we can rewrite the above as:
\begin{equation}
\theta^{a^{*}-1} \exp(-\theta b^{*})
\end{equation}
This means that $a^{*}=a+\sum_i^{n} x_i$ and $b^{*}=b+n$.
We can find a constant $k$ such that the above is a proper probability density function, i.e.:
\begin{equation}
\int_{-\infty}^{\infty} k \theta^{a^{*}-1} \exp(-\theta b^{*})=1
\end{equation}
Thus, the posterior has the form of a Gamma distribution with parameters
$a^{*}=a+\sum_i^{n} x_i, b^{*}=b+n$. Hence the gamma distribution is a conjugate prior for the Poisson.
\subsection{Exponential-Gamma}\label{conjugateformsexpgamma}
If prior is $Ga(a,b)$, given $Exp(\lambda$) as likelihood, posterior is $Ga(a+n,\sum x + b)$.
\begin{equation}
\begin{split}
Lik\times Prior =& [\lambda^n \exp \{-\lambda \sum x \}]
\left[ \frac{b^a \lambda^{a-1} \exp\{-b\lambda\}}{\Gamma(a)} \right]\\
=& \frac{b^a}{\Gamma(a)} \lambda^{a+n-1}\exp\{-\lambda(b+\sum x) \}
\end{split}
\end{equation}
The posterior is $Gamma(a+n,b+\sum x)$.
\subsubsection{The posterior predictive distribution of Y given x}
\begin{equation}
\begin{split}
f(Y\mid x,\lambda) =& \int f(Y,\lambda\mid x)\, d\lambda\\
=& \int f(Y\mid \lambda, x) f(\lambda\mid x)\, d\lambda\\
=&\int f(Y\mid \lambda) f(\lambda\mid x)\, d\lambda\\
=& \int \lambda \exp\{-\lambda y\} \lambda^{a+1-1}\exp\{ -\lambda(b+\sum x) \} \frac{b^a}{\Gamma(a)}\, d \lambda\\
=& \int \exp\{-\lambda y\} \lambda^{a+2-1}\exp\{ -\lambda(b+\sum x + y) \} \frac{b^a}{\Gamma(a)}\, d \lambda\\
\end{split}
\end{equation}
[Note that we can write $f(Y\mid \lambda, x)$ as $f(Y\mid \lambda)$ due to conditional independence of Y and x].
Now we use the trick of finding the integral to solve this:
\begin{equation}
\frac{\Gamma(a+2)}{(b+\sum x + y)^{a+2}} \frac{b^a}{\Gamma(a)}
\int \frac{(b+\sum x + y)^{a+2}}{\Gamma(a+2)}
\lambda^{a+2-1}\exp\{ -\lambda(b+\sum x + y) \}\, d \lambda
\end{equation}
The integral on the right is the $Gamma(a+2,b+\sum x + y)$ and integrates to 1. Hence,
\begin{equation}
f(Y\mid x,\lambda) = \frac{\Gamma(a+2)}{(b+\sum x + y)^{a+2}} \frac{b^a}{\Gamma(a)}
\end{equation}
If we have a Jeffrey's prior, Ga(0,0), then
$a, b \rightarrow 0$.
Posterior for $\lambda\mid x$ becomes: $Ga(n,\sum x)$.
Predictive distribution for $Y\mid x$ becomes:
\begin{equation}\label{expgammapred1}
\frac{\Gamma(2)}{(\sum x + y)^{2}}
\end{equation}
\subsubsection{Plug-in prediction} \label{plugin}
In the Jeffrey's prior case, estimate $\lambda$ from posterior mean: $\hat\lambda=\frac{n}{\sum x}$, and then plug it into $P(Y\mid x)$.
\begin{equation} \label{expgammapred2}
\begin{split}
f(Y\mid \lambda,x) =& \lambda \exp(-\lambda y) \lambda^{a+1-1} exp(-\lambda(b+\sum x))\\
=& \frac{1}{\bar{x}} \exp(\frac{1}{\bar{x}} (\sum x - y))
\end{split}
\end{equation}
Given that x=10, y=0,10,30,50, using equation for the posterior predictive distribution \ref{expgammapred1}, we have ($\frac{\Gamma(2)}{(\sum x + y)^{2}}$).
<<echo=F>>=
expgammapred1<-function(x,y){
gamma(2)/((x+y)^2)
}
ys<-c(0,10,30,50,500)
x<-10
store<-NULL
for(i in 1:length(ys)){
store[i]<-expgammapred1(x,ys[i])
}
@
And using equation \ref{expgammapred2} for the plug-in posterior predictive distribution, we have
$\frac{1}{\bar{x}} \exp(\frac{1}{\bar{x} (\sum x - y)}
})$.
<<echo=F>>=
expgammapred2<-function(x,y,n=length(x)){
est.lambda<-(n)/(sum(x))
est.lambda * exp(est.lambda * (sum(x)- y))
}
store2<-NULL
for(i in 1:length(ys)){
store2[i]<-expgammapred2(x=x,y=ys[i])
}
dat<-rbind(store,store2)
plot(ys,store,type="l")
lines(ys,store2)
dat<-as.data.frame(dat)
colnames(dat)<-ys
rownames(dat)<-c("postpred","plugin")
@
\begin{verbatim}
0 10 30 50 500
postpred 0.01000 0.0025 0.000625 0.00027778 3.8447e-06
plugin 0.27183 0.1000 0.013534 0.00183156 5.2429e-23
\end{verbatim}
The plug-in prediction approximates the true PPD for large values of y.
\section{Deriving full conditional distributions}
Full conditional distributions are needed, e.g., for Gibbs sampling.
Deriving a full conditional using a DAG as a guide (Lunn et al): Prior on the target parameter times distribution of each child of target parameter conditional on that child's parents.
This section is based on Gilks et al.
%%to-do: add citation.
How to get the full conditional distribution of $\theta_i\mid \mathbf{\theta}_{-i},\mathbf{x}$? P.\ 77 of lecture notes.
\begin{equation}
\begin{split}
f(\theta_i\mid \mathbf{\theta}_{-i},\mathbf{x}) =&
\frac{f(\theta_i,\mathbf{\theta}_{-i}\mid \mathbf{x})}{f(\mathbf{\theta}_{-i}\mathbf{x})}\\
=& \frac{f(\mathbf{\theta}\mid \mathbf{x})}{\int f(\mathbf{\theta}\mid \mathbf{x})\, d\theta_i}\\
\propto f(\mathbf{\theta}\mid \mathbf{x})
\end{split}
\end{equation}
So, to get $f(\theta_i\mid \mathbf{\theta}_{-i},\mathbf{x})$ we simply take
$f(\mathbf{\theta}\mid \mathbf{x})$ and treat all the parameters in $\theta_{-1}$ as constants.
\subsection{Example 1: A simple Bayesian model}
\begin{itemize}
\item
$y_i \sim N(\mu,\tau^{-1}), i=1,\dots,n$
\item
$\mu \sim N(0,1)$
\item
$\tau \sim Ga(2,1)$
\end{itemize}
$y_i$ are conditionally independent given $\mu,\tau$, and $\mu,\tau$ are themselves independent. Let $y=(y_1,\dots,y_n)$.
The joint likelihood:
\begin{equation}
p(y,\mu,\tau)=
\prod_{i=1}^{n} P(y_i\mid \mu, \tau) P(\mu)P(\tau)
\end{equation}
Expanding this out:
\begin{equation}
p(y,\mu,\tau)=
(2\pi)^{(n+1)/2} \tau^{n/2} \exp\left\{
-\frac{\tau}{2} \sum (y_i-\mu)^2
\right\}
\exp\left\{
-\frac{1}{2}\mu^2
\right\}
\tau
e^{-\tau}
\end{equation}
When $y$ is observed, the joint posterior of $\mu,\tau$ is:
\begin{equation}
p(\mu,\tau)= P(\mu,\tau\mid y) = \frac{P(y,\mu,\tau)}{\int P(y,\mu,\tau)\,d\mu\,d\tau}
\end{equation}
The full conditional for $\mu$ is
\begin{equation}
p(\mu\mid \tau) = \frac{P(\mu,\tau\mid y)}{P(\tau
\mid y)} = \frac{P(y,\mu,\tau)}{P(y,\tau)}
\end{equation}
This is proportional to $P(y,\mu,\tau)$.
Therefore,
\begin{equation}
\begin{split}
p(\mu\mid \tau) \propto& \exp\left\{
-\frac{\tau}{2}\sum(y_i-\mu)^2
\right\}
\exp\left\{
-\frac{1}{2}\mu^2
\right\}\\
\propto& \exp\left\{
-\frac{1}{2}(1+n\tau)(\mu-\frac{t\sum y}{1+n\tau})^2
\right\}
\end{split}
\end{equation}
Therefore $p(\mu\mid \tau)$ is $N(\frac{t\sum y}{1+n\tau},(1+n\tau)^{-1})$.
Similarly,
\begin{equation}
\begin{split}
p(\tau\mid \mu) \propto& \tau^{n/2}
\exp\left\{
-\frac{\tau}{2} \sum (y-\mu)^2
\right\}
\tau e^{-\tau}\\
=& \tau^{1+n/2}
\exp\left\{
-\tau[1+\frac{1}{2}\sum (y_i-\mu)^2]
\right\}
\end{split}
\end{equation}
This is the kernel for $Ga(2+n/2,1++\frac{1}{2}\sum (y_i-\mu)^2)$.
\subsection{Example 2: A more complex DAG (hierarchical) model}
The full conditional distribution for any parameter can be contructed from those few terms of the joint distribution which depend on it.
Consider the \textbf{Normal random effects model}:
\begin{itemize}
\item
$y_{ij} \sim N(\alpha_i,\tau^{-1})$, where $j=1,\dots,m_i$, and $i=1,\dots,n$.
\item
$\alpha_i\sim N(\mu,\omega^{-1})$.
\item
$\mu \sim N(0,1)$
\item
$\tau \sim Ga(2,1)$
\item
$\omega \sim Ga(1,1)$
\end{itemize}
Hyperparameters are $\mu$ and $\tau^{-1}$, and the priors on these are the hyperpriors.
Assumptions:
\begin{itemize}
\item $y_{ij}$ independent given all parameters.
\item $\alpha_i$ independent given $\mu,\tau,\omega$.
\item $\mu,\tau,\omega$ mutually independent.
\end{itemize}
The joint probability:
\begin{equation}
p(y,\alpha,\mu,\tau,\omega) =
\prod_{_i=1}^{n} \left\{
\prod_{_j=1}^{m_i} P(y_{ij}\mid \alpha_i,\tau)P(\alpha_i\mid \mu,\omega)
\right\} P(\mu)P(\tau)P(\omega)
\end{equation}
Then, the full conditional for $\alpha_i$ is
\begin{equation}
p(\alpha_i\mid y,\alpha_{-1},\mu,\tau,\omega) \propto
\prod_{_j=1}^{m_i}
P(y_{ij}\mid \alpha_i,\tau)P(\alpha_i\mid \mu,\omega)
\end{equation}
Note that $P(\mu)P(\tau)P(\omega)$ drop off as they are now constants. The above expands to:
\begin{equation}
p(\alpha_i\mid y,\alpha_{-1},\mu,\tau,\omega) \propto \exp\left\{
-\frac{1}{2} (\omega+m_i \tau)(\alpha_i - \frac{\omega\mu + \tau \sum_{i=1}^{m_i} y_{ij}}{\omega + m_i\tau})^2
\right\}
\end{equation}
In other words, $N(\frac{\omega\mu + \tau \sum_{i=1}^{m_i} y_{ij}}{\omega + m_i\tau},(\omega+m_i \tau)^{-1})$.
\subsection{Example 3: Meta-analysis of stroke data (old exam question)}
Given data from four trials on treatment and control groups. The number of strokes in each group is counted.
<<echo=F,eval=F>>=
dat<-data.frame(trial=rep(1:4,2),counts=c(5,6,8,11,10,6,12,11),total=c(100,155,170,190,100,130,150,180),group=rep(c("trtmt","cont"),each=4))
library(xtable)
xtable(dat)
@
% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Sat May 17 11:19:08 2014
\begin{table}[ht]
\centering
\begin{tabular}{rrrrl}
\hline
& trial & counts & total & group \\
\hline
1 & 1 & 5 & 100 & trtmt \\
2 & 2 & 6 & 155 & trtmt \\
3 & 3 & 8 & 170 & trtmt \\
4 & 4 & 11 & 190 & trtmt \\
5 & 1 & 10 & 100 & cont \\
6 & 2 & 6 & 130 & cont \\
7 & 3 & 12 & 150 & cont \\
8 & 4 & 11 & 180 & cont \\
\hline
\end{tabular}
\end{table}
Let $x_i$ be the number of strokes in the control group, $y_i$ the number of strokes in the treatment group, i is trial number. Total count in control is $nx_i$ and in treatment group $ny_i$.
The model is
\begin{itemize}
\item Likelihoods:
\begin{itemize}
\item
$x_i \mid \gamma_i, nx_i \sim Binom(\gamma_i,nx_i)$
\item
$y_i \mid \gamma_i, ny_i \sim Binom(\delta_i,ny_i)$
\end{itemize}
\item Priors:
\begin{itemize}
\item
$\mu_i \sim N(mean=0,variance=1/0.001)$
$logit(\gamma_i) \leftarrow \mu_i$
\item
$\theta_i \sim N(\alpha, \beta)$
$logit(\delta_i) \leftarrow \mu_i + \theta_i$
\item
$\alpha \sim N(0,1/0.002)$
\item
$\beta \leftarrow 1/\sigma^2$
\item
$\sigma^2 \sim Unif(0,100)$
\end{itemize}
\end{itemize}
Find the full conditional distribution of $f(\alpha\mid \beta,\mu,\theta,x,y)$.
The joint posterior distribution of the parameters is:
\begin{equation}
p(\alpha,\beta,\mu,\theta\mid x,y) = \prod_{i=1}^4 p(x_i\mid \gamma_i,nx_i) \prod_{i=1}^4 p(y_i\mid \delta_i,ny_i) p(\alpha)p(\beta)p(\mu)p(\theta\mid \alpha,\beta)
\end{equation}
To get the conditional distribution of $\alpha$, we just treat all terms not involving $\alpha$ as constant. This gives us (up to proportionality):
\begin{equation}
\begin{split}
(\alpha\mid \beta,\mu,\theta,x,y) \propto &
p(\alpha)p(\theta\mid \alpha,\beta)\\
\propto & N(0,0.002)\times N(\alpha,\beta)
\end{split}
\end{equation}
<<>>=
dat<-list(y=c(5,6,8,11),
x=c(10,6,12,11),
ny=c(100,155,170,190),
nx=c(100,130,150,180))
## prediction:
cat("
model
{
for(i in 1:4){
x[i] ~ dbin(gamma[i],nx[i])
y[i] ~ dbin(delta[i],ny[i])
logit(gamma[i]) <- mu[i]
logit(delta[i]) <- mu[i]+theta[i]
## priors:
mu[i] ~ dnorm(0,0.001)
theta[i] ~ dnorm(alpha,beta)
}
alpha ~ dnorm(0,0.001)
beta<-1/sigma
sigma ~ dunif(0,100)
mu.pred ~ dnorm(0,0.001)
theta.pred ~ dnorm(alpha,beta)
probc<-exp(mu.pred)/(1+exp(mu.pred))
probt<-exp(mu.pred+theta.pred)/(1+exp(mu.pred+theta.pred))
logit(gamma.pred) <- mu.pred
logit(delta.pred) <- mu.pred+theta.pred
## shaped like a Beta(1/2,1/2)
st ~ dbin(gamma.pred,100)
sc ~ dbin(delta.pred,100)
}
",
file="JAGSmodels/metaanalysisstrokes.jag" )
@
<<echo=F,eval=F>>=
## specify variables to track
## the posterior distribution of: