-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcha-bnp.html
More file actions
922 lines (881 loc) · 102 KB
/
cha-bnp.html
File metadata and controls
922 lines (881 loc) · 102 KB
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 11 Bayesian nonparametric models | NimbleUserManual.knit</title>
<meta name="description" content="This is the NIMBLE User Manual." />
<meta name="generator" content="bookdown 0.37 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 11 Bayesian nonparametric models | NimbleUserManual.knit" />
<meta property="og:type" content="book" />
<meta property="og:image" content="/nimble-icon.png" />
<meta property="og:description" content="This is the NIMBLE User Manual." />
<meta name="github-repo" content="nimble-dev/nimble" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 11 Bayesian nonparametric models | NimbleUserManual.knit" />
<meta name="twitter:description" content="This is the NIMBLE User Manual." />
<meta name="twitter:image" content="/nimble-icon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="cha-spatial.html"/>
<link rel="next" href="overview.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<img src="./nimble-icon.png"
width=100>
<li><a href="./cha-welcome-nimble.html">NIMBLE User Manual, Version 1.4.2</a></li>
<li><a href="https://github.com/nimble-dev/nimble">NIMBLE Development Team</a></li>
<li><a href="https://R-nimble.org">https://R-nimble.org</a></li>
<li class="divider"></li>
<li class="part"><span><b>I Introduction</b></span></li>
<li class="chapter" data-level="1" data-path="cha-welcome-nimble.html"><a href="cha-welcome-nimble.html"><i class="fa fa-check"></i><b>1</b> Welcome to NIMBLE</a>
<ul>
<li class="chapter" data-level="1.1" data-path="cha-welcome-nimble.html"><a href="cha-welcome-nimble.html#sec:what-is-nimble"><i class="fa fa-check"></i><b>1.1</b> What does NIMBLE do?</a></li>
<li class="chapter" data-level="1.2" data-path="cha-welcome-nimble.html"><a href="cha-welcome-nimble.html#how-to-use-this-manual"><i class="fa fa-check"></i><b>1.2</b> How to use this manual</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html"><i class="fa fa-check"></i><b>2</b> Lightning introduction</a>
<ul>
<li class="chapter" data-level="2.1" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:brief-example"><i class="fa fa-check"></i><b>2.1</b> A brief example</a></li>
<li class="chapter" data-level="2.2" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:creating-model"><i class="fa fa-check"></i><b>2.2</b> Creating a model</a></li>
<li class="chapter" data-level="2.3" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:compiling-model"><i class="fa fa-check"></i><b>2.3</b> Compiling the model</a></li>
<li class="chapter" data-level="2.4" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:intro-runMCMC"><i class="fa fa-check"></i><b>2.4</b> One-line invocation of MCMC</a></li>
<li class="chapter" data-level="2.5" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:creating-mcmc"><i class="fa fa-check"></i><b>2.5</b> Creating, compiling and running a basic MCMC configuration</a></li>
<li class="chapter" data-level="2.6" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:customizing-mcmc"><i class="fa fa-check"></i><b>2.6</b> Customizing the MCMC</a></li>
<li class="chapter" data-level="2.7" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:running-mcem"><i class="fa fa-check"></i><b>2.7</b> Running MCEM</a></li>
<li class="chapter" data-level="2.8" data-path="cha-lightning-intro.html"><a href="cha-lightning-intro.html#sec:creating-your-own"><i class="fa fa-check"></i><b>2.8</b> Creating your own functions</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html"><i class="fa fa-check"></i><b>3</b> More introduction</a>
<ul>
<li class="chapter" data-level="3.1" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html#nimble-adopts-and-extends-the-bugs-language-for-specifying-models"><i class="fa fa-check"></i><b>3.1</b> NIMBLE adopts and extends the BUGS language for specifying models</a></li>
<li class="chapter" data-level="3.2" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html#sec:nimble-lang-writ"><i class="fa fa-check"></i><b>3.2</b> nimbleFunctions for writing algorithms</a></li>
<li class="chapter" data-level="3.3" data-path="cha-more-introduction.html"><a href="cha-more-introduction.html#sec:nimble-algor-libr"><i class="fa fa-check"></i><b>3.3</b> The NIMBLE algorithm library</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html"><i class="fa fa-check"></i><b>4</b> Installing NIMBLE</a>
<ul>
<li class="chapter" data-level="4.1" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#sec:requ-run-nimble"><i class="fa fa-check"></i><b>4.1</b> Requirements to run NIMBLE</a></li>
<li class="chapter" data-level="4.2" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#sec:compiler"><i class="fa fa-check"></i><b>4.2</b> Installing a C++ compiler for NIMBLE to use</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#macos"><i class="fa fa-check"></i><b>4.2.1</b> MacOS</a></li>
<li class="chapter" data-level="4.2.2" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#linux"><i class="fa fa-check"></i><b>4.2.2</b> Linux</a></li>
<li class="chapter" data-level="4.2.3" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#windows"><i class="fa fa-check"></i><b>4.2.3</b> Windows</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#installing-the-nimble-package"><i class="fa fa-check"></i><b>4.3</b> Installing the NIMBLE package</a></li>
<li class="chapter" data-level="4.4" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#troubleshooting-installation-problems"><i class="fa fa-check"></i><b>4.4</b> Troubleshooting installation problems</a></li>
<li class="chapter" data-level="4.5" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#customizing-your-installation"><i class="fa fa-check"></i><b>4.5</b> Customizing your installation</a>
<ul>
<li class="chapter" data-level="4.5.1" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#using-your-own-copy-of-eigen"><i class="fa fa-check"></i><b>4.5.1</b> Using your own copy of Eigen</a></li>
<li class="chapter" data-level="4.5.2" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#using-libnimble"><i class="fa fa-check"></i><b>4.5.2</b> Using libnimble</a></li>
<li class="chapter" data-level="4.5.3" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#sec:blas"><i class="fa fa-check"></i><b>4.5.3</b> BLAS and LAPACK</a></li>
<li class="chapter" data-level="4.5.4" data-path="cha-installing-nimble.html"><a href="cha-installing-nimble.html#customizing-compilation-of-the-nimble-generated-c"><i class="fa fa-check"></i><b>4.5.4</b> Customizing compilation of the NIMBLE-generated C++</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II Models in NIMBLE</b></span></li>
<li class="chapter" data-level="5" data-path="cha-writing-models.html"><a href="cha-writing-models.html"><i class="fa fa-check"></i><b>5</b> Writing models in NIMBLE’s dialect of BUGS</a>
<ul>
<li class="chapter" data-level="5.1" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:supp-feat-bugs"><i class="fa fa-check"></i><b>5.1</b> Comparison to BUGS dialects supported by WinBUGS, OpenBUGS and JAGS</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="cha-writing-models.html"><a href="cha-writing-models.html#supported-features-of-bugs-and-jags"><i class="fa fa-check"></i><b>5.1.1</b> Supported features of BUGS and JAGS</a></li>
<li class="chapter" data-level="5.1.2" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:extensions-bugs"><i class="fa fa-check"></i><b>5.1.2</b> NIMBLE’s Extensions to BUGS and JAGS</a></li>
<li class="chapter" data-level="5.1.3" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:not-yet-supported"><i class="fa fa-check"></i><b>5.1.3</b> Not-supported features of BUGS and JAGS</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="cha-writing-models.html"><a href="cha-writing-models.html#writing-models"><i class="fa fa-check"></i><b>5.2</b> Writing models</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="cha-writing-models.html"><a href="cha-writing-models.html#declaring-stochastic-and-deterministic-nodes"><i class="fa fa-check"></i><b>5.2.1</b> Declaring stochastic and deterministic nodes</a></li>
<li class="chapter" data-level="5.2.2" data-path="cha-writing-models.html"><a href="cha-writing-models.html#sec:more-kinds-bugs"><i class="fa fa-check"></i><b>5.2.2</b> More kinds of BUGS declarations</a></li>
<li class="chapter" data-level="5.2.3" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:vectorized-versus-scalar-declarations"><i class="fa fa-check"></i><b>5.2.3</b> Vectorized versus scalar declarations</a></li>
<li class="chapter" data-level="5.2.4" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:dists-and-functions"><i class="fa fa-check"></i><b>5.2.4</b> Available distributions</a></li>
<li class="chapter" data-level="5.2.5" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:BUGS-lang-fxns"><i class="fa fa-check"></i><b>5.2.5</b> Available BUGS language functions</a></li>
<li class="chapter" data-level="5.2.6" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:BUGS-link"><i class="fa fa-check"></i><b>5.2.6</b> Available link functions</a></li>
<li class="chapter" data-level="5.2.7" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:trunc"><i class="fa fa-check"></i><b>5.2.7</b> Truncation, censoring, and constraints</a></li>
<li class="chapter" data-level="5.2.8" data-path="cha-writing-models.html"><a href="cha-writing-models.html#subsec:macros"><i class="fa fa-check"></i><b>5.2.8</b> Model macros</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="cha-building-models.html"><a href="cha-building-models.html"><i class="fa fa-check"></i><b>6</b> Building and using models</a>
<ul>
<li class="chapter" data-level="6.1" data-path="cha-building-models.html"><a href="cha-building-models.html#creating-model-objects"><i class="fa fa-check"></i><b>6.1</b> Creating model objects</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="cha-building-models.html"><a href="cha-building-models.html#using-nimblemodel-to-create-a-model"><i class="fa fa-check"></i><b>6.1.1</b> Using <em>nimbleModel</em> to create a model</a></li>
<li class="chapter" data-level="6.1.2" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:readBUGSmodel"><i class="fa fa-check"></i><b>6.1.2</b> Creating a model from standard BUGS and JAGS input files</a></li>
<li class="chapter" data-level="6.1.3" data-path="cha-building-models.html"><a href="cha-building-models.html#sub:multiple-instances"><i class="fa fa-check"></i><b>6.1.3</b> Making multiple instances from the same model definition</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:nodes-and-variables"><i class="fa fa-check"></i><b>6.2</b> NIMBLE models are objects you can query and manipulate</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:what-are-nodes-and-variables"><i class="fa fa-check"></i><b>6.2.1</b> What are variables and nodes?</a></li>
<li class="chapter" data-level="6.2.2" data-path="cha-building-models.html"><a href="cha-building-models.html#determining-the-nodes-and-variables-in-a-model"><i class="fa fa-check"></i><b>6.2.2</b> Determining the nodes and variables in a model</a></li>
<li class="chapter" data-level="6.2.3" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:accessing-nodes"><i class="fa fa-check"></i><b>6.2.3</b> Accessing nodes</a></li>
<li class="chapter" data-level="6.2.4" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:how-nodes-are"><i class="fa fa-check"></i><b>6.2.4</b> How nodes are named</a></li>
<li class="chapter" data-level="6.2.5" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:why-use-node"><i class="fa fa-check"></i><b>6.2.5</b> Why use node names?</a></li>
<li class="chapter" data-level="6.2.6" data-path="cha-building-models.html"><a href="cha-building-models.html#sec:cdisdata"><i class="fa fa-check"></i><b>6.2.6</b> Checking if a node holds data</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="cha-building-models.html"><a href="cha-building-models.html#using-models-in-parallel"><i class="fa fa-check"></i><b>6.3</b> Using models in parallel</a></li>
</ul></li>
<li class="part"><span><b>III Algorithms in NIMBLE</b></span></li>
<li class="chapter" data-level="7" data-path="cha-mcmc.html"><a href="cha-mcmc.html"><i class="fa fa-check"></i><b>7</b> MCMC</a>
<ul>
<li class="chapter" data-level="7.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:nimbleMCMC"><i class="fa fa-check"></i><b>7.1</b> One-line invocation of MCMC: <em>nimbleMCMC</em></a></li>
<li class="chapter" data-level="7.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:mcmc-configuration"><i class="fa fa-check"></i><b>7.2</b> The MCMC configuration</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:default-mcmc-conf"><i class="fa fa-check"></i><b>7.2.1</b> Default MCMC configuration</a></li>
<li class="chapter" data-level="7.2.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:customizing-mcmc-conf"><i class="fa fa-check"></i><b>7.2.2</b> Customizing the MCMC configuration</a></li>
<li class="chapter" data-level="7.2.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:derived-quantities"><i class="fa fa-check"></i><b>7.2.3</b> Setting up derived quantities for additional quantities of interest</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:build-compile-mcmc"><i class="fa fa-check"></i><b>7.3</b> Building and compiling the MCMC</a></li>
<li class="chapter" data-level="7.4" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:initMCMC"><i class="fa fa-check"></i><b>7.4</b> Initializing MCMC</a></li>
<li class="chapter" data-level="7.5" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:runMCMC"><i class="fa fa-check"></i><b>7.5</b> User-friendly execution of MCMC algorithms: <em>runMCMC</em></a></li>
<li class="chapter" data-level="7.6" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:executing-the-mcmc-algorithm"><i class="fa fa-check"></i><b>7.6</b> Running the MCMC</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:mcmc-rerun"><i class="fa fa-check"></i><b>7.6.1</b> Rerunning versus restarting an MCMC</a></li>
<li class="chapter" data-level="7.6.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:sampler-time"><i class="fa fa-check"></i><b>7.6.2</b> Measuring sampler computation times: <em>getTimes</em></a></li>
<li class="chapter" data-level="7.6.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#assessing-the-adaption-process-of-rw-and-rw_block-samplers"><i class="fa fa-check"></i><b>7.6.3</b> Assessing the adaption process of <em>RW</em> and <em>RW_block</em> samplers</a></li>
</ul></li>
<li class="chapter" data-level="7.7" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:extracting-samples"><i class="fa fa-check"></i><b>7.7</b> Extracting MCMC samples</a></li>
<li class="chapter" data-level="7.8" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:WAIC"><i class="fa fa-check"></i><b>7.8</b> Calculating WAIC</a></li>
<li class="chapter" data-level="7.9" data-path="cha-mcmc.html"><a href="cha-mcmc.html#k-fold-cross-validation"><i class="fa fa-check"></i><b>7.9</b> k-fold cross-validation</a></li>
<li class="chapter" data-level="7.10" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:rjmcmc"><i class="fa fa-check"></i><b>7.10</b> Variable selection using Reversible Jump MCMC</a>
<ul>
<li class="chapter" data-level="7.10.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:rjmcmc-indicator"><i class="fa fa-check"></i><b>7.10.1</b> Using indicator variables</a></li>
<li class="chapter" data-level="7.10.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:rjmcmc-no-indicator"><i class="fa fa-check"></i><b>7.10.2</b> Without indicator variables</a></li>
</ul></li>
<li class="chapter" data-level="7.11" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:samplers-provided"><i class="fa fa-check"></i><b>7.11</b> Samplers provided with NIMBLE</a>
<ul>
<li class="chapter" data-level="7.11.1" data-path="cha-mcmc.html"><a href="cha-mcmc.html#conjugate-gibbs-samplers"><i class="fa fa-check"></i><b>7.11.1</b> Conjugate (‘Gibbs’) samplers</a></li>
<li class="chapter" data-level="7.11.2" data-path="cha-mcmc.html"><a href="cha-mcmc.html#subsec:HMC"><i class="fa fa-check"></i><b>7.11.2</b> Hamiltonian Monte Carlo (HMC)</a></li>
<li class="chapter" data-level="7.11.3" data-path="cha-mcmc.html"><a href="cha-mcmc.html#particle-filter-samplers"><i class="fa fa-check"></i><b>7.11.3</b> Particle filter samplers</a></li>
<li class="chapter" data-level="7.11.4" data-path="cha-mcmc.html"><a href="cha-mcmc.html#customized-log-likelihood-evaluations-rw_llfunction-sampler"><i class="fa fa-check"></i><b>7.11.4</b> Customized log-likelihood evaluations: <em>RW_llFunction sampler</em></a></li>
</ul></li>
<li class="chapter" data-level="7.12" data-path="cha-mcmc.html"><a href="cha-mcmc.html#sec:mcmc-example-litters"><i class="fa fa-check"></i><b>7.12</b> Detailed MCMC example: <em>litters</em></a></li>
<li class="chapter" data-level="7.13" data-path="cha-mcmc.html"><a href="cha-mcmc.html#mcmc-suite-compare-mcmcs"><i class="fa fa-check"></i><b>7.13</b> Comparing different MCMCs with <em>compareMCMCs</em></a></li>
<li class="chapter" data-level="7.14" data-path="cha-mcmc.html"><a href="cha-mcmc.html#running-mcmc-chains-in-parallel"><i class="fa fa-check"></i><b>7.14</b> Running MCMC chains in parallel</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html"><i class="fa fa-check"></i><b>8</b> Particle Filters, PMCMC, and MCEM</a>
<ul>
<li class="chapter" data-level="8.1" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#particle-filters-sequential-monte-carlo-and-iterated-filtering"><i class="fa fa-check"></i><b>8.1</b> Particle filters / sequential Monte Carlo and iterated filtering</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#filtering-algorithms"><i class="fa fa-check"></i><b>8.1.1</b> Filtering algorithms</a></li>
<li class="chapter" data-level="8.1.2" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#sec:particle-mcmc"><i class="fa fa-check"></i><b>8.1.2</b> Particle MCMC (PMCMC)</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#monte-carlo-expectation-maximization-mcem"><i class="fa fa-check"></i><b>8.2</b> Monte Carlo Expectation Maximization (MCEM)</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#sec:estimate-mcem-cov"><i class="fa fa-check"></i><b>8.2.1</b> Estimating the asymptotic covariance From MCEM</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="9" data-path="cha-laplace.html"><a href="cha-laplace.html"><i class="fa fa-check"></i><b>9</b> Laplace, AGHQ, and nested approximations</a>
<ul>
<li class="chapter" data-level="9.1" data-path="cha-laplace.html"><a href="cha-laplace.html#sec:AD-laplace"><i class="fa fa-check"></i><b>9.1</b> Laplace approximation and adaptive Gauss-Hermite quadrature (AGHQ)</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="cha-laplace.html"><a href="cha-laplace.html#glmm-example"><i class="fa fa-check"></i><b>9.1.1</b> GLMM example</a></li>
<li class="chapter" data-level="9.1.2" data-path="cha-laplace.html"><a href="cha-laplace.html#using-laplace-approximation"><i class="fa fa-check"></i><b>9.1.2</b> Using Laplace approximation</a></li>
<li class="chapter" data-level="9.1.3" data-path="cha-laplace.html"><a href="cha-laplace.html#using-the-laplace-approximation-methods-directly"><i class="fa fa-check"></i><b>9.1.3</b> Using the Laplace approximation methods directly</a></li>
<li class="chapter" data-level="9.1.4" data-path="cha-laplace.html"><a href="cha-laplace.html#changing-the-optimization-methods"><i class="fa fa-check"></i><b>9.1.4</b> Changing the optimization methods</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="cha-laplace.html"><a href="cha-laplace.html#sec:nested-approx"><i class="fa fa-check"></i><b>9.2</b> Nested approximation (INLA-like) methods</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="cha-laplace.html"><a href="cha-laplace.html#overview-of-the-methodology"><i class="fa fa-check"></i><b>9.2.1</b> Overview of the methodology</a></li>
<li class="chapter" data-level="9.2.2" data-path="cha-laplace.html"><a href="cha-laplace.html#using-nimbles-nested-approximation"><i class="fa fa-check"></i><b>9.2.2</b> Using NIMBLE’s nested approximation</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="cha-spatial.html"><a href="cha-spatial.html"><i class="fa fa-check"></i><b>10</b> Spatial models</a>
<ul>
<li class="chapter" data-level="10.1" data-path="cha-spatial.html"><a href="cha-spatial.html#intrinsic-gaussian-car-model-dcar_normal"><i class="fa fa-check"></i><b>10.1</b> Intrinsic Gaussian CAR model: <em>dcar_normal</em></a>
<ul>
<li class="chapter" data-level="10.1.1" data-path="cha-spatial.html"><a href="cha-spatial.html#specification-and-density"><i class="fa fa-check"></i><b>10.1.1</b> Specification and density</a></li>
<li class="chapter" data-level="10.1.2" data-path="cha-spatial.html"><a href="cha-spatial.html#example"><i class="fa fa-check"></i><b>10.1.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="10.2" data-path="cha-spatial.html"><a href="cha-spatial.html#proper-gaussian-car-model-dcar_proper"><i class="fa fa-check"></i><b>10.2</b> Proper Gaussian CAR model: <em>dcar_proper</em></a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="cha-spatial.html"><a href="cha-spatial.html#specification-and-density-1"><i class="fa fa-check"></i><b>10.2.1</b> Specification and density</a></li>
<li class="chapter" data-level="10.2.2" data-path="cha-spatial.html"><a href="cha-spatial.html#example-1"><i class="fa fa-check"></i><b>10.2.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="cha-spatial.html"><a href="cha-spatial.html#sec:spatial-mcmc-sampling-car"><i class="fa fa-check"></i><b>10.3</b> MCMC Sampling of CAR models</a>
<ul>
<li class="chapter" data-level="10.3.1" data-path="cha-spatial.html"><a href="cha-spatial.html#initial-values"><i class="fa fa-check"></i><b>10.3.1</b> Initial values</a></li>
<li class="chapter" data-level="10.3.2" data-path="cha-spatial.html"><a href="cha-spatial.html#zero-neighbor-regions"><i class="fa fa-check"></i><b>10.3.2</b> Zero-neighbor regions</a></li>
<li class="chapter" data-level="10.3.3" data-path="cha-spatial.html"><a href="cha-spatial.html#zero-mean-constraint"><i class="fa fa-check"></i><b>10.3.3</b> Zero-mean constraint</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="cha-bnp.html"><a href="cha-bnp.html"><i class="fa fa-check"></i><b>11</b> Bayesian nonparametric models</a>
<ul>
<li class="chapter" data-level="11.1" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:bnpmixtures"><i class="fa fa-check"></i><b>11.1</b> Bayesian nonparametric mixture models</a></li>
<li class="chapter" data-level="11.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:crp"><i class="fa fa-check"></i><b>11.2</b> Chinese Restaurant Process model</a>
<ul>
<li class="chapter" data-level="11.2.1" data-path="cha-bnp.html"><a href="cha-bnp.html#specification-and-density-2"><i class="fa fa-check"></i><b>11.2.1</b> Specification and density</a></li>
<li class="chapter" data-level="11.2.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:excrp"><i class="fa fa-check"></i><b>11.2.2</b> Example</a></li>
<li class="chapter" data-level="11.2.3" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:extensionscrp"><i class="fa fa-check"></i><b>11.2.3</b> Extensions</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:sb"><i class="fa fa-check"></i><b>11.3</b> Stick-breaking model</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="cha-bnp.html"><a href="cha-bnp.html#specification-and-function"><i class="fa fa-check"></i><b>11.3.1</b> Specification and function</a></li>
<li class="chapter" data-level="11.3.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:exsb"><i class="fa fa-check"></i><b>11.3.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="cha-bnp.html"><a href="cha-bnp.html#mcmc-sampling-of-bnp-models"><i class="fa fa-check"></i><b>11.4</b> MCMC sampling of BNP models</a>
<ul>
<li class="chapter" data-level="11.4.1" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:mcmcdcrp"><i class="fa fa-check"></i><b>11.4.1</b> Sampling CRP models</a></li>
<li class="chapter" data-level="11.4.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:mcmcsb"><i class="fa fa-check"></i><b>11.4.2</b> Sampling stick-breaking models</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>IV Programming with NIMBLE</b></span></li>
<li class="chapter" data-level="" data-path="overview.html"><a href="overview.html"><i class="fa fa-check"></i>Overview</a></li>
<li class="chapter" data-level="12" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html"><i class="fa fa-check"></i><b>12</b> Writing simple nimbleFunctions</a>
<ul>
<li class="chapter" data-level="12.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:RC-intro"><i class="fa fa-check"></i><b>12.1</b> Introduction to simple nimbleFunctions</a></li>
<li class="chapter" data-level="12.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:r-fiunctions-implemented"><i class="fa fa-check"></i><b>12.2</b> R functions (or variants) implemented in NIMBLE</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#finding-help-for-nimbles-versions-of-r-functions"><i class="fa fa-check"></i><b>12.2.1</b> Finding help for NIMBLE’s versions of R functions</a></li>
<li class="chapter" data-level="12.2.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#basic-operations"><i class="fa fa-check"></i><b>12.2.2</b> Basic operations</a></li>
<li class="chapter" data-level="12.2.3" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:basic-math-linear"><i class="fa fa-check"></i><b>12.2.3</b> Math and linear algebra</a></li>
<li class="chapter" data-level="12.2.4" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:nimble-dist-funs"><i class="fa fa-check"></i><b>12.2.4</b> Distribution functions</a></li>
<li class="chapter" data-level="12.2.5" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:basic-flow-control"><i class="fa fa-check"></i><b>12.2.5</b> Flow control: <em>if-then-else</em>, <em>for</em>, <em>while</em>, and <em>stop</em></a></li>
<li class="chapter" data-level="12.2.6" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:print"><i class="fa fa-check"></i><b>12.2.6</b> <em>print</em> and <em>cat</em></a></li>
<li class="chapter" data-level="12.2.7" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:check-user-interr"><i class="fa fa-check"></i><b>12.2.7</b> Checking for user interrupts: <em>checkInterrupt</em></a></li>
<li class="chapter" data-level="12.2.8" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#optimization-optim-and-nimoptim"><i class="fa fa-check"></i><b>12.2.8</b> Optimization: <em>optim</em> and <em>nimOptim</em></a></li>
<li class="chapter" data-level="12.2.9" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#integration-integrate-and-nimintegrate"><i class="fa fa-check"></i><b>12.2.9</b> Integration: <em>integrate</em> and <em>nimIntegrate</em></a></li>
<li class="chapter" data-level="12.2.10" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:altern-keyw-some"><i class="fa fa-check"></i><b>12.2.10</b> ‘nim’ synonyms for some functions</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:how-nimble-handles"><i class="fa fa-check"></i><b>12.3</b> How NIMBLE handles types of variables</a>
<ul>
<li class="chapter" data-level="12.3.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:nimbleList-RCFuns"><i class="fa fa-check"></i><b>12.3.1</b> nimbleList data structures</a></li>
<li class="chapter" data-level="12.3.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:how-types-work"><i class="fa fa-check"></i><b>12.3.2</b> How numeric types work</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:decl-argum-return"><i class="fa fa-check"></i><b>12.4</b> Declaring argument and return types</a></li>
<li class="chapter" data-level="12.5" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:comp-nimbl-pass"><i class="fa fa-check"></i><b>12.5</b> Compiled nimbleFunctions pass arguments by reference</a></li>
<li class="chapter" data-level="12.6" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:calling-external-code"><i class="fa fa-check"></i><b>12.6</b> Calling external compiled code</a></li>
<li class="chapter" data-level="12.7" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:calling-R-code"><i class="fa fa-check"></i><b>12.7</b> Calling uncompiled R functions from compiled nimbleFunctions</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="cha-user-defined.html"><a href="cha-user-defined.html"><i class="fa fa-check"></i><b>13</b> Creating user-defined distributions and functions for models</a>
<ul>
<li class="chapter" data-level="13.1" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-functions"><i class="fa fa-check"></i><b>13.1</b> User-defined functions</a></li>
<li class="chapter" data-level="13.2" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-distributions"><i class="fa fa-check"></i><b>13.2</b> User-defined distributions</a>
<ul>
<li class="chapter" data-level="13.2.1" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:registerDistributions"><i class="fa fa-check"></i><b>13.2.1</b> Using <em>registerDistributions</em> for alternative parameterizations and providing other information</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:adv-user-def"><i class="fa fa-check"></i><b>13.3</b> Advanced user-defined functions and distributions</a></li>
<li class="chapter" data-level="13.4" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-macros"><i class="fa fa-check"></i><b>13.4</b> User-defined model macros</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="cha-using-models.html"><a href="cha-using-models.html"><i class="fa fa-check"></i><b>14</b> Working with NIMBLE models</a>
<ul>
<li class="chapter" data-level="14.1" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:accessing-variables"><i class="fa fa-check"></i><b>14.1</b> The variables and nodes in a NIMBLE model</a>
<ul>
<li class="chapter" data-level="14.1.1" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:get-nodes"><i class="fa fa-check"></i><b>14.1.1</b> Determining the nodes in a model</a></li>
<li class="chapter" data-level="14.1.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:introduced-nodes"><i class="fa fa-check"></i><b>14.1.2</b> Understanding lifted nodes</a></li>
<li class="chapter" data-level="14.1.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdgetdependencies"><i class="fa fa-check"></i><b>14.1.3</b> Determining dependencies in a model</a></li>
</ul></li>
<li class="chapter" data-level="14.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:nodeInfo"><i class="fa fa-check"></i><b>14.2</b> Accessing information about nodes and variables</a>
<ul>
<li class="chapter" data-level="14.2.1" data-path="cha-using-models.html"><a href="cha-using-models.html#getting-distributional-information-about-a-node"><i class="fa fa-check"></i><b>14.2.1</b> Getting distributional information about a node</a></li>
<li class="chapter" data-level="14.2.2" data-path="cha-using-models.html"><a href="cha-using-models.html#getting-information-about-a-distribution"><i class="fa fa-check"></i><b>14.2.2</b> Getting information about a distribution</a></li>
<li class="chapter" data-level="14.2.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:getParam"><i class="fa fa-check"></i><b>14.2.3</b> Getting distribution parameter values for a node</a></li>
<li class="chapter" data-level="14.2.4" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:getBound"><i class="fa fa-check"></i><b>14.2.4</b> Getting distribution bounds for a node</a></li>
</ul></li>
<li class="chapter" data-level="14.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdcalc-cdsim-cdgetl"><i class="fa fa-check"></i><b>14.3</b> Carrying out model calculations</a>
<ul>
<li class="chapter" data-level="14.3.1" data-path="cha-using-models.html"><a href="cha-using-models.html#core-model-operations-calculation-and-simulation"><i class="fa fa-check"></i><b>14.3.1</b> Core model operations: calculation and simulation</a></li>
<li class="chapter" data-level="14.3.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdsimn-cdcalcn-cdget"><i class="fa fa-check"></i><b>14.3.2</b> Pre-defined nimbleFunctions for operating on model nodes: <em>simNodes</em>, <em>calcNodes</em>, and <em>getLogProbNodes</em></a></li>
<li class="chapter" data-level="14.3.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:access-log-prob"><i class="fa fa-check"></i><b>14.3.3</b> Accessing log probabilities via <em>logProb</em> variables</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="15" data-path="cha-data-structures.html"><a href="cha-data-structures.html"><i class="fa fa-check"></i><b>15</b> Data structures in NIMBLE</a>
<ul>
<li class="chapter" data-level="15.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:modelValues-struct"><i class="fa fa-check"></i><b>15.1</b> The modelValues data structure</a>
<ul>
<li class="chapter" data-level="15.1.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#creating-modelvalues-objects"><i class="fa fa-check"></i><b>15.1.1</b> Creating modelValues objects</a></li>
<li class="chapter" data-level="15.1.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:access-cont-modelv"><i class="fa fa-check"></i><b>15.1.2</b> Accessing contents of modelValues</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:nimbleLists"><i class="fa fa-check"></i><b>15.2</b> The nimbleList data structure</a>
<ul>
<li class="chapter" data-level="15.2.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:predef-nimbleLists"><i class="fa fa-check"></i><b>15.2.1</b> Pre-defined nimbleList types</a></li>
<li class="chapter" data-level="15.2.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:eigen-nimFunctions"><i class="fa fa-check"></i><b>15.2.2</b> Using <em>eigen</em> and <em>svd</em> in nimbleFunctions</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="16" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html"><i class="fa fa-check"></i><b>16</b> Writing nimbleFunctions to interact with models</a>
<ul>
<li class="chapter" data-level="16.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:writ-nimble-funct"><i class="fa fa-check"></i><b>16.1</b> Overview</a></li>
<li class="chapter" data-level="16.2" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:using-comp-nimbl"><i class="fa fa-check"></i><b>16.2</b> Using and compiling nimbleFunctions</a></li>
<li class="chapter" data-level="16.3" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#writing-setup-code"><i class="fa fa-check"></i><b>16.3</b> Writing setup code</a>
<ul>
<li class="chapter" data-level="16.3.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#useful-tools-for-setup-functions"><i class="fa fa-check"></i><b>16.3.1</b> Useful tools for setup functions</a></li>
<li class="chapter" data-level="16.3.2" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:access-modify-numer"><i class="fa fa-check"></i><b>16.3.2</b> Accessing and modifying numeric values from setup</a></li>
<li class="chapter" data-level="16.3.3" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#determining-numeric-types-in-nimblefunctions"><i class="fa fa-check"></i><b>16.3.3</b> Determining numeric types in nimbleFunctions</a></li>
<li class="chapter" data-level="16.3.4" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:determ-pers-texttts"><i class="fa fa-check"></i><b>16.3.4</b> Control of setup outputs</a></li>
</ul></li>
<li class="chapter" data-level="16.4" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:nimble-lang-comp"><i class="fa fa-check"></i><b>16.4</b> Writing run code</a>
<ul>
<li class="chapter" data-level="16.4.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:driv-models:-calc"><i class="fa fa-check"></i><b>16.4.1</b> Driving models: <em>calculate</em>, <em>calculateDiff</em>, <em>simulate</em>, <em>getLogProb</em></a></li>
<li class="chapter" data-level="16.4.2" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#getting-and-setting-variable-and-node-values"><i class="fa fa-check"></i><b>16.4.2</b> Getting and setting variable and node values</a></li>
<li class="chapter" data-level="16.4.3" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#getting-parameter-values-and-node-bounds"><i class="fa fa-check"></i><b>16.4.3</b> Getting parameter values and node bounds</a></li>
<li class="chapter" data-level="16.4.4" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:access-model-modelv"><i class="fa fa-check"></i><b>16.4.4</b> Using modelValues objects</a></li>
<li class="chapter" data-level="16.4.5" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:using-model-variable"><i class="fa fa-check"></i><b>16.4.5</b> Using model variables and modelValues in expressions</a></li>
<li class="chapter" data-level="16.4.6" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:incl-other-meth"><i class="fa fa-check"></i><b>16.4.6</b> Including other methods in a nimbleFunction</a></li>
<li class="chapter" data-level="16.4.7" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:using-other-nimbl"><i class="fa fa-check"></i><b>16.4.7</b> Using other nimbleFunctions</a></li>
<li class="chapter" data-level="16.4.8" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:virt-nimbl-nimbl"><i class="fa fa-check"></i><b>16.4.8</b> Virtual nimbleFunctions and nimbleFunctionLists</a></li>
<li class="chapter" data-level="16.4.9" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#character-objects"><i class="fa fa-check"></i><b>16.4.9</b> Character objects</a></li>
<li class="chapter" data-level="16.4.10" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:user-defined-data"><i class="fa fa-check"></i><b>16.4.10</b> User-defined data structures</a></li>
</ul></li>
<li class="chapter" data-level="16.5" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:user-samplers"><i class="fa fa-check"></i><b>16.5</b> Example: writing user-defined samplers to extend NIMBLE’s MCMC engine</a>
<ul>
<li class="chapter" data-level="16.5.1" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#user-defined-samplers-and-posterior-predictive-nodes"><i class="fa fa-check"></i><b>16.5.1</b> User-defined samplers and posterior predictive nodes</a></li>
</ul></li>
<li class="chapter" data-level="16.6" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#copying-nimblefunctions-and-nimble-models"><i class="fa fa-check"></i><b>16.6</b> Copying nimbleFunctions (and NIMBLE models)</a></li>
<li class="chapter" data-level="16.7" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:debugging"><i class="fa fa-check"></i><b>16.7</b> Debugging nimbleFunctions</a></li>
<li class="chapter" data-level="16.8" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#timing-nimblefunctions-with-run.time"><i class="fa fa-check"></i><b>16.8</b> Timing nimbleFunctions with <em>run.time</em></a></li>
<li class="chapter" data-level="16.9" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#clearing-and-unloading-compiled-objects"><i class="fa fa-check"></i><b>16.9</b> Clearing and unloading compiled objects</a></li>
<li class="chapter" data-level="16.10" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#reducing-memory-usage"><i class="fa fa-check"></i><b>16.10</b> Reducing memory usage</a></li>
</ul></li>
<li class="part"><span><b>V Automatic Derivatives in NIMBLE</b></span></li>
<li class="chapter" data-level="17" data-path="cha-AD.html"><a href="cha-AD.html"><i class="fa fa-check"></i><b>17</b> Automatic Derivatives</a>
<ul>
<li class="chapter" data-level="17.1" data-path="cha-AD.html"><a href="cha-AD.html#sec:use-derivs"><i class="fa fa-check"></i><b>17.1</b> How to turn on derivatives in a model</a></li>
<li class="chapter" data-level="17.2" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-user-def"><i class="fa fa-check"></i><b>17.2</b> How to support derivatives in user-defined functions and distributions</a></li>
<li class="chapter" data-level="17.3" data-path="cha-AD.html"><a href="cha-AD.html#what-operations-are-and-arent-supported-for-ad"><i class="fa fa-check"></i><b>17.3</b> What operations are and aren’t supported for AD</a></li>
<li class="chapter" data-level="17.4" data-path="cha-AD.html"><a href="cha-AD.html#basics-of-obtaining-derivatives-in-nimblefunctions"><i class="fa fa-check"></i><b>17.4</b> Basics of obtaining derivatives in <code>nimbleFunctions</code></a>
<ul>
<li class="chapter" data-level="17.4.1" data-path="cha-AD.html"><a href="cha-AD.html#checking-derivatives-with-uncompiled-execution"><i class="fa fa-check"></i><b>17.4.1</b> Checking derivatives with uncompiled execution</a></li>
<li class="chapter" data-level="17.4.2" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-holding-out"><i class="fa fa-check"></i><b>17.4.2</b> Holding some local variables out of derivative tracking</a></li>
<li class="chapter" data-level="17.4.3" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-multiple-NF"><i class="fa fa-check"></i><b>17.4.3</b> Using AD with multiple nimbleFunctions</a></li>
<li class="chapter" data-level="17.4.4" data-path="cha-AD.html"><a href="cha-AD.html#sec:understanding-more-AD"><i class="fa fa-check"></i><b>17.4.4</b> Understanding more about how AD works: <em>taping</em> of operations</a></li>
<li class="chapter" data-level="17.4.5" data-path="cha-AD.html"><a href="cha-AD.html#resetting-a-nimderivs-call"><i class="fa fa-check"></i><b>17.4.5</b> Resetting a <code>nimDerivs</code> call</a></li>
<li class="chapter" data-level="17.4.6" data-path="cha-AD.html"><a href="cha-AD.html#a-note-on-performance-benchmarking"><i class="fa fa-check"></i><b>17.4.6</b> A note on performance benchmarking</a></li>
</ul></li>
<li class="chapter" data-level="17.5" data-path="cha-AD.html"><a href="cha-AD.html#advanced-uses-double-taping"><i class="fa fa-check"></i><b>17.5</b> Advanced uses: double taping</a></li>
<li class="chapter" data-level="17.6" data-path="cha-AD.html"><a href="cha-AD.html#derivatives-involving-model-calculations"><i class="fa fa-check"></i><b>17.6</b> Derivatives involving model calculations</a>
<ul>
<li class="chapter" data-level="17.6.1" data-path="cha-AD.html"><a href="cha-AD.html#method-1-nimderivs-of-modelcalculate"><i class="fa fa-check"></i><b>17.6.1</b> Method 1: <code>nimDerivs</code> of <code>model$calculate</code></a></li>
<li class="chapter" data-level="17.6.2" data-path="cha-AD.html"><a href="cha-AD.html#method-2-nimderivs-of-a-method-that-calls-modelcalculate"><i class="fa fa-check"></i><b>17.6.2</b> Method 2: <code>nimDerivs</code> of a method that calls <code>model$calculate</code></a></li>
</ul></li>
<li class="chapter" data-level="17.7" data-path="cha-AD.html"><a href="cha-AD.html#sec:parameter-transform"><i class="fa fa-check"></i><b>17.7</b> Parameter transformations</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="example-maximum-likelihood-estimation-using-optim-with-gradients-from-nimderivs..html"><a href="example-maximum-likelihood-estimation-using-optim-with-gradients-from-nimderivs..html"><i class="fa fa-check"></i><b>18</b> Example: maximum likelihood estimation using <code>optim</code> with gradients from <code>nimDerivs</code>.</a></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./"></a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="cha-bnp" class="section level1 hasAnchor" number="11">
<h1><span class="header-section-number">Chapter 11</span> Bayesian nonparametric models<a href="cha-bnp.html#cha-bnp" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div id="sec:bnpmixtures" class="section level2 hasAnchor" number="11.1">
<h2><span class="header-section-number">11.1</span> Bayesian nonparametric mixture models<a href="cha-bnp.html#sec:bnpmixtures" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>NIMBLE provides support for Bayesian nonparametric (BNP) mixture modeling. The current implementation provides support for hierarchical specifications involving Dirichlet process (DP) mixtures <span class="citation">(<a href="references.html#ref-ferguson_73">Ferguson 1973</a>, <a href="references.html#ref-ferguson_74">1974</a>; <a href="references.html#ref-lo_84">Lo 1984</a>; <a href="references.html#ref-escobar_94">Escobar 1994</a>; <a href="references.html#ref-escobar_west_95">Escobar and West 1995</a>)</span>. These allow one to avoid specifying a particular parametric distribution for a given node (parameter) in a model. Instead one can use a DP mixture as a much more general, nonparametric distribution. For example, a normal distribution for a random effect could be replaced by a DP mixture of normal distributions, with the number of components of the mixture being determined from the data and not fixed in advance.</p>
<p>We’ll first introduce the general, technical definition of a DP mixture model before describing the Chinese Restaurant Process representation, which may be more interpretable for many readers. More specifically, a DP mixture model for a random variable <span class="math inline">\(y_i\)</span> takes the form</p>
<p><span class="math display">\[y_i \mid G \overset{iid}{\sim} \int h(y_i \mid \theta) G(d\theta),\]</span>
<span class="math display">\[G \mid \alpha, G_0 \sim DP(\alpha, G_0),\]</span></p>
<p>where <span class="math inline">\(h(\cdot \mid \theta)\)</span> is a suitable kernel (i.e., probability density/mass function) with parameter <span class="math inline">\(\theta\)</span>, and <span class="math inline">\(\alpha\)</span> and <span class="math inline">\(G_0\)</span> are the concentration and baseline distribution parameters of the DP, respectively. DP mixture models can be written with different levels of hierarchy, all being equivalent to the model above. While “y” would often be used as notation for a data value, it is used generically here, noting that often DP mixtures are used for random effects rather than directly for observations.</p>
<p>When the random distribution (also referred to as a random ‘measure’) <span class="math inline">\(G\)</span> is integrated out from the model, the DP mixture model can be written using latent or membership variables, <span class="math inline">\(z_i\)</span>, following a Chinese Restaurant Process (CRP) distribution <span class="citation">(<a href="references.html#ref-blackwell_mcqueen_73">Blackwell and MacQueen 1973</a>)</span>, discussed in Section <a href="cha-bnp.html#sec:crp">11.2</a>. The model takes the form</p>
<p><span class="math display">\[y_i \mid \tilde{\boldsymbol{\theta}}, z_i \overset{ind}{\sim} h(\cdot \mid \tilde{\theta}_{z_i}),\]</span>
<span class="math display">\[\boldsymbol{z}\mid \alpha \sim \mbox{CRP}(\alpha),\hspace{0.5cm} \tilde{\theta}_j \overset{iid}{\sim}G_0,\]</span>
where <span class="math inline">\(\mbox{CRP}(\alpha)\)</span> denotes the CRP distribution with concentration parameter <span class="math inline">\(\alpha\)</span>. Put in perhaps more intuitive terms, <span class="math inline">\(z_i\)</span> says which cluster/group the <span class="math inline">\(i\)</span>th unit is in, and the parameter <span class="math inline">\(\tilde{\theta}_j\)</span> for group <span class="math inline">\(j\)</span> is distributed according to the <span class="math inline">\(G_0\)</span> baseline distribution. The parameter <span class="math inline">\(\alpha\)</span> controls how dispersed the clustering is, described more in the next section.</p>
<p>If a stick-breaking representation <span class="citation">(<a href="references.html#ref-sethuraman_94">Sethuraman 1994</a>)</span>, discussed in section <a href="cha-bnp.html#sec:sb">11.3</a>, is assumed for the random distribution (measure) <span class="math inline">\(G\)</span>, then the model takes the form</p>
<p><span class="math display">\[y_i \mid {\boldsymbol{\theta}}^{\star}, \boldsymbol{v} \overset{ind}{\sim} \sum_{l=1}^{\infty}\left\{ v_l\prod_{m<l}(1-v_m)\right\} h(\cdot \mid {\theta}_l^{\star}),\]</span>
<span class="math display">\[v_l \mid \alpha \overset{iid}{\sim} Beta(1, \alpha),\hspace{0.5cm} {\theta}_l^{\star} \overset{iid}{\sim}G_0.\]</span></p>
<p>More general representations of the random distribution can be specify by considering <span class="math inline">\(v_l \mid \nu_l, \alpha_l \overset{ind}{\sim} Beta(\nu_l, \alpha_l)\)</span>. Finite dimensional approximations can be obtained by truncating the infinite sum to have <span class="math inline">\(L\)</span> components.</p>
<p>Different representations of DP mixtures lead to different computational algorithms. NIMBLE supports sampling algorithms based on the CRP representation, as well as on the stick-breaking representation. NIMBLE includes definitions of structures required to implement the CRP and stick-breaking distributions, and the associated MCMC algorithms.</p>
</div>
<div id="sec:crp" class="section level2 hasAnchor" number="11.2">
<h2><span class="header-section-number">11.2</span> Chinese Restaurant Process model<a href="cha-bnp.html#sec:crp" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The CRP is a distribution over the space of partitions of positive integers and is implemented in NIMBLE as the <code>dCRP</code> distribution. More details for using this distribution are available using <code>help(CRP)</code>.</p>
<p>The CRP can be described as a stochastic process in which customers arrive at a restaurant, potentially with an infinite number of tables. Each customer sits at an empty or occupied table according to probabilities that depend on the number of customers in the occupied tables. Thus, the CRP partitions the set of customers, through their assignment to tables in the restaurant.</p>
<div id="specification-and-density-2" class="section level3 hasAnchor" number="11.2.1">
<h3><span class="header-section-number">11.2.1</span> Specification and density<a href="cha-bnp.html#specification-and-density-2" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>NIMBLE parametrizes the <code>dCRP</code> distribution by a concentration parameter and a size parameter.</p>
<div id="specification-2" class="section level4 hasAnchor" number="11.2.1.1">
<h4><span class="header-section-number">11.2.1.1</span> Specification<a href="cha-bnp.html#specification-2" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The <code>dCRP</code> distribution is specified in NIMBLE for a membership vector <code>z</code> as</p>
<p><code>z[1:N] ~ dCRP(conc, size)</code></p>
<p>The <code>conc</code> parameter is the concentration parameter of the CRP, controlling the probability of a customer sitting on a new table, i.e., creating a new cluster. The <code>size</code> parameter defines the size of the set of integers to be partitioned.</p>
<p>The <code>conc</code> parameter is a positive real value that can be treated as known or unknown. When a gamma prior is assumed for the <code>conc</code> parameter, a specialized sampler is assigned. See more on this in section <a href="cha-bnp.html#sec:mcmcdcrp">11.4.1</a>.</p>
<p>The <code>size</code> parameter is a positive integer that has to be fixed and equal to the length of vector <code>z</code>. It defines the set of consecutive integers from <code>1</code> to <code>N</code> to be partitioned. Each element in <code>z</code> can be an integer from <code>1</code> to <code>N</code>, and repetitions are allowed.</p>
</div>
<div id="density-2" class="section level4 hasAnchor" number="11.2.1.2">
<h4><span class="header-section-number">11.2.1.2</span> Density<a href="cha-bnp.html#density-2" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The CRP distribution partitions the set of positive integers <span class="math inline">\({1, \ldots, N}\)</span>, into <span class="math inline">\(N^{\star} \leq N\)</span> disjoint subsets, indicating to which subset each element belongs. For instance, if <span class="math inline">\(N=6\)</span>, the set <span class="math inline">\(\{1, 2, 3, 4, 5, 6 \}\)</span> can be partitioned into the subsets <span class="math inline">\(S_1=\{1, 2, 6\}\)</span>, <span class="math inline">\(S_2=\{4, 5\}\)</span>, and <span class="math inline">\(S_3=\{3\}\)</span>. Note that <span class="math inline">\(N^{\star} =3\)</span>, and this is one partition from out of 203 possibilities. The CRP-distributed vector <span class="math inline">\(\boldsymbol{z}\)</span> encodes this partition and its observed values would be <span class="math inline">\((1, 1, 3, 2, 2, 1)\)</span>, for this example. In mixture modeling, this indicates that observations 1, 2, and 6 belong to cluster 1, observations 4 and 5 to cluster 2, and observation 3 to cluster 3. Note that this representation is not unique, vector <span class="math inline">\((2, 2, 1, 3, 3, 2)\)</span> encodes the same partition.</p>
<p>The joint probability function of <span class="math inline">\(z=(z_1, \ldots, z_N)\)</span>, with concentration parameter <span class="math inline">\(\alpha\)</span>, is given by</p>
<p><span class="math display">\[p(\boldsymbol{z} \mid \alpha) \propto \frac{\Gamma{(\alpha)}}{\Gamma{(\alpha + n)}} \alpha^{N^{\star}(\boldsymbol{z})}\prod_{k=1}^{N^{\star}(\boldsymbol{z})}\Gamma(m_{k}(\boldsymbol{z})),\]</span></p>
<p>where <span class="math inline">\(m_k(\boldsymbol{z})\)</span> denotes the number of elements in <span class="math inline">\(\boldsymbol{z}\)</span> that are equal to <span class="math inline">\(k\)</span>, <span class="math inline">\(N^{\star}(\boldsymbol{z})\)</span> denotes the number of unique elements in <span class="math inline">\(\boldsymbol{z}\)</span>, and <span class="math inline">\(\Gamma(\cdot)\)</span> denotes the gamma function. The full conditional distribution for <span class="math inline">\(z_i\)</span> given <span class="math inline">\(z_{-i}\)</span> is</p>
<p><span class="math display">\[p(z_i = m \mid z_{-i}, \alpha) = \frac{1}{n-1+\alpha} \sum_{j \neq i}1_{\{z_j\}} (m)+ \frac{\alpha}{n-1+\alpha}1_{\{z^{new}\}}(m),\]</span></p>
<p>where <span class="math inline">\(z_{-i}\)</span> denotes vector <span class="math inline">\(\boldsymbol{z}\)</span> after removing its <span class="math inline">\(i-\)</span>th component, <span class="math inline">\(z^{new}\)</span> is a value not in <span class="math inline">\(z_{-i}\)</span>, and <span class="math inline">\(1_{A}\)</span> denotes the indicator function at set <span class="math inline">\(A\)</span>.</p>
<p>Note that the probability of creating a new cluster is proportional to <span class="math inline">\(\alpha\)</span>: the larger the concentration parameter, the more clusters are created.</p>
</div>
</div>
<div id="sec:excrp" class="section level3 hasAnchor" number="11.2.2">
<h3><span class="header-section-number">11.2.2</span> Example<a href="cha-bnp.html#sec:excrp" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The following example illustrates how to use NIMBLE to perform single density estimation for real-valued data, under a BNP approach, using the <code>dCRP</code> distribution. (Note that the BNP approach is also often used to perform density estimation on random effects.) The model is given by</p>
<p><span class="math display">\[ y_i \mid \tilde{\boldsymbol{\theta}}, \tilde{\boldsymbol{\sigma}}^2, z_i \overset{ind}{\sim} N(\tilde{\theta}_{z_i}, \tilde{\sigma}^2_{z_i}), \hspace{0.5cm} i = 1, \ldots, N, \]</span>
<span class="math display">\[ \boldsymbol{z} \sim \mbox{CRP}(\alpha), \hspace{0.5cm} \alpha \sim \mbox{Gamma}(1, 1), \]</span>
<span class="math display">\[ \tilde{\theta}_j \overset{iid}{\sim} N(0, 100), \hspace{0.5cm}\tilde{\sigma}^2_j \overset{iid}{\sim}\mbox{InvGamma}(1, 1), \hspace{0.2cm} j=1, \ldots, M. \]</span></p>
<div class="sourceCode" id="cb367"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb367-1"><a href="cha-bnp.html#cb367-1" tabindex="-1"></a>code <span class="ot"><-</span> <span class="fu">nimbleCode</span>({</span>
<span id="cb367-2"><a href="cha-bnp.html#cb367-2" tabindex="-1"></a> z[<span class="dv">1</span><span class="sc">:</span>N] <span class="sc">~</span> <span class="fu">dCRP</span>(alpha, <span class="at">size =</span> N)</span>
<span id="cb367-3"><a href="cha-bnp.html#cb367-3" tabindex="-1"></a> alpha <span class="sc">~</span> <span class="fu">dgamma</span>(<span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb367-4"><a href="cha-bnp.html#cb367-4" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>M) {</span>
<span id="cb367-5"><a href="cha-bnp.html#cb367-5" tabindex="-1"></a> thetatilde[i] <span class="sc">~</span> <span class="fu">dnorm</span>(<span class="dv">0</span>, <span class="at">var =</span> <span class="dv">100</span>)</span>
<span id="cb367-6"><a href="cha-bnp.html#cb367-6" tabindex="-1"></a> s2tilde[i] <span class="sc">~</span> <span class="fu">dinvgamma</span>(<span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb367-7"><a href="cha-bnp.html#cb367-7" tabindex="-1"></a> }</span>
<span id="cb367-8"><a href="cha-bnp.html#cb367-8" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>N) </span>
<span id="cb367-9"><a href="cha-bnp.html#cb367-9" tabindex="-1"></a> y[i] <span class="sc">~</span> <span class="fu">dnorm</span>(thetatilde[z[i]], <span class="at">var =</span> s2tilde[z[i]]) </span>
<span id="cb367-10"><a href="cha-bnp.html#cb367-10" tabindex="-1"></a>})</span>
<span id="cb367-11"><a href="cha-bnp.html#cb367-11" tabindex="-1"></a></span>
<span id="cb367-12"><a href="cha-bnp.html#cb367-12" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb367-13"><a href="cha-bnp.html#cb367-13" tabindex="-1"></a>constants <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">N =</span> <span class="dv">100</span>, <span class="at">M =</span> <span class="dv">50</span>)</span>
<span id="cb367-14"><a href="cha-bnp.html#cb367-14" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">y =</span> <span class="fu">c</span>(<span class="fu">rnorm</span>(<span class="dv">50</span>, <span class="sc">-</span><span class="dv">5</span>, <span class="fu">sqrt</span>(<span class="dv">3</span>)), <span class="fu">rnorm</span>(<span class="dv">50</span>, <span class="dv">5</span>, <span class="fu">sqrt</span>(<span class="dv">4</span>))))</span>
<span id="cb367-15"><a href="cha-bnp.html#cb367-15" tabindex="-1"></a>inits <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">thetatilde =</span> <span class="fu">rnorm</span>(constants<span class="sc">$</span>M, <span class="dv">0</span>, <span class="dv">10</span>), </span>
<span id="cb367-16"><a href="cha-bnp.html#cb367-16" tabindex="-1"></a> <span class="at">s2tilde =</span> <span class="fu">rinvgamma</span>(constants<span class="sc">$</span>M, <span class="dv">1</span>, <span class="dv">1</span>), </span>
<span id="cb367-17"><a href="cha-bnp.html#cb367-17" tabindex="-1"></a> <span class="at">z =</span> <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>, <span class="at">size =</span> constants<span class="sc">$</span>N, <span class="at">replace =</span> <span class="cn">TRUE</span>),</span>
<span id="cb367-18"><a href="cha-bnp.html#cb367-18" tabindex="-1"></a> <span class="at">alpha =</span> <span class="dv">1</span>)</span>
<span id="cb367-19"><a href="cha-bnp.html#cb367-19" tabindex="-1"></a>model <span class="ot"><-</span> <span class="fu">nimbleModel</span>(code, constants, data, inits)</span></code></pre></div>
<p>The model can be fitted through MCMC sampling. NIMBLE will assign a specialized sampler to update <code>z</code> and <code>alpha</code>. See Chapter <a href="cha-mcmc.html#cha-mcmc">7</a> for information about NIMBLE’s MCMC engine, and Section <a href="cha-bnp.html#sec:mcmcdcrp">11.4.1</a> for details on MCMC sampling of the CRP.</p>
<p>One of the advantages of BNP mixture models is that the number of clusters is treated as random. Therefore, in MCMC sampling, the number of cluster parameters varies with the iteration. Since NIMBLE does not currently allow dynamic length allocation, the number of unique cluster parameters, <span class="math inline">\(N^{\star}\)</span>, has to be fixed. One safe option is to set this number to <span class="math inline">\(N\)</span>, but this is inefficient, both in terms of computation and in terms of storage, because in practice it is often that <span class="math inline">\(N^{\star} < N\)</span>. In addition, configuring and building the MCMC can be slow (and use a lot of memory) for large <span class="math inline">\(N\)</span>. In an effort to mitigate these inefficiencies, we allow the user to set <span class="math inline">\(N^{\star} = M\)</span>, with <span class="math inline">\(M<N\)</span>, as seen in the example above. However, if this number is too small and is exceeded in any iteration a warning is issued.</p>
</div>
<div id="sec:extensionscrp" class="section level3 hasAnchor" number="11.2.3">
<h3><span class="header-section-number">11.2.3</span> Extensions<a href="cha-bnp.html#sec:extensionscrp" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The BNP functionality in NIMBLE was extended in version 0.10.0 to more general models. These extensions enable users, for instance, to use a DP or DPM prior for the distribution of the random effects in a generalized linear mixed effects model with mutiple measurements over time or multiple trials per participant.</p>
<p>The following example illustrates how to use NIMBLE in a random effects model with repeated measurements per subject using a DP prior for the distribution of the subject’s random effects. The model is given by</p>
<p><span class="math display">\[ y_{i,j} \mid \tilde{\boldsymbol{\theta}}, z_i, \sigma^2 \ {\sim}\ N(\tilde{\boldsymbol{\theta}}_{z_i}, {\sigma}^2), \hspace{0.5cm} i = 1, \ldots, N,\hspace{0.5cm} j = 1, \ldots, J, \]</span></p>
<p><span class="math display">\[ \boldsymbol{z} \sim \mbox{CRP}(\alpha), \hspace{0.5cm} \alpha \sim \mbox{Gamma}(1, 1), \]</span></p>
<p><span class="math display">\[ \tilde{\theta}_m \overset{iid}{\sim} N(0, 100), \hspace{0.2cm} m=1, \ldots, M, \hspace{0.5cm} {\sigma}^2 \sim \mbox{InvGamma}(1, 1). \]</span></p>
<p>The corresponding NIMBLE code is</p>
<div class="sourceCode" id="cb368"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb368-1"><a href="cha-bnp.html#cb368-1" tabindex="-1"></a>code <span class="ot"><-</span> <span class="fu">nimbleCode</span>({</span>
<span id="cb368-2"><a href="cha-bnp.html#cb368-2" tabindex="-1"></a> z[<span class="dv">1</span><span class="sc">:</span>N] <span class="sc">~</span> <span class="fu">dCRP</span>(alpha, <span class="at">size =</span> N)</span>
<span id="cb368-3"><a href="cha-bnp.html#cb368-3" tabindex="-1"></a> alpha <span class="sc">~</span> <span class="fu">dgamma</span>(<span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb368-4"><a href="cha-bnp.html#cb368-4" tabindex="-1"></a> sigma2 <span class="sc">~</span> <span class="fu">dinvgamma</span>(<span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb368-5"><a href="cha-bnp.html#cb368-5" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>M) {</span>
<span id="cb368-6"><a href="cha-bnp.html#cb368-6" tabindex="-1"></a> thetatilde[i] <span class="sc">~</span> <span class="fu">dnorm</span>(<span class="dv">0</span>, <span class="at">var =</span> <span class="dv">100</span>) </span>
<span id="cb368-7"><a href="cha-bnp.html#cb368-7" tabindex="-1"></a> }</span>
<span id="cb368-8"><a href="cha-bnp.html#cb368-8" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>N) {</span>
<span id="cb368-9"><a href="cha-bnp.html#cb368-9" tabindex="-1"></a> <span class="cf">for</span>(j <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>J) {</span>
<span id="cb368-10"><a href="cha-bnp.html#cb368-10" tabindex="-1"></a> y[i, j] <span class="sc">~</span> <span class="fu">dnorm</span>(thetatilde[z[i]], <span class="at">var =</span> sigma2)</span>
<span id="cb368-11"><a href="cha-bnp.html#cb368-11" tabindex="-1"></a> }</span>
<span id="cb368-12"><a href="cha-bnp.html#cb368-12" tabindex="-1"></a> }</span>
<span id="cb368-13"><a href="cha-bnp.html#cb368-13" tabindex="-1"></a>})</span>
<span id="cb368-14"><a href="cha-bnp.html#cb368-14" tabindex="-1"></a></span>
<span id="cb368-15"><a href="cha-bnp.html#cb368-15" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb368-16"><a href="cha-bnp.html#cb368-16" tabindex="-1"></a>constants <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">N =</span> <span class="dv">10</span>, <span class="at">J =</span> <span class="dv">5</span>, <span class="at">M =</span> <span class="dv">5</span>)</span>
<span id="cb368-17"><a href="cha-bnp.html#cb368-17" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">y =</span> <span class="fu">matrix</span>(<span class="fu">c</span>(<span class="fu">rnorm</span>(<span class="dv">25</span>, <span class="sc">-</span><span class="dv">25</span>, <span class="dv">1</span>), <span class="fu">rnorm</span>(<span class="dv">25</span>, <span class="dv">25</span>, <span class="dv">1</span>)), <span class="at">ncol=</span>constants<span class="sc">$</span>J,</span>
<span id="cb368-18"><a href="cha-bnp.html#cb368-18" tabindex="-1"></a> <span class="at">nrow=</span>constants<span class="sc">$</span>N, <span class="at">byrow=</span><span class="cn">TRUE</span>))</span>
<span id="cb368-19"><a href="cha-bnp.html#cb368-19" tabindex="-1"></a>inits <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">thetatilde =</span> <span class="fu">rnorm</span>(constants<span class="sc">$</span>M, <span class="dv">0</span>, <span class="dv">10</span>), </span>
<span id="cb368-20"><a href="cha-bnp.html#cb368-20" tabindex="-1"></a> <span class="at">z =</span> <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">size =</span> constants<span class="sc">$</span>N, <span class="at">replace =</span> <span class="cn">TRUE</span>),</span>
<span id="cb368-21"><a href="cha-bnp.html#cb368-21" tabindex="-1"></a> <span class="at">alpha =</span> <span class="dv">1</span>,</span>
<span id="cb368-22"><a href="cha-bnp.html#cb368-22" tabindex="-1"></a> <span class="at">sigma2 =</span> <span class="dv">1</span>)</span>
<span id="cb368-23"><a href="cha-bnp.html#cb368-23" tabindex="-1"></a>modelRandEff <span class="ot"><-</span> <span class="fu">nimbleModel</span>(code, constants, data, inits)</span></code></pre></div>
<p>Alternatively, each subject could have a vector of parameters being clustered. For example in the model above one could instead specify a vector of means, such as <code>thetaTilde[z[i], j]</code>, instead of a single mean. This allows group-specific parameters to also vary across the repeated mesasurements.</p>
<p>As before, the model can be fitted through MCMC sampling. NIMBLE will assign a specialized sampler to update <code>z</code> and <code>alpha</code>. See Chapter <a href="cha-mcmc.html#cha-mcmc">7</a> for information about NIMBLE’s MCMC engine, and Section <a href="cha-bnp.html#sec:mcmcdcrp">11.4.1</a> for details on MCMC sampling of the CRP.</p>
</div>
</div>
<div id="sec:sb" class="section level2 hasAnchor" number="11.3">
<h2><span class="header-section-number">11.3</span> Stick-breaking model<a href="cha-bnp.html#sec:sb" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>In NIMBLE, weights defined by sequentially breaking a stick, as in the stick-breaking process, are implemented as the <code>stick_breaking</code> link function. More details for using this function are available using <code>help(stick_breaking)</code>.</p>
<div id="specification-and-function" class="section level3 hasAnchor" number="11.3.1">
<h3><span class="header-section-number">11.3.1</span> Specification and function<a href="cha-bnp.html#specification-and-function" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>NIMBLE parametrizes the <code>stick_breaking</code> function by vector of values in <span class="math inline">\((0,1)\)</span>.</p>
<div id="function" class="section level4 hasAnchor" number="11.3.1.1">
<h4><span class="header-section-number">11.3.1.1</span> Function<a href="cha-bnp.html#function" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The weights <span class="math inline">\((w_1, \ldots, w_L)\)</span> follow a finite stick-breaking construction if</p>
<p><span class="math display">\[ \hspace{-2cm} w_1 = v_1, \]</span>
<span class="math display">\[ \hspace{2.2cm} w_l = v_l\prod_{m<l}(1-v_m),l=2,\ldots,L-1 \]</span>
<span class="math display">\[ \hspace{-0.4cm} w_L = \prod_{m<L}(1-v_m).\]</span></p>
<p>for <span class="math inline">\(v_l \in [0,1], l=1,\ldots,L-1\)</span>.</p>
</div>
<div id="specification-3" class="section level4 hasAnchor" number="11.3.1.2">
<h4><span class="header-section-number">11.3.1.2</span> Specification<a href="cha-bnp.html#specification-3" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>The <code>stick_breaking</code> function is specified in NIMBLE for a vector <code>w</code> of probabilities as</p>
<p><code>w[1:L] <- stick_breaking(v[1:(L-1)])</code></p>
<p>The argument <code>v</code> is a vector of values between 0 and 1 defining the sequential breaking points of the stick after removing the previous portions already broken off. It is of length <span class="math inline">\(L-1\)</span>, implicitly assuming that its last component is equal to 1.</p>
<p>In order to complete the definition of the weights in the stick-breaking representation of <span class="math inline">\(G\)</span>, a prior distribution on <span class="math inline">\((0,1)\)</span> should to be assumed for <span class="math inline">\(v_l\)</span>, <span class="math inline">\(l =1, \ldots, L-1\)</span>, for instance a beta prior.</p>
</div>
</div>
<div id="sec:exsb" class="section level3 hasAnchor" number="11.3.2">
<h3><span class="header-section-number">11.3.2</span> Example<a href="cha-bnp.html#sec:exsb" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Here we illustrate how to use NIMBLE for the example described in section <a href="cha-bnp.html#sec:excrp">11.2.2</a>, but considering a stick-breaking representation for <span class="math inline">\(G\)</span>. The model is given by</p>
<p><span class="math display">\[ y_i \mid \boldsymbol{\theta}^{\star}, {\boldsymbol{\sigma}^{\star}}^2, z_i \overset{ind}{\sim} N({{\theta}^{\star}}_{z_i}, {{\sigma}^2}^{\star}_{z_i}),\hspace{0.5cm} i = 1, \ldots, N, \]</span>
<span class="math display">\[ z_i \sim \mbox{Categorical}(\boldsymbol{w}),\hspace{0.5cm} i = 1, \ldots, N, \]</span>
<span class="math display">\[v_l\overset{iid}{\sim} \mbox{Beta}(1, \alpha), \hspace{0.2cm}l=1, \ldots, L-1, \]</span>
<span class="math display">\[ \alpha \sim \mbox{Gamma}(1, 1),\]</span>
<span class="math display">\[{\theta}^{\star}_l \overset{iid}{\sim} N(0, 100), \hspace{0.5cm}{{\sigma}^2}^{\star}_l \overset{iid}{\sim}\mbox{InvGamma}(1, 1), \hspace{0.2cm} l=1, \ldots, L.\]</span></p>
<p>where <span class="math inline">\(w_1=v_1\)</span>, <span class="math inline">\(w_l=v_l \prod_{m<l}(1-v_m)\)</span>, for <span class="math inline">\(l=1, \ldots, L-1\)</span>, and <span class="math inline">\(w_L=\prod_{m<L}(1-v_m).\)</span></p>
<div class="sourceCode" id="cb369"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb369-1"><a href="cha-bnp.html#cb369-1" tabindex="-1"></a>code <span class="ot"><-</span> <span class="fu">nimbleCode</span>({</span>
<span id="cb369-2"><a href="cha-bnp.html#cb369-2" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>(L<span class="dv">-1</span>)){</span>
<span id="cb369-3"><a href="cha-bnp.html#cb369-3" tabindex="-1"></a> v[i] <span class="sc">~</span> <span class="fu">dbeta</span>(<span class="dv">1</span>, alpha)</span>
<span id="cb369-4"><a href="cha-bnp.html#cb369-4" tabindex="-1"></a> }</span>
<span id="cb369-5"><a href="cha-bnp.html#cb369-5" tabindex="-1"></a> alpha <span class="sc">~</span> <span class="fu">dgamma</span>(<span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb369-6"><a href="cha-bnp.html#cb369-6" tabindex="-1"></a> w[<span class="dv">1</span><span class="sc">:</span>L] <span class="ot"><-</span> <span class="fu">stick_breaking</span>(v[<span class="dv">1</span><span class="sc">:</span>(L<span class="dv">-1</span>)])</span>
<span id="cb369-7"><a href="cha-bnp.html#cb369-7" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>L) {</span>
<span id="cb369-8"><a href="cha-bnp.html#cb369-8" tabindex="-1"></a> thetastar[i] <span class="sc">~</span> <span class="fu">dnorm</span>(<span class="dv">0</span>, <span class="at">var =</span> <span class="dv">100</span>)</span>
<span id="cb369-9"><a href="cha-bnp.html#cb369-9" tabindex="-1"></a> s2star[i] <span class="sc">~</span> <span class="fu">dinvgamma</span>(<span class="dv">1</span>, <span class="dv">1</span>)</span>
<span id="cb369-10"><a href="cha-bnp.html#cb369-10" tabindex="-1"></a> }</span>
<span id="cb369-11"><a href="cha-bnp.html#cb369-11" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>N) {</span>
<span id="cb369-12"><a href="cha-bnp.html#cb369-12" tabindex="-1"></a> z[i] <span class="sc">~</span> <span class="fu">dcat</span>(w[<span class="dv">1</span><span class="sc">:</span>L])</span>
<span id="cb369-13"><a href="cha-bnp.html#cb369-13" tabindex="-1"></a> y[i] <span class="sc">~</span> <span class="fu">dnorm</span>(thetastar[z[i]], <span class="at">var =</span> s2star[z[i]]) </span>
<span id="cb369-14"><a href="cha-bnp.html#cb369-14" tabindex="-1"></a> }</span>
<span id="cb369-15"><a href="cha-bnp.html#cb369-15" tabindex="-1"></a>})</span>
<span id="cb369-16"><a href="cha-bnp.html#cb369-16" tabindex="-1"></a></span>
<span id="cb369-17"><a href="cha-bnp.html#cb369-17" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb369-18"><a href="cha-bnp.html#cb369-18" tabindex="-1"></a>constants <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">N =</span> <span class="dv">100</span>, <span class="at">L=</span><span class="dv">50</span>)</span>
<span id="cb369-19"><a href="cha-bnp.html#cb369-19" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">y =</span> <span class="fu">c</span>(<span class="fu">rnorm</span>(<span class="dv">50</span>, <span class="sc">-</span><span class="dv">5</span>, <span class="fu">sqrt</span>(<span class="dv">3</span>)), <span class="fu">rnorm</span>(<span class="dv">50</span>, <span class="dv">5</span>, <span class="fu">sqrt</span>(<span class="dv">4</span>))))</span>
<span id="cb369-20"><a href="cha-bnp.html#cb369-20" tabindex="-1"></a>inits <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">thetastar =</span> <span class="fu">rnorm</span>(constants<span class="sc">$</span>L, <span class="dv">0</span>, <span class="dv">100</span>), </span>
<span id="cb369-21"><a href="cha-bnp.html#cb369-21" tabindex="-1"></a> <span class="at">s2star =</span> <span class="fu">rinvgamma</span>(constants<span class="sc">$</span>L, <span class="dv">1</span>, <span class="dv">1</span>), </span>
<span id="cb369-22"><a href="cha-bnp.html#cb369-22" tabindex="-1"></a> <span class="at">z =</span> <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>, <span class="at">size =</span> constants<span class="sc">$</span>N, <span class="at">replace =</span> <span class="cn">TRUE</span>),</span>
<span id="cb369-23"><a href="cha-bnp.html#cb369-23" tabindex="-1"></a> <span class="at">v =</span> <span class="fu">rbeta</span>(constants<span class="sc">$</span>L, <span class="dv">1</span>, <span class="dv">1</span>),</span>
<span id="cb369-24"><a href="cha-bnp.html#cb369-24" tabindex="-1"></a> <span class="at">alpha =</span> <span class="dv">1</span>)</span>
<span id="cb369-25"><a href="cha-bnp.html#cb369-25" tabindex="-1"></a>modelSB <span class="ot"><-</span> <span class="fu">nimbleModel</span>(code, constants, data, inits)</span></code></pre></div>
<p>The resulting model may be carried through to MCMC sampling. NIMBLE will assign a specialized sampler to update <code>v</code>. See Chapter <a href="cha-mcmc.html#cha-mcmc">7</a> for information about NIMBLE’s MCMC engine, and Section <a href="cha-bnp.html#sec:mcmcsb">11.4.2</a> for details on MCMC sampling of the stick-breaking weights.</p>
</div>
</div>
<div id="mcmc-sampling-of-bnp-models" class="section level2 hasAnchor" number="11.4">
<h2><span class="header-section-number">11.4</span> MCMC sampling of BNP models<a href="cha-bnp.html#mcmc-sampling-of-bnp-models" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>BNP models can be specified in different, yet equivalent, manners. Examples <a href="cha-bnp.html#sec:excrp">11.2.2</a> and <a href="cha-bnp.html#sec:exsb">11.3.2</a> are examples of density estimation for real-valued data, and are specified through the CRP and the stick-breaking process, respectively. Different specifications lead NIMBLE to assign different sampling algorithms for the model. When the model is specified through a CRP, a collapsed sampler <span class="citation">(<a href="references.html#ref-neal_2000">R. Neal 2000</a>)</span> is assigned. Under this specification, the random measure <span class="math inline">\(G\)</span> is integrated out from the model. When a stick-breaking representation is used, a blocked Gibbs sampler is assigned, see <span class="citation">Hemant Ishwaran and James (<a href="references.html#ref-ishwaran_2001">2001</a>)</span> and <span class="citation">H. Ishwaran and James (<a href="references.html#ref-ishwaran_james_2002">2002</a>)</span>.</p>
<div id="sec:mcmcdcrp" class="section level3 hasAnchor" number="11.4.1">
<h3><span class="header-section-number">11.4.1</span> Sampling CRP models<a href="cha-bnp.html#sec:mcmcdcrp" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>NIMBLE’s MCMC engine provides specialized samplers for the <code>dCRP</code> distribution, updating each component of the membership vector sequentially. Internally, the sampler is assigned based on inspection of the model structure, evaluating conjugacy between the mixture kernel and the baseline distribution, as follows:</p>
<ol style="list-style-type: decimal">
<li>A conjugate sampler in the case of the baseline distribution being conjugate for the mixture kernel.</li>
<li>A non-conjugate sampler in the case of the baseline distribution not being conjugate for the mixture kernel.</li>
</ol>
<p>Note that both samplers are specialized versions that operate on a vector having a CRP distribution. Details of these assignments are strictly internal to the CRP samplers. The current release of NIMBLE supports conjugate sampling for the <code>dCRP</code> distribution for the relationships listed in Table <a href="cha-bnp.html#tab:BNPconjugacy">11.1</a>. Additional relationships are provided in Table <a href="cha-bnp.html#tab:BNPconjugacy2">11.2</a> for normal mixture kernels when both mean and variance are unknown.</p>
<table>
<caption><span id="tab:BNPconjugacy">Table 11.1: </span> Conjugate relationships for the <code>dCRP</code> distribution supported by NIMBLE’s MCMC engine.</caption>
<thead>
<tr class="header">
<th>Baseline Distribution</th>
<th>Mixture (Dependent Node) Distribution</th>
<th align="left">Parameter</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Beta</td>
<td>Bernoulli</td>
<td align="left"><code>prob</code></td>
</tr>
<tr class="even">
<td></td>
<td>Binomial</td>
<td align="left"><code>prob</code></td>
</tr>
<tr class="odd">
<td></td>
<td>Negative Binomial</td>
<td align="left"><code>prob</code></td>
</tr>
<tr class="even">
<td>Dirichlet</td>
<td>Multinomial</td>
<td align="left"><code>prob</code></td>
</tr>
<tr class="odd">
<td>Gamma</td>
<td>Poisson</td>
<td align="left"><code>lambda</code></td>
</tr>
<tr class="even">
<td></td>
<td>Normal</td>
<td align="left"><code>tau</code></td>
</tr>
<tr class="odd">
<td></td>
<td>Gamma</td>
<td align="left"><code>rate</code></td>
</tr>
<tr class="even">
<td></td>
<td>Inverse Gamma</td>
<td align="left"><code>scale</code></td>
</tr>
<tr class="odd">
<td></td>
<td>Exponential</td>
<td align="left"><code>rate</code></td>
</tr>
<tr class="even">
<td></td>
<td>Weibull</td>
<td align="left"><code>lambda</code></td>
</tr>
<tr class="odd">
<td>Inverse Gamma</td>
<td>Normal</td>
<td align="left"><code>var</code></td>
</tr>
<tr class="even">
<td>Normal</td>
<td>Normal</td>
<td align="left"><code>mean</code></td>
</tr>
<tr class="odd">
<td>Multivariate Normal</td>
<td>Multivariate Normal</td>
<td align="left"><code>mean</code></td>
</tr>
<tr class="even">
<td>Wishart</td>
<td>Multivariate Normal</td>
<td align="left"><code>prec</code></td>
</tr>
<tr class="odd">
<td>Inverse Wishart</td>
<td>Multivariate Normal</td>
<td align="left"><code>cov</code></td>
</tr>
</tbody>
</table>
<table>
<caption><span id="tab:BNPconjugacy2">Table 11.2: </span> Additional conjugate relationships for the <code>dCRP</code> distribution supported by NIMBLE’s MCMC engine.</caption>
<thead>
<tr class="header">
<th align="left">Baseline Distribution</th>
<th>Mixture (Dependent Node) Distribution</th>
<th align="left">Parameter</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Normal-Gamma</td>
<td>Normal</td>
<td align="left"><code>mean</code>, <code>tau</code></td>
</tr>
<tr class="even">
<td align="left">Normal-Inverse Gamma</td>
<td>Normal</td>
<td align="left"><code>mean</code>, <code>var</code></td>
</tr>
<tr class="odd">
<td align="left">Multivariate Normal-Wishart</td>
<td>Multivariate Normal</td>
<td align="left"><code>mean</code>, <code>prec</code></td>
</tr>
<tr class="even">
<td align="left">Multivariate Normal-Inverse Wishart</td>
<td>Multivariate Normal</td>
<td align="left"><code>mean</code>, <code>cov</code></td>
</tr>
</tbody>
</table>
<p>To reduce computation and improve mixing, we only sample the parameters of the clusters (e.g., <span class="math inline">\(\tilde{\theta}_j\)</span> in <a href="cha-bnp.html#sec:excrp">11.2.2</a> when the associated cluster is occupied, using the <code>CRP_cluster_wrapper</code> sampler, which wraps around an underlying actual sampler. In addition, this approach requires that any sampler assigned to parameters of the base measure, <span class="math inline">\(G_0\)</span>, (i.e., unknown parameters in the prior for <span class="math inline">\(\tilde{\theta}_j\)</span>) ignore cluster parameters associated with clusters that are not occupied, since their current values are meaningless. We assign a special slice sampler that determines the occupied clusters in any given iteration, called <code>slice_CRP_base_param</code>. Note that if you choose to use a different sampler for the base measure parameters, you should also avoid using the <code>CRP_cluster_wrapper</code> sampler.</p>
<p>Finally, a specialized sampler is assigned to the <code>conc</code> hyper parameter when a gamma hyper prior is assigned, see section 6 in <span class="citation">Escobar and West (<a href="references.html#ref-escobar_west_95">1995</a>)</span> for more details. Otherwise, a random walk Metropolis-Hastings sampler is assigned.</p>
<div id="initial-values-1" class="section level4 hasAnchor" number="11.4.1.1">
<h4><span class="header-section-number">11.4.1.1</span> Initial values<a href="cha-bnp.html#initial-values-1" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>Valid initial values should be provided for all elements of the process specified by a CRP structure before running the MCMC. A simple and safe choice for <code>z</code> is to provide a sample of size <code>N</code>, the same as its length, of values between 1 and some reasonable number of clusters (less than or equal to the length of <code>z</code>), with replacement, as done in the preceding CRP example. For the concentration parameter, a safe initial value is 1.</p>
</div>
<div id="sampling-the-random-measure" class="section level4 hasAnchor" number="11.4.1.2">
<h4><span class="header-section-number">11.4.1.2</span> Sampling the random measure<a href="cha-bnp.html#sampling-the-random-measure" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>In BNP models, it is oftenly of interest to make inference about the unknown measure <span class="math inline">\(G\)</span>. NIMBLE provides the <code>getSamplesDPmeasure</code> sampler to generate posterior samples of the random measure <span class="math inline">\(G\)</span> when a CRP structure is involved in the model.</p>
<p>The <code>getSamplesDPmeasure</code> function has two arguments: <code>MCMC</code> and <code>epsilon</code>.</p>
<p>The <code>MCMC</code> argument is a compiled or uncompiled MCMC object. The MCMC object should monitor the membership (or clustering) variable, the cluster parameters, all stochastic nodes of the cluster parameters, and the concentration parameter, if it is random. Use the <code>monitors</code> argument when configuring the MCMC to ensure these variables are monitored.</p>
<p>The <code>epsilon</code> argument is used to determine the truncation level of the representation of the random measure. Its default value is <span class="math inline">\(1e-04\)</span>.</p>
<p>The sampler is used only after the MCMC for the model has been run; more details are available from <code>help(getsamplesDPmeasure)</code>.</p>
<p>Posterior samples of the random measure <span class="math inline">\(G\)</span> are (almost surely) of the form <span class="math inline">\(\sum_{l=1}^{\infty}w_l\delta_{\theta_l}\)</span>, where <span class="math inline">\(\delta_{\theta}\)</span> is the Dirac measure at <span class="math inline">\(\theta\)</span>, <span class="math inline">\(w_l\)</span> are stick-breaking weights, and <span class="math inline">\(\theta_l\)</span> are atoms (or point masses). The variables that define the stick-breaking weights are iid <span class="math inline">\(Beta(1, \alpha + N)\)</span> distributed, where <span class="math inline">\(\alpha\)</span> is the concentration parameter of the CRP distribution and <span class="math inline">\(N\)</span> is the sample size. Independently, the atoms, <span class="math inline">\(\theta_l\)</span>, are iid and follow a distribution of the form</p>
<p><span class="math display">\[(\alpha+N)^{-1}\sum_{k=1}^{N^{\star}(\boldsymbol{z})}m_k(\boldsymbol{z})\delta_{\{\tilde{\theta}_k\}} + \alpha(\alpha+N)^{-1}G_0,\]</span></p>
<p>where <span class="math inline">\(G_0\)</span> is the prior baseline distribution, and <span class="math inline">\(\boldsymbol{z}\)</span> and <span class="math inline">\(\tilde{\theta}_k\)</span> are posterior samples of the labeling vector and cluster parameters, respectively. Their values, together with the values of <span class="math inline">\(\alpha\)</span> (if random) are obtained from the MCMC’s output. Expressions <span class="math inline">\(m_k(\boldsymbol{z})\)</span> and <span class="math inline">\(N^{\star}(\boldsymbol{z})\)</span> are defined as in section <a href="cha-bnp.html#sec:crp">11.2</a>.</p>
<p>The <code>getsamplesDPmeasure</code> function provides samples of a truncated version of the infinite mixture to a level <span class="math inline">\(L\)</span>. The truncation level <span class="math inline">\(L\)</span> is such that the tail probability left from the approximation is at most <code>epsilon</code>, denoted <span class="math inline">\(\epsilon\)</span>. The following relationship determines the truncation level: <span class="math inline">\(L = \log(\epsilon) / \log[(\alpha+N) / (\alpha + N +1)]\)</span>. The value of <span class="math inline">\(L\)</span> varies at each iteration of the MCMC’s output when <span class="math inline">\(\alpha\)</span> is random, while it is the same at each iteration when <span class="math inline">\(\alpha\)</span> is fixed. For more details about sampling the random measure and its truncation level see <span class="citation">Gelfand and Kottas (<a href="references.html#ref-gelfand_2002">2002</a>)</span>.</p>
<p>Because of the discrete nature of the atom’s distribution, the stick-breaking representation truncated to a level <span class="math inline">\(L\)</span> will generally have repeated values. Therefore, in order to reduce the output’s dimensionality, the stick-breaking weights of identical atoms are added up. This results in samples of <span class="math inline">\(G\)</span> that have only the ‘unique’ atoms in <span class="math inline">\(\{\theta_l\}_{l=1}^L\)</span>.</p>
<p>The following code exemplifies how to generate samples from <span class="math inline">\(G\)</span> after defining the model as in Section <a href="cha-bnp.html#sec:excrp">11.2.2</a>.</p>
<div class="sourceCode" id="cb370"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb370-1"><a href="cha-bnp.html#cb370-1" tabindex="-1"></a>cmodel <span class="ot"><-</span> <span class="fu">compileNimble</span>(model)</span>
<span id="cb370-2"><a href="cha-bnp.html#cb370-2" tabindex="-1"></a></span>
<span id="cb370-3"><a href="cha-bnp.html#cb370-3" tabindex="-1"></a>monitors <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">'z'</span>, <span class="st">'thetatilde'</span>, <span class="st">'s2tilde'</span> , <span class="st">'alpha'</span>)</span>
<span id="cb370-4"><a href="cha-bnp.html#cb370-4" tabindex="-1"></a>modelConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(model, <span class="at">monitors =</span> monitors)</span>
<span id="cb370-5"><a href="cha-bnp.html#cb370-5" tabindex="-1"></a>modelMCMC <span class="ot"><-</span> <span class="fu">buildMCMC</span>(modelConf)</span>
<span id="cb370-6"><a href="cha-bnp.html#cb370-6" tabindex="-1"></a></span>
<span id="cb370-7"><a href="cha-bnp.html#cb370-7" tabindex="-1"></a>cmodelMCMC <span class="ot"><-</span> <span class="fu">compileNimble</span>(modelMCMC, <span class="at">project =</span> model)</span>
<span id="cb370-8"><a href="cha-bnp.html#cb370-8" tabindex="-1"></a>cmodelMCMC<span class="sc">$</span><span class="fu">run</span>(<span class="dv">1000</span>)</span>
<span id="cb370-9"><a href="cha-bnp.html#cb370-9" tabindex="-1"></a></span>
<span id="cb370-10"><a href="cha-bnp.html#cb370-10" tabindex="-1"></a>samplesG <span class="ot"><-</span> <span class="fu">getSamplesDPmeasure</span>(cmodelMCMC)</span></code></pre></div>
</div>
</div>
<div id="sec:mcmcsb" class="section level3 hasAnchor" number="11.4.2">
<h3><span class="header-section-number">11.4.2</span> Sampling stick-breaking models<a href="cha-bnp.html#sec:mcmcsb" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>NIMBLE’s MCMC engine provides specialized samplers for the
beta-distributed random variables that are the arguments to the stick-breaking function, updating each component of the weight vector sequentially. The sampler is assigned based on inspection of the model structure. Specifically, the specialized sampler is assigned when the membership vector has a categorical distribution, its weights are defined by a stick-breaking function, and the vector defining the weights follows a beta distribution.</p>
<div id="initial-values-2" class="section level4 hasAnchor" number="11.4.2.1">
<h4><span class="header-section-number">11.4.2.1</span> Initial values<a href="cha-bnp.html#initial-values-2" class="anchor-section" aria-label="Anchor link to header"></a></h4>
<p>Valid initial values should be provided for all elements of the stick-breaking function and membership variable before running the MCMC. A simple and safe choice for <span class="math inline">\(z\)</span> is to provide a sample of size <span class="math inline">\(N\)</span>, of values between 1 and some value less than <span class="math inline">\(L\)</span>, with replacement, as done in the preceding stick-breaking example. For the stick variables, safe initial values can be simulated from a beta distribution.</p>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="cha-spatial.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="overview.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["NimbleUserManual.pdf", "NimbleUserManual.epub"],
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
},
"toc_depth": 3
});
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
src = src.replace(/^https?:/, '');
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>