-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcha-lightning-intro.html
1112 lines (1074 loc) · 109 KB
/
cha-lightning-intro.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 2 Lightning introduction | NimbleUserManual.knit</title>
<meta name="description" content="This is the NIMBLE User Manual." />
<meta name="generator" content="bookdown 0.39 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 2 Lightning introduction | 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 2 Lightning introduction | 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-welcome-nimble.html"/>
<link rel="next" href="cha-more-introduction.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 { display: inline-block; 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;
margin-bottom: 0em;
}
.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.3.0</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>
</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>MCMCsuite</em> and <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, MCEM, Laplace approximation and quadrature</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>
<li class="chapter" data-level="8.3" data-path="cha-algos-provided.html"><a href="cha-algos-provided.html#laplace-approximation-and-adaptive-gauss-hermite-quadrature"><i class="fa fa-check"></i><b>8.3</b> Laplace approximation and adaptive Gauss-Hermite quadrature</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="cha-spatial.html"><a href="cha-spatial.html"><i class="fa fa-check"></i><b>9</b> Spatial models</a>
<ul>
<li class="chapter" data-level="9.1" data-path="cha-spatial.html"><a href="cha-spatial.html#intrinsic-gaussian-car-model-dcar_normal"><i class="fa fa-check"></i><b>9.1</b> Intrinsic Gaussian CAR model: <em>dcar_normal</em></a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="cha-spatial.html"><a href="cha-spatial.html#specification-and-density"><i class="fa fa-check"></i><b>9.1.1</b> Specification and density</a></li>
<li class="chapter" data-level="9.1.2" data-path="cha-spatial.html"><a href="cha-spatial.html#example"><i class="fa fa-check"></i><b>9.1.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="cha-spatial.html"><a href="cha-spatial.html#proper-gaussian-car-model-dcar_proper"><i class="fa fa-check"></i><b>9.2</b> Proper Gaussian CAR model: <em>dcar_proper</em></a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="cha-spatial.html"><a href="cha-spatial.html#specification-and-density-1"><i class="fa fa-check"></i><b>9.2.1</b> Specification and density</a></li>
<li class="chapter" data-level="9.2.2" data-path="cha-spatial.html"><a href="cha-spatial.html#example-1"><i class="fa fa-check"></i><b>9.2.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="cha-spatial.html"><a href="cha-spatial.html#sec:spatial-mcmc-sampling-car"><i class="fa fa-check"></i><b>9.3</b> MCMC Sampling of CAR models</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="cha-spatial.html"><a href="cha-spatial.html#initial-values"><i class="fa fa-check"></i><b>9.3.1</b> Initial values</a></li>
<li class="chapter" data-level="9.3.2" data-path="cha-spatial.html"><a href="cha-spatial.html#zero-neighbor-regions"><i class="fa fa-check"></i><b>9.3.2</b> Zero-neighbor regions</a></li>
<li class="chapter" data-level="9.3.3" data-path="cha-spatial.html"><a href="cha-spatial.html#zero-mean-constraint"><i class="fa fa-check"></i><b>9.3.3</b> Zero-mean constraint</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="cha-bnp.html"><a href="cha-bnp.html"><i class="fa fa-check"></i><b>10</b> Bayesian nonparametric models</a>
<ul>
<li class="chapter" data-level="10.1" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:bnpmixtures"><i class="fa fa-check"></i><b>10.1</b> Bayesian nonparametric mixture models</a></li>
<li class="chapter" data-level="10.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:crp"><i class="fa fa-check"></i><b>10.2</b> Chinese Restaurant Process model</a>
<ul>
<li class="chapter" data-level="10.2.1" data-path="cha-bnp.html"><a href="cha-bnp.html#specification-and-density-2"><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-bnp.html"><a href="cha-bnp.html#sec:excrp"><i class="fa fa-check"></i><b>10.2.2</b> Example</a></li>
<li class="chapter" data-level="10.2.3" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:extensionscrp"><i class="fa fa-check"></i><b>10.2.3</b> Extensions</a></li>
</ul></li>
<li class="chapter" data-level="10.3" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:sb"><i class="fa fa-check"></i><b>10.3</b> Stick-breaking model</a>
<ul>
<li class="chapter" data-level="10.3.1" data-path="cha-bnp.html"><a href="cha-bnp.html#specification-and-function"><i class="fa fa-check"></i><b>10.3.1</b> Specification and function</a></li>
<li class="chapter" data-level="10.3.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:exsb"><i class="fa fa-check"></i><b>10.3.2</b> Example</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="cha-bnp.html"><a href="cha-bnp.html#mcmc-sampling-of-bnp-models"><i class="fa fa-check"></i><b>10.4</b> MCMC sampling of BNP models</a>
<ul>
<li class="chapter" data-level="10.4.1" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:mcmcdcrp"><i class="fa fa-check"></i><b>10.4.1</b> Sampling CRP models</a></li>
<li class="chapter" data-level="10.4.2" data-path="cha-bnp.html"><a href="cha-bnp.html#sec:mcmcsb"><i class="fa fa-check"></i><b>10.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="11" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html"><i class="fa fa-check"></i><b>11</b> Writing simple nimbleFunctions</a>
<ul>
<li class="chapter" data-level="11.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:RC-intro"><i class="fa fa-check"></i><b>11.1</b> Introduction to simple nimbleFunctions</a></li>
<li class="chapter" data-level="11.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:r-fiunctions-implemented"><i class="fa fa-check"></i><b>11.2</b> R functions (or variants) implemented in NIMBLE</a>
<ul>
<li class="chapter" data-level="11.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>11.2.1</b> Finding help for NIMBLE’s versions of R functions</a></li>
<li class="chapter" data-level="11.2.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#basic-operations"><i class="fa fa-check"></i><b>11.2.2</b> Basic operations</a></li>
<li class="chapter" data-level="11.2.3" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:basic-math-linear"><i class="fa fa-check"></i><b>11.2.3</b> Math and linear algebra</a></li>
<li class="chapter" data-level="11.2.4" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:nimble-dist-funs"><i class="fa fa-check"></i><b>11.2.4</b> Distribution functions</a></li>
<li class="chapter" data-level="11.2.5" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:basic-flow-control"><i class="fa fa-check"></i><b>11.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="11.2.6" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:print"><i class="fa fa-check"></i><b>11.2.6</b> <em>print</em> and <em>cat</em></a></li>
<li class="chapter" data-level="11.2.7" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:check-user-interr"><i class="fa fa-check"></i><b>11.2.7</b> Checking for user interrupts: <em>checkInterrupt</em></a></li>
<li class="chapter" data-level="11.2.8" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#optimization-optim-and-nimoptim"><i class="fa fa-check"></i><b>11.2.8</b> Optimization: <em>optim</em> and <em>nimOptim</em></a></li>
<li class="chapter" data-level="11.2.9" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#integration-integrate-and-nimintegrate"><i class="fa fa-check"></i><b>11.2.9</b> Integration: <em>integrate</em> and <em>nimIntegrate</em></a></li>
<li class="chapter" data-level="11.2.10" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:altern-keyw-some"><i class="fa fa-check"></i><b>11.2.10</b> ‘nim’ synonyms for some functions</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:how-nimble-handles"><i class="fa fa-check"></i><b>11.3</b> How NIMBLE handles types of variables</a>
<ul>
<li class="chapter" data-level="11.3.1" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:nimbleList-RCFuns"><i class="fa fa-check"></i><b>11.3.1</b> nimbleList data structures</a></li>
<li class="chapter" data-level="11.3.2" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:how-types-work"><i class="fa fa-check"></i><b>11.3.2</b> How numeric types work</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:decl-argum-return"><i class="fa fa-check"></i><b>11.4</b> Declaring argument and return types</a></li>
<li class="chapter" data-level="11.5" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:comp-nimbl-pass"><i class="fa fa-check"></i><b>11.5</b> Compiled nimbleFunctions pass arguments by reference</a></li>
<li class="chapter" data-level="11.6" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:calling-external-code"><i class="fa fa-check"></i><b>11.6</b> Calling external compiled code</a></li>
<li class="chapter" data-level="11.7" data-path="cha-RCfunctions.html"><a href="cha-RCfunctions.html#sec:calling-R-code"><i class="fa fa-check"></i><b>11.7</b> Calling uncompiled R functions from compiled nimbleFunctions</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="cha-user-defined.html"><a href="cha-user-defined.html"><i class="fa fa-check"></i><b>12</b> Creating user-defined distributions and functions for models</a>
<ul>
<li class="chapter" data-level="12.1" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-functions"><i class="fa fa-check"></i><b>12.1</b> User-defined functions</a></li>
<li class="chapter" data-level="12.2" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-distributions"><i class="fa fa-check"></i><b>12.2</b> User-defined distributions</a>
<ul>
<li class="chapter" data-level="12.2.1" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:registerDistributions"><i class="fa fa-check"></i><b>12.2.1</b> Using <em>registerDistributions</em> for alternative parameterizations and providing other information</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:adv-user-def"><i class="fa fa-check"></i><b>12.3</b> Advanced user-defined functions and distributions</a></li>
<li class="chapter" data-level="12.4" data-path="cha-user-defined.html"><a href="cha-user-defined.html#sec:user-macros"><i class="fa fa-check"></i><b>12.4</b> User-defined model macros</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="cha-using-models.html"><a href="cha-using-models.html"><i class="fa fa-check"></i><b>13</b> Working with NIMBLE models</a>
<ul>
<li class="chapter" data-level="13.1" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:accessing-variables"><i class="fa fa-check"></i><b>13.1</b> The variables and nodes in a NIMBLE model</a>
<ul>
<li class="chapter" data-level="13.1.1" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:get-nodes"><i class="fa fa-check"></i><b>13.1.1</b> Determining the nodes in a model</a></li>
<li class="chapter" data-level="13.1.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:introduced-nodes"><i class="fa fa-check"></i><b>13.1.2</b> Understanding lifted nodes</a></li>
<li class="chapter" data-level="13.1.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdgetdependencies"><i class="fa fa-check"></i><b>13.1.3</b> Determining dependencies in a model</a></li>
</ul></li>
<li class="chapter" data-level="13.2" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:nodeInfo"><i class="fa fa-check"></i><b>13.2</b> Accessing information about nodes and variables</a>
<ul>
<li class="chapter" data-level="13.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>13.2.1</b> Getting distributional information about a node</a></li>
<li class="chapter" data-level="13.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>13.2.2</b> Getting information about a distribution</a></li>
<li class="chapter" data-level="13.2.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:getParam"><i class="fa fa-check"></i><b>13.2.3</b> Getting distribution parameter values for a node</a></li>
<li class="chapter" data-level="13.2.4" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:getBound"><i class="fa fa-check"></i><b>13.2.4</b> Getting distribution bounds for a node</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="cha-using-models.html"><a href="cha-using-models.html#sec:cdcalc-cdsim-cdgetl"><i class="fa fa-check"></i><b>13.3</b> Carrying out model calculations</a>
<ul>
<li class="chapter" data-level="13.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>13.3.1</b> Core model operations: calculation and simulation</a></li>
<li class="chapter" data-level="13.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>13.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="13.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>13.3.3</b> Accessing log probabilities via <em>logProb</em> variables</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="14" data-path="cha-data-structures.html"><a href="cha-data-structures.html"><i class="fa fa-check"></i><b>14</b> Data structures in NIMBLE</a>
<ul>
<li class="chapter" data-level="14.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:modelValues-struct"><i class="fa fa-check"></i><b>14.1</b> The modelValues data structure</a>
<ul>
<li class="chapter" data-level="14.1.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#creating-modelvalues-objects"><i class="fa fa-check"></i><b>14.1.1</b> Creating modelValues objects</a></li>
<li class="chapter" data-level="14.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>14.1.2</b> Accessing contents of modelValues</a></li>
</ul></li>
<li class="chapter" data-level="14.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:nimbleLists"><i class="fa fa-check"></i><b>14.2</b> The nimbleList data structure</a>
<ul>
<li class="chapter" data-level="14.2.1" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:predef-nimbleLists"><i class="fa fa-check"></i><b>14.2.1</b> Pre-defined nimbleList types</a></li>
<li class="chapter" data-level="14.2.2" data-path="cha-data-structures.html"><a href="cha-data-structures.html#sec:eigen-nimFunctions"><i class="fa fa-check"></i><b>14.2.2</b> Using <em>eigen</em> and <em>svd</em> in nimbleFunctions</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="15" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html"><i class="fa fa-check"></i><b>15</b> Writing nimbleFunctions to interact with models</a>
<ul>
<li class="chapter" data-level="15.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>15.1</b> Overview</a></li>
<li class="chapter" data-level="15.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>15.2</b> Using and compiling nimbleFunctions</a></li>
<li class="chapter" data-level="15.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>15.3</b> Writing setup code</a>
<ul>
<li class="chapter" data-level="15.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>15.3.1</b> Useful tools for setup functions</a></li>
<li class="chapter" data-level="15.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>15.3.2</b> Accessing and modifying numeric values from setup</a></li>
<li class="chapter" data-level="15.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>15.3.3</b> Determining numeric types in nimbleFunctions</a></li>
<li class="chapter" data-level="15.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>15.3.4</b> Control of setup outputs</a></li>
</ul></li>
<li class="chapter" data-level="15.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>15.4</b> Writing run code</a>
<ul>
<li class="chapter" data-level="15.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>15.4.1</b> Driving models: <em>calculate</em>, <em>calculateDiff</em>, <em>simulate</em>, <em>getLogProb</em></a></li>
<li class="chapter" data-level="15.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>15.4.2</b> Getting and setting variable and node values</a></li>
<li class="chapter" data-level="15.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>15.4.3</b> Getting parameter values and node bounds</a></li>
<li class="chapter" data-level="15.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>15.4.4</b> Using modelValues objects</a></li>
<li class="chapter" data-level="15.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>15.4.5</b> Using model variables and modelValues in expressions</a></li>
<li class="chapter" data-level="15.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>15.4.6</b> Including other methods in a nimbleFunction</a></li>
<li class="chapter" data-level="15.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>15.4.7</b> Using other nimbleFunctions</a></li>
<li class="chapter" data-level="15.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>15.4.8</b> Virtual nimbleFunctions and nimbleFunctionLists</a></li>
<li class="chapter" data-level="15.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>15.4.9</b> Character objects</a></li>
<li class="chapter" data-level="15.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>15.4.10</b> User-defined data structures</a></li>
</ul></li>
<li class="chapter" data-level="15.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>15.5</b> Example: writing user-defined samplers to extend NIMBLE’s MCMC engine</a>
<ul>
<li class="chapter" data-level="15.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>15.5.1</b> User-defined samplers and posterior predictive nodes</a></li>
</ul></li>
<li class="chapter" data-level="15.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>15.6</b> Copying nimbleFunctions (and NIMBLE models)</a></li>
<li class="chapter" data-level="15.7" data-path="cha-progr-with-models.html"><a href="cha-progr-with-models.html#sec:debugging"><i class="fa fa-check"></i><b>15.7</b> Debugging nimbleFunctions</a></li>
<li class="chapter" data-level="15.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>15.8</b> Timing nimbleFunctions with <em>run.time</em></a></li>
<li class="chapter" data-level="15.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>15.9</b> Clearing and unloading compiled objects</a></li>
<li class="chapter" data-level="15.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>15.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="16" data-path="cha-AD.html"><a href="cha-AD.html"><i class="fa fa-check"></i><b>16</b> Automatic Derivatives</a>
<ul>
<li class="chapter" data-level="16.1" data-path="cha-AD.html"><a href="cha-AD.html#sec:use-derivs"><i class="fa fa-check"></i><b>16.1</b> How to turn on derivatives in a model</a>
<ul>
<li class="chapter" data-level="16.1.1" data-path="cha-AD.html"><a href="cha-AD.html#finish-setting-up-the-glmm-example"><i class="fa fa-check"></i><b>16.1.1</b> Finish setting up the GLMM example</a></li>
</ul></li>
<li class="chapter" data-level="16.2" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-laplace"><i class="fa fa-check"></i><b>16.2</b> How to use Laplace approximation and adaptive Gauss-Hermite quadrature</a>
<ul>
<li class="chapter" data-level="16.2.1" data-path="cha-AD.html"><a href="cha-AD.html#using-the-laplace-approximation-methods-directly"><i class="fa fa-check"></i><b>16.2.1</b> Using the Laplace approximation methods directly</a></li>
<li class="chapter" data-level="16.2.2" data-path="cha-AD.html"><a href="cha-AD.html#changing-the-optimization-methods"><i class="fa fa-check"></i><b>16.2.2</b> Changing the optimization methods</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-user-def"><i class="fa fa-check"></i><b>16.3</b> How to support derivatives in user-defined functions and distributions</a></li>
<li class="chapter" data-level="16.4" 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>16.4</b> What operations are and aren’t supported for AD</a></li>
<li class="chapter" data-level="16.5" data-path="cha-AD.html"><a href="cha-AD.html#basics-of-obtaining-derivatives-in-nimblefunctions"><i class="fa fa-check"></i><b>16.5</b> Basics of obtaining derivatives in <code>nimbleFunctions</code></a>
<ul>
<li class="chapter" data-level="16.5.1" data-path="cha-AD.html"><a href="cha-AD.html#checking-derivatives-with-uncompiled-execution"><i class="fa fa-check"></i><b>16.5.1</b> Checking derivatives with uncompiled execution</a></li>
<li class="chapter" data-level="16.5.2" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-holding-out"><i class="fa fa-check"></i><b>16.5.2</b> Holding some local variables out of derivative tracking</a></li>
<li class="chapter" data-level="16.5.3" data-path="cha-AD.html"><a href="cha-AD.html#sec:AD-multiple-NF"><i class="fa fa-check"></i><b>16.5.3</b> Using AD with multiple nimbleFunctions</a></li>
<li class="chapter" data-level="16.5.4" data-path="cha-AD.html"><a href="cha-AD.html#sec:understanding-more-AD"><i class="fa fa-check"></i><b>16.5.4</b> Understanding more about how AD works: <em>taping</em> of operations</a></li>
<li class="chapter" data-level="16.5.5" data-path="cha-AD.html"><a href="cha-AD.html#resetting-a-nimderivs-call"><i class="fa fa-check"></i><b>16.5.5</b> Resetting a <code>nimDerivs</code> call</a></li>
<li class="chapter" data-level="16.5.6" data-path="cha-AD.html"><a href="cha-AD.html#a-note-on-performance-benchmarking"><i class="fa fa-check"></i><b>16.5.6</b> A note on performance benchmarking</a></li>
</ul></li>
<li class="chapter" data-level="16.6" data-path="cha-AD.html"><a href="cha-AD.html#advanced-uses-double-taping"><i class="fa fa-check"></i><b>16.6</b> Advanced uses: double taping</a></li>
<li class="chapter" data-level="16.7" data-path="cha-AD.html"><a href="cha-AD.html#derivatives-involving-model-calculations"><i class="fa fa-check"></i><b>16.7</b> Derivatives involving model calculations</a>
<ul>
<li class="chapter" data-level="16.7.1" data-path="cha-AD.html"><a href="cha-AD.html#method-1-nimderivs-of-modelcalculate"><i class="fa fa-check"></i><b>16.7.1</b> Method 1: <code>nimDerivs</code> of <code>model$calculate</code></a></li>
<li class="chapter" data-level="16.7.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>16.7.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="16.8" data-path="cha-AD.html"><a href="cha-AD.html#sec:parameter-transform"><i class="fa fa-check"></i><b>16.8</b> Parameter transformations</a></li>
</ul></li>
<li class="chapter" data-level="17" 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>17</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-lightning-intro" class="section level1 hasAnchor" number="2">
<h1><span class="header-section-number">Chapter 2</span> Lightning introduction<a href="cha-lightning-intro.html#cha-lightning-intro" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<div id="sec:brief-example" class="section level2 hasAnchor" number="2.1">
<h2><span class="header-section-number">2.1</span> A brief example<a href="cha-lightning-intro.html#sec:brief-example" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Here we’ll give a simple example of building a model and running some algorithms on the model, as well as creating our own user-specified algorithm. The goal is to give you a sense for what one can do in the system. Later sections will provide more detail.</p>
<p>We’ll use the <em>pump</em> model example from BUGS<a href="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a>. We could load the model
from the standard BUGS example file formats (Section <a href="cha-building-models.html#sec:readBUGSmodel">6.1.2</a>), but instead we’ll show
how to enter it directly in R.</p>
<p>In this ‘lightning introduction’ we will:</p>
<ol style="list-style-type: decimal">
<li>Create the model for the pump example.</li>
<li>Compile the model.</li>
<li>Create a basic MCMC configuration for the pump model.</li>
<li>Compile and run the MCMC</li>
<li>Customize the MCMC configuration and compile and run that.</li>
<li>Create, compile and run a Monte Carlo Expectation Maximization (MCEM)
algorithm, which illustrates some of the flexibility NIMBLE
provides to combine R and NIMBLE.</li>
<li>Write a short <code>nimbleFunction</code> to generate simulations from
designated nodes of any model.</li>
</ol>
</div>
<div id="sec:creating-model" class="section level2 hasAnchor" number="2.2">
<h2><span class="header-section-number">2.2</span> Creating a model<a href="cha-lightning-intro.html#sec:creating-model" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>First we define the model code, its constants, data, and initial
values for MCMC.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="cha-lightning-intro.html#cb1-1" tabindex="-1"></a>pumpCode <span class="ot"><-</span> <span class="fu">nimbleCode</span>({ </span>
<span id="cb1-2"><a href="cha-lightning-intro.html#cb1-2" 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="cb1-3"><a href="cha-lightning-intro.html#cb1-3" tabindex="-1"></a> theta[i] <span class="sc">~</span> <span class="fu">dgamma</span>(alpha,beta)</span>
<span id="cb1-4"><a href="cha-lightning-intro.html#cb1-4" tabindex="-1"></a> lambda[i] <span class="ot"><-</span> theta[i]<span class="sc">*</span>t[i]</span>
<span id="cb1-5"><a href="cha-lightning-intro.html#cb1-5" tabindex="-1"></a> x[i] <span class="sc">~</span> <span class="fu">dpois</span>(lambda[i])</span>
<span id="cb1-6"><a href="cha-lightning-intro.html#cb1-6" tabindex="-1"></a> }</span>
<span id="cb1-7"><a href="cha-lightning-intro.html#cb1-7" tabindex="-1"></a> alpha <span class="sc">~</span> <span class="fu">dexp</span>(<span class="fl">1.0</span>)</span>
<span id="cb1-8"><a href="cha-lightning-intro.html#cb1-8" tabindex="-1"></a> beta <span class="sc">~</span> <span class="fu">dgamma</span>(<span class="fl">0.1</span>,<span class="fl">1.0</span>)</span>
<span id="cb1-9"><a href="cha-lightning-intro.html#cb1-9" tabindex="-1"></a>})</span>
<span id="cb1-10"><a href="cha-lightning-intro.html#cb1-10" tabindex="-1"></a></span>
<span id="cb1-11"><a href="cha-lightning-intro.html#cb1-11" tabindex="-1"></a>pumpConsts <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">N =</span> <span class="dv">10</span>,</span>
<span id="cb1-12"><a href="cha-lightning-intro.html#cb1-12" tabindex="-1"></a> <span class="at">t =</span> <span class="fu">c</span>(<span class="fl">94.3</span>, <span class="fl">15.7</span>, <span class="fl">62.9</span>, <span class="dv">126</span>, <span class="fl">5.24</span>,</span>
<span id="cb1-13"><a href="cha-lightning-intro.html#cb1-13" tabindex="-1"></a> <span class="fl">31.4</span>, <span class="fl">1.05</span>, <span class="fl">1.05</span>, <span class="fl">2.1</span>, <span class="fl">10.5</span>))</span>
<span id="cb1-14"><a href="cha-lightning-intro.html#cb1-14" tabindex="-1"></a></span>
<span id="cb1-15"><a href="cha-lightning-intro.html#cb1-15" tabindex="-1"></a>pumpData <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">14</span>, <span class="dv">3</span>, <span class="dv">19</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">22</span>))</span>
<span id="cb1-16"><a href="cha-lightning-intro.html#cb1-16" tabindex="-1"></a></span>
<span id="cb1-17"><a href="cha-lightning-intro.html#cb1-17" tabindex="-1"></a>pumpInits <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">alpha =</span> <span class="dv">1</span>, <span class="at">beta =</span> <span class="dv">1</span>,</span>
<span id="cb1-18"><a href="cha-lightning-intro.html#cb1-18" tabindex="-1"></a> <span class="at">theta =</span> <span class="fu">rep</span>(<span class="fl">0.1</span>, pumpConsts<span class="sc">$</span>N))</span></code></pre></div>
<p>Here <code>x[i]</code> is the number of failures recorded during a time
duration of length <code>t[i]</code> for the <code>i</code><span class="math inline">\(^{th}\)</span> pump.
<code>theta[i]</code> is a failure rate, and the goal is estimate parameters
<code>alpha</code> and <code>beta</code>. Now let’s create the model and look at some of its nodes.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="cha-lightning-intro.html#cb2-1" tabindex="-1"></a>pump <span class="ot"><-</span> <span class="fu">nimbleModel</span>(<span class="at">code =</span> pumpCode, <span class="at">name =</span> <span class="st">"pump"</span>, <span class="at">constants =</span> pumpConsts,</span>
<span id="cb2-2"><a href="cha-lightning-intro.html#cb2-2" tabindex="-1"></a> <span class="at">data =</span> pumpData, <span class="at">inits =</span> pumpInits)</span>
<span id="cb2-3"><a href="cha-lightning-intro.html#cb2-3" tabindex="-1"></a></span>
<span id="cb2-4"><a href="cha-lightning-intro.html#cb2-4" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">getNodeNames</span>()</span></code></pre></div>
<pre><code>## [1] "alpha" "beta" "lifted_d1_over_beta"
## [4] "theta[1]" "theta[2]" "theta[3]"
## [7] "theta[4]" "theta[5]" "theta[6]"
## [10] "theta[7]" "theta[8]" "theta[9]"
## [13] "theta[10]" "lambda[1]" "lambda[2]"
## [16] "lambda[3]" "lambda[4]" "lambda[5]"
## [19] "lambda[6]" "lambda[7]" "lambda[8]"
## [22] "lambda[9]" "lambda[10]" "x[1]"
## [25] "x[2]" "x[3]" "x[4]"
## [28] "x[5]" "x[6]" "x[7]"
## [31] "x[8]" "x[9]" "x[10]"</code></pre>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="cha-lightning-intro.html#cb4-1" tabindex="-1"></a>pump<span class="sc">$</span>x</span></code></pre></div>
<pre><code>## [1] 5 1 5 14 3 19 1 1 4 22</code></pre>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="cha-lightning-intro.html#cb6-1" tabindex="-1"></a>pump<span class="sc">$</span>logProb_x</span></code></pre></div>
<pre><code>## [1] -2.998011 -1.118924 -1.882686 -2.319466 -4.254550 -20.739651
## [7] -2.358795 -2.358795 -9.630645 -48.447798</code></pre>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="cha-lightning-intro.html#cb8-1" tabindex="-1"></a>pump<span class="sc">$</span>alpha</span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="cha-lightning-intro.html#cb10-1" tabindex="-1"></a>pump<span class="sc">$</span>theta</span></code></pre></div>
<pre><code>## [1] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1</code></pre>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="cha-lightning-intro.html#cb12-1" tabindex="-1"></a>pump<span class="sc">$</span>lambda</span></code></pre></div>
<pre><code>## [1] 9.430 1.570 6.290 12.600 0.524 3.140 0.105 0.105 0.210 1.050</code></pre>
<p>Notice that in the list of nodes, NIMBLE has introduced a new node,
<code>lifted_d1_over_beta</code>. We call this a ‘lifted’ node. Like R,
NIMBLE allows alternative parameterizations, such as the scale or rate
parameterization of the gamma distribution. Choice of parameterization
can generate a lifted node, as can using a link function or a
distribution argument that is an expression. It’s helpful to know why
they exist, but you shouldn’t need to worry about them.</p>
<p>Thanks to the plotting capabilities of the <code>igraph</code> package that
NIMBLE uses to represent the directed acyclic graph, we can plot the
model (Figure 2.1).</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="cha-lightning-intro.html#cb14-1" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">plotGraph</span>()</span></code></pre></div>
<div class="figure"><span style="display:block;" id="fig:plotPump"></span>
<img src="NimbleUserManual_files/figure-html/plotPump-1.png" alt="Directed Acyclic Graph plot of the pump model, thanks to the igraph package" width="720" />
<p class="caption">
Figure 2.1: Directed Acyclic Graph plot of the pump model, thanks to the igraph package
</p>
</div>
<p>You are in control of the model. By default, <code>nimbleModel</code> does
its best to initialize a model, but let’s say you want to
re-initialize <code>theta</code>. To simulate from the prior for <code>theta</code> (overwriting the
initial values previously in the model) we first need to be sure the
parent nodes of all <code>theta[i]</code> nodes are fully initialized, including any non-stochastic nodes such
as lifted nodes. We then use the <code>simulate</code> function to simulate
from the distribution for <code>theta</code>. Finally we use the
<code>calculate</code> function to
calculate the dependencies of <code>theta</code>, namely <code>lambda</code> and the
log probabilities of <code>x</code> to ensure all parts of the
model are up to date. First we show how
to use the model’s <code>getDependencies</code> method to query information
about its graph.
<!--- TODO: the logic here is a bit weird - we say we want to know all parents of theta are initialized by our code actually finds dependencies of alpha+beta not parents of theta --></p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="cha-lightning-intro.html#cb15-1" tabindex="-1"></a><span class="co"># Show all dependencies of alpha and beta terminating in stochastic nodes</span></span>
<span id="cb15-2"><a href="cha-lightning-intro.html#cb15-2" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">getDependencies</span>(<span class="fu">c</span>(<span class="st">"alpha"</span>, <span class="st">"beta"</span>))</span></code></pre></div>
<pre><code>## [1] "alpha" "beta" "lifted_d1_over_beta"
## [4] "theta[1]" "theta[2]" "theta[3]"
## [7] "theta[4]" "theta[5]" "theta[6]"
## [10] "theta[7]" "theta[8]" "theta[9]"
## [13] "theta[10]"</code></pre>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="cha-lightning-intro.html#cb17-1" tabindex="-1"></a><span class="co"># Now show only the deterministic dependencies</span></span>
<span id="cb17-2"><a href="cha-lightning-intro.html#cb17-2" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">getDependencies</span>(<span class="fu">c</span>(<span class="st">"alpha"</span>, <span class="st">"beta"</span>), <span class="at">determOnly =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<pre><code>## [1] "lifted_d1_over_beta"</code></pre>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="cha-lightning-intro.html#cb19-1" tabindex="-1"></a><span class="co"># Check that the lifted node was initialized. </span></span>
<span id="cb19-2"><a href="cha-lightning-intro.html#cb19-2" tabindex="-1"></a>pump[[<span class="st">"lifted_d1_over_beta"</span>]] <span class="co"># It was.</span></span></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="cha-lightning-intro.html#cb21-1" tabindex="-1"></a><span class="co"># Now let's simulate new theta values</span></span>
<span id="cb21-2"><a href="cha-lightning-intro.html#cb21-2" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>) <span class="co"># This makes the simulations here reproducible</span></span>
<span id="cb21-3"><a href="cha-lightning-intro.html#cb21-3" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">simulate</span>(<span class="st">"theta"</span>)</span>
<span id="cb21-4"><a href="cha-lightning-intro.html#cb21-4" tabindex="-1"></a>pump<span class="sc">$</span>theta <span class="co"># the new theta values</span></span></code></pre></div>
<pre><code>## [1] 0.15514136 1.88240160 1.80451250 0.83617765 1.22254365 1.15835525
## [7] 0.99001994 0.30737332 0.09461909 0.15720154</code></pre>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="cha-lightning-intro.html#cb23-1" tabindex="-1"></a><span class="co"># lambda and logProb_x haven't been re-calculated yet</span></span>
<span id="cb23-2"><a href="cha-lightning-intro.html#cb23-2" tabindex="-1"></a>pump<span class="sc">$</span>lambda <span class="co"># these are the same values as above</span></span></code></pre></div>
<pre><code>## [1] 9.430 1.570 6.290 12.600 0.524 3.140 0.105 0.105 0.210 1.050</code></pre>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="cha-lightning-intro.html#cb25-1" tabindex="-1"></a>pump<span class="sc">$</span>logProb_x</span></code></pre></div>
<pre><code>## [1] -2.998011 -1.118924 -1.882686 -2.319466 -4.254550 -20.739651
## [7] -2.358795 -2.358795 -9.630645 -48.447798</code></pre>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="cha-lightning-intro.html#cb27-1" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">getLogProb</span>(<span class="st">"x"</span>) <span class="co"># The sum of logProb_x</span></span></code></pre></div>
<pre><code>## [1] -96.10932</code></pre>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="cha-lightning-intro.html#cb29-1" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">calculate</span>(pump<span class="sc">$</span><span class="fu">getDependencies</span>(<span class="fu">c</span>(<span class="st">"theta"</span>)))</span></code></pre></div>
<pre><code>## [1] -262.204</code></pre>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="cha-lightning-intro.html#cb31-1" tabindex="-1"></a>pump<span class="sc">$</span>lambda <span class="co"># Now they have.</span></span></code></pre></div>
<pre><code>## [1] 14.6298299 29.5537051 113.5038360 105.3583839 6.4061287 36.3723548
## [7] 1.0395209 0.3227420 0.1987001 1.6506161</code></pre>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="cha-lightning-intro.html#cb33-1" tabindex="-1"></a>pump<span class="sc">$</span>logProb_x</span></code></pre></div>
<pre><code>## [1] -6.002009 -26.167496 -94.632145 -65.346457 -2.626123 -7.429868
## [7] -1.000761 -1.453644 -9.840589 -39.096527</code></pre>
<p>Notice that the first <code>getDependencies</code> call returned dependencies
from <code>alpha</code> and <code>beta</code> down to the next stochastic nodes in the
model. The second call requested only deterministic dependencies.
The call to <code>pump$simulate("theta")</code>
expands <code>"theta"</code> to include all nodes in <code>theta</code>. After
simulating into <code>theta</code>, we can see that <code>lambda</code> and the log
probabilities of <code>x</code> still reflect the old values of <code>theta</code>, so
we <code>calculate</code> them and then see that they have been updated.</p>
</div>
<div id="sec:compiling-model" class="section level2 hasAnchor" number="2.3">
<h2><span class="header-section-number">2.3</span> Compiling the model<a href="cha-lightning-intro.html#sec:compiling-model" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Next we compile the model, which means generating C++ code, compiling
that code, and loading it back into R with an object that can be used just
like the uncompiled model. The values in the compiled model will be
initialized from those of the original model in R, but
the original and compiled models are distinct objects so any
subsequent changes in one will not be reflected in the other.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="cha-lightning-intro.html#cb35-1" tabindex="-1"></a>Cpump <span class="ot"><-</span> <span class="fu">compileNimble</span>(pump)</span>
<span id="cb35-2"><a href="cha-lightning-intro.html#cb35-2" tabindex="-1"></a>Cpump<span class="sc">$</span>theta</span></code></pre></div>
<pre><code>## [1] 0.15514136 1.88240160 1.80451250 0.83617765 1.22254365 1.15835525
## [7] 0.99001994 0.30737332 0.09461909 0.15720154</code></pre>
<p>Note that the compiled model is used when running any NIMBLE algorithms via C++, so the model needs to be compiled before (or at the same time as) any compilation of algorithms, such as the compilation of the MCMC done in the next section.</p>
</div>
<div id="sec:intro-runMCMC" class="section level2 hasAnchor" number="2.4">
<h2><span class="header-section-number">2.4</span> One-line invocation of MCMC<a href="cha-lightning-intro.html#sec:intro-runMCMC" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>The most direct approach to invoking NIMBLE’s MCMC engine is using the
<code>nimbleMCMC</code> function. This function would generally take the code,
data, constants, and initial values as input, but it can also accept the (compiled or uncompiled)
model object as an argument. It provides a variety of options for executing and
controlling multiple chains of NIMBLE’s default MCMC algorithm, and
returning posterior samples, posterior summary statistics,
and/or WAIC values.</p>
<p>For example, to execute two MCMC chains of 10,000 samples each, and
return samples, summary statistics, and WAIC values:</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="cha-lightning-intro.html#cb37-1" tabindex="-1"></a>mcmc.out <span class="ot"><-</span> <span class="fu">nimbleMCMC</span>(<span class="at">code =</span> pumpCode, <span class="at">constants =</span> pumpConsts,</span>
<span id="cb37-2"><a href="cha-lightning-intro.html#cb37-2" tabindex="-1"></a> <span class="at">data =</span> pumpData, <span class="at">inits =</span> pumpInits,</span>
<span id="cb37-3"><a href="cha-lightning-intro.html#cb37-3" tabindex="-1"></a> <span class="at">nchains =</span> <span class="dv">2</span>, <span class="at">niter =</span> <span class="dv">10000</span>,</span>
<span id="cb37-4"><a href="cha-lightning-intro.html#cb37-4" tabindex="-1"></a> <span class="at">summary =</span> <span class="cn">TRUE</span>, <span class="at">WAIC =</span> <span class="cn">TRUE</span>,</span>
<span id="cb37-5"><a href="cha-lightning-intro.html#cb37-5" tabindex="-1"></a> <span class="at">monitors =</span> <span class="fu">c</span>(<span class="st">'alpha'</span>,<span class="st">'beta'</span>,<span class="st">'theta'</span>))</span></code></pre></div>
<pre><code>## [Warning] There are 7 individual pWAIC values that are greater than 0.4. This may indicate that the WAIC estimate is unstable (Vehtari et al., 2017), at least in cases without grouping of data nodes or multivariate data nodes.</code></pre>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="cha-lightning-intro.html#cb39-1" tabindex="-1"></a><span class="fu">names</span>(mcmc.out)</span></code></pre></div>
<pre><code>## [1] "samples" "summary" "WAIC"</code></pre>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="cha-lightning-intro.html#cb41-1" tabindex="-1"></a>mcmc.out<span class="sc">$</span>summary</span></code></pre></div>
<pre><code>## $chain1
## Mean Median St.Dev. 95%CI_low 95%CI_upp
## alpha 0.69804352 0.65835063 0.27037676 0.287898244 1.3140461
## beta 0.92862598 0.82156847 0.54969128 0.183699137 2.2872696
## theta[1] 0.06019274 0.05676327 0.02544956 0.021069950 0.1199230
## theta[2] 0.10157737 0.08203988 0.07905076 0.008066869 0.3034085
## theta[3] 0.08874755 0.08396502 0.03760562 0.031186960 0.1769982
## theta[4] 0.11567784 0.11301465 0.03012598 0.064170937 0.1824525
## theta[5] 0.60382223 0.54935089 0.31219612 0.159731108 1.3640771
## theta[6] 0.61204831 0.60085518 0.13803302 0.372712375 0.9135269
## theta[7] 0.90263434 0.70803389 0.73960182 0.074122175 2.7598261
## theta[8] 0.89021051 0.70774794 0.72668155 0.072571029 2.8189252
## theta[9] 1.57678136 1.44390008 0.76825189 0.455195149 3.4297368
## theta[10] 1.98954127 1.96171250 0.42409802 1.241383787 2.9012192
##
## $chain2
## Mean Median St.Dev. 95%CI_low 95%CI_upp
## alpha 0.69101961 0.65803654 0.26548378 0.277195564 1.2858148
## beta 0.91627273 0.81434426 0.53750825 0.185772263 2.2702428
## theta[1] 0.05937364 0.05611283 0.02461866 0.020956151 0.1161870
## theta[2] 0.10017726 0.08116259 0.07855024 0.008266343 0.3010355
## theta[3] 0.08908126 0.08390782 0.03704170 0.031330829 0.1736876
## theta[4] 0.11592652 0.11356920 0.03064645 0.063595333 0.1829574
## theta[5] 0.59755632 0.54329373 0.31871551 0.149286703 1.3748728
## theta[6] 0.61080189 0.59946693 0.13804343 0.371373877 0.9097319
## theta[7] 0.89902759 0.70901502 0.72930369 0.076243503 2.7441445
## theta[8] 0.89954594 0.70727079 0.73345905 0.071250926 2.8054633
## theta[9] 1.57530029 1.45005738 0.75242164 0.469959364 3.3502795
## theta[10] 1.98911473 1.96227061 0.42298189 1.246910723 2.9102326
##
## $all.chains
## Mean Median St.Dev. 95%CI_low 95%CI_upp
## alpha 0.69453156 0.65803654 0.26795776 0.28329854 1.2999319
## beta 0.92244935 0.81828160 0.54365539 0.18549077 2.2785444
## theta[1] 0.05978319 0.05646474 0.02504028 0.02102807 0.1183433
## theta[2] 0.10087731 0.08162361 0.07880204 0.00811108 0.3017967
## theta[3] 0.08891440 0.08394667 0.03732417 0.03123228 0.1749967
## theta[4] 0.11580218 0.11326039 0.03038683 0.06385253 0.1827382
## theta[5] 0.60068928 0.54668011 0.31548032 0.15363752 1.3686801
## theta[6] 0.61142510 0.60015416 0.13803618 0.37203765 0.9122467
## theta[7] 0.90083096 0.70852800 0.73445465 0.07550465 2.7534885
## theta[8] 0.89487822 0.70761105 0.73007484 0.07211191 2.8067373
## theta[9] 1.57604083 1.44719278 0.76035931 0.46374515 3.3866706
## theta[10] 1.98932800 1.96195345 0.42352979 1.24334249 2.9068229</code></pre>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="cha-lightning-intro.html#cb43-1" tabindex="-1"></a>mcmc.out<span class="sc">$</span>WAIC </span></code></pre></div>
<pre><code>## nimbleList object of type waicNimbleList
## Field "WAIC":
## [1] 48.69896
## Field "lppd":
## [1] -19.99653
## Field "pWAIC":
## [1] 4.352945</code></pre>
<p>See Section <a href="cha-mcmc.html#sec:nimbleMCMC">7.1</a> or <code>help(nimbleMCMC)</code> for more
details about using <code>nimbleMCMC</code>.</p>
<p>Note that the WAIC value varies depending on what quantities are treated
as parameters. See Section <a href="cha-mcmc.html#sec:WAIC">7.8</a> or <code>help(waic)</code> for more details.</p>
</div>
<div id="sec:creating-mcmc" class="section level2 hasAnchor" number="2.5">
<h2><span class="header-section-number">2.5</span> Creating, compiling and running a basic MCMC configuration<a href="cha-lightning-intro.html#sec:creating-mcmc" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>At this point we have initial values for all of the nodes in the model,
and we have both the original and compiled versions of the model. As a first algorithm
to try on our model, let’s use NIMBLE’s default MCMC. Note that conjugate relationships are detected for all nodes except for
<code>alpha</code>, on which the default sampler is a random walk Metropolis sampler.
<!--- ^[We haven't set up conjugate relationships for an
% exponential yet.] -->
<!--- footnote is true but not relevant as there is not a conj relationship for alpha in a gamma-distributed dependency --></p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="cha-lightning-intro.html#cb45-1" tabindex="-1"></a>pumpConf <span class="ot"><-</span> <span class="fu">configureMCMC</span>(pump, <span class="at">print =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<pre><code>## ===== Monitors =====
## thin = 1: alpha, beta
## ===== Samplers =====
## RW sampler (1)
## - alpha
## conjugate sampler (11)
## - beta
## - theta[] (10 elements)</code></pre>
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="cha-lightning-intro.html#cb47-1" tabindex="-1"></a>pumpConf<span class="sc">$</span><span class="fu">addMonitors</span>(<span class="fu">c</span>(<span class="st">"alpha"</span>, <span class="st">"beta"</span>, <span class="st">"theta"</span>))</span></code></pre></div>
<pre><code>## thin = 1: alpha, beta, theta</code></pre>
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="cha-lightning-intro.html#cb49-1" tabindex="-1"></a>pumpMCMC <span class="ot"><-</span> <span class="fu">buildMCMC</span>(pumpConf)</span>
<span id="cb49-2"><a href="cha-lightning-intro.html#cb49-2" tabindex="-1"></a>CpumpMCMC <span class="ot"><-</span> <span class="fu">compileNimble</span>(pumpMCMC, <span class="at">project =</span> pump)</span>
<span id="cb49-3"><a href="cha-lightning-intro.html#cb49-3" tabindex="-1"></a></span>
<span id="cb49-4"><a href="cha-lightning-intro.html#cb49-4" tabindex="-1"></a>niter <span class="ot"><-</span> <span class="dv">1000</span></span>
<span id="cb49-5"><a href="cha-lightning-intro.html#cb49-5" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb49-6"><a href="cha-lightning-intro.html#cb49-6" tabindex="-1"></a>samples <span class="ot"><-</span> <span class="fu">runMCMC</span>(CpumpMCMC, <span class="at">niter =</span> niter)</span>
<span id="cb49-7"><a href="cha-lightning-intro.html#cb49-7" tabindex="-1"></a></span>
<span id="cb49-8"><a href="cha-lightning-intro.html#cb49-8" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">4</span>), <span class="at">mai =</span> <span class="fu">c</span>(.<span class="dv">6</span>, .<span class="dv">4</span>, .<span class="dv">1</span>, .<span class="dv">2</span>))</span>
<span id="cb49-9"><a href="cha-lightning-intro.html#cb49-9" tabindex="-1"></a><span class="fu">plot</span>(samples[ , <span class="st">"alpha"</span>], <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">xlab =</span> <span class="st">"iteration"</span>,</span>
<span id="cb49-10"><a href="cha-lightning-intro.html#cb49-10" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(alpha))</span>
<span id="cb49-11"><a href="cha-lightning-intro.html#cb49-11" tabindex="-1"></a><span class="fu">plot</span>(samples[ , <span class="st">"beta"</span>], <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">xlab =</span> <span class="st">"iteration"</span>,</span>
<span id="cb49-12"><a href="cha-lightning-intro.html#cb49-12" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(beta))</span>
<span id="cb49-13"><a href="cha-lightning-intro.html#cb49-13" tabindex="-1"></a><span class="fu">plot</span>(samples[ , <span class="st">"alpha"</span>], samples[ , <span class="st">"beta"</span>], <span class="at">xlab =</span> <span class="fu">expression</span>(alpha),</span>
<span id="cb49-14"><a href="cha-lightning-intro.html#cb49-14" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(beta))</span>
<span id="cb49-15"><a href="cha-lightning-intro.html#cb49-15" tabindex="-1"></a><span class="fu">plot</span>(samples[ , <span class="st">"theta[1]"</span>], <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">xlab =</span> <span class="st">"iteration"</span>,</span>
<span id="cb49-16"><a href="cha-lightning-intro.html#cb49-16" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(theta[<span class="dv">1</span>]))</span></code></pre></div>
<p><img src="NimbleUserManual_files/figure-html/mcmcPump-1.png" width="672" /></p>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="cha-lightning-intro.html#cb50-1" tabindex="-1"></a><span class="fu">acf</span>(samples[, <span class="st">"alpha"</span>]) <span class="co"># plot autocorrelation of alpha sample</span></span>
<span id="cb50-2"><a href="cha-lightning-intro.html#cb50-2" tabindex="-1"></a><span class="fu">acf</span>(samples[, <span class="st">"beta"</span>]) <span class="co"># plot autocorrelation of beta sample</span></span></code></pre></div>
<p><img src="NimbleUserManual_files/figure-html/mcmcPump-2.png" width="672" /></p>
<p>Notice the posterior correlation between <code>alpha</code> and <code>beta</code>.
A measure of the mixing for each is the
autocorrelation for each parameter, shown by the <code>acf</code> plots.</p>
</div>
<div id="sec:customizing-mcmc" class="section level2 hasAnchor" number="2.6">
<h2><span class="header-section-number">2.6</span> Customizing the MCMC<a href="cha-lightning-intro.html#sec:customizing-mcmc" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Let’s add an adaptive
block sampler on <code>alpha</code> and <code>beta</code> jointly and see if that
improves the mixing.</p>
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb51-1"><a href="cha-lightning-intro.html#cb51-1" tabindex="-1"></a>pumpConf<span class="sc">$</span><span class="fu">addSampler</span>(<span class="at">target =</span> <span class="fu">c</span>(<span class="st">"alpha"</span>, <span class="st">"beta"</span>), <span class="at">type =</span> <span class="st">"RW_block"</span>,</span>
<span id="cb51-2"><a href="cha-lightning-intro.html#cb51-2" tabindex="-1"></a> <span class="at">control =</span> <span class="fu">list</span>(<span class="at">adaptInterval =</span> <span class="dv">100</span>))</span>
<span id="cb51-3"><a href="cha-lightning-intro.html#cb51-3" tabindex="-1"></a> </span>
<span id="cb51-4"><a href="cha-lightning-intro.html#cb51-4" tabindex="-1"></a>pumpMCMC2 <span class="ot"><-</span> <span class="fu">buildMCMC</span>(pumpConf)</span>
<span id="cb51-5"><a href="cha-lightning-intro.html#cb51-5" tabindex="-1"></a></span>
<span id="cb51-6"><a href="cha-lightning-intro.html#cb51-6" tabindex="-1"></a><span class="co"># need to reset the nimbleFunctions in order to add the new MCMC</span></span>
<span id="cb51-7"><a href="cha-lightning-intro.html#cb51-7" tabindex="-1"></a>CpumpNewMCMC <span class="ot"><-</span> <span class="fu">compileNimble</span>(pumpMCMC2, <span class="at">project =</span> pump,</span>
<span id="cb51-8"><a href="cha-lightning-intro.html#cb51-8" tabindex="-1"></a> <span class="at">resetFunctions =</span> <span class="cn">TRUE</span>)</span>
<span id="cb51-9"><a href="cha-lightning-intro.html#cb51-9" tabindex="-1"></a></span>
<span id="cb51-10"><a href="cha-lightning-intro.html#cb51-10" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb51-11"><a href="cha-lightning-intro.html#cb51-11" tabindex="-1"></a>CpumpNewMCMC<span class="sc">$</span><span class="fu">run</span>(niter)</span>
<span id="cb51-12"><a href="cha-lightning-intro.html#cb51-12" tabindex="-1"></a>samplesNew <span class="ot"><-</span> <span class="fu">as.matrix</span>(CpumpNewMCMC<span class="sc">$</span>mvSamples)</span>
<span id="cb51-13"><a href="cha-lightning-intro.html#cb51-13" tabindex="-1"></a></span>
<span id="cb51-14"><a href="cha-lightning-intro.html#cb51-14" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mfrow =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">4</span>), <span class="at">mai =</span> <span class="fu">c</span>(.<span class="dv">6</span>, .<span class="dv">4</span>, .<span class="dv">1</span>, .<span class="dv">2</span>))</span>
<span id="cb51-15"><a href="cha-lightning-intro.html#cb51-15" tabindex="-1"></a><span class="fu">plot</span>(samplesNew[ , <span class="st">"alpha"</span>], <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">xlab =</span> <span class="st">"iteration"</span>,</span>
<span id="cb51-16"><a href="cha-lightning-intro.html#cb51-16" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(alpha))</span>
<span id="cb51-17"><a href="cha-lightning-intro.html#cb51-17" tabindex="-1"></a><span class="fu">plot</span>(samplesNew[ , <span class="st">"beta"</span>], <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">xlab =</span> <span class="st">"iteration"</span>,</span>
<span id="cb51-18"><a href="cha-lightning-intro.html#cb51-18" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(beta))</span>
<span id="cb51-19"><a href="cha-lightning-intro.html#cb51-19" tabindex="-1"></a><span class="fu">plot</span>(samplesNew[ , <span class="st">"alpha"</span>], samplesNew[ , <span class="st">"beta"</span>], <span class="at">xlab =</span> <span class="fu">expression</span>(alpha),</span>
<span id="cb51-20"><a href="cha-lightning-intro.html#cb51-20" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(beta))</span>
<span id="cb51-21"><a href="cha-lightning-intro.html#cb51-21" tabindex="-1"></a><span class="fu">plot</span>(samplesNew[ , <span class="st">"theta[1]"</span>], <span class="at">type =</span> <span class="st">"l"</span>, <span class="at">xlab =</span> <span class="st">"iteration"</span>,</span>
<span id="cb51-22"><a href="cha-lightning-intro.html#cb51-22" tabindex="-1"></a> <span class="at">ylab =</span> <span class="fu">expression</span>(theta[<span class="dv">1</span>]))</span></code></pre></div>
<p><img src="NimbleUserManual_files/figure-html/mcmcPump2-1.png" width="672" /></p>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="cha-lightning-intro.html#cb52-1" tabindex="-1"></a><span class="fu">acf</span>(samplesNew[, <span class="st">"alpha"</span>]) <span class="co"># plot autocorrelation of alpha sample</span></span>
<span id="cb52-2"><a href="cha-lightning-intro.html#cb52-2" tabindex="-1"></a><span class="fu">acf</span>(samplesNew[, <span class="st">"beta"</span>]) <span class="co"># plot autocorrelation of beta sample</span></span></code></pre></div>
<p><img src="NimbleUserManual_files/figure-html/mcmcPump2-2.png" width="672" /></p>
<p>We can see that the block sampler has decreased the
autocorrelation for both <code>alpha</code> and <code>beta</code>. Of course these
are just short runs, and what we are really interested in is the
effective sample size of the MCMC per computation time, but that’s not
the point of this example.</p>
<p>Once you learn the MCMC system, you can write your own samplers and
include them. The entire system is written in nimbleFunctions.</p>
</div>
<div id="sec:running-mcem" class="section level2 hasAnchor" number="2.7">
<h2><span class="header-section-number">2.7</span> Running MCEM<a href="cha-lightning-intro.html#sec:running-mcem" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>NIMBLE is a system for working with algorithms, not just an MCMC engine. So let’s try maximizing the marginal likelihood for <code>alpha</code> and <code>beta</code> using Monte Carlo Expectation Maximization<a href="#fn4" class="footnote-ref" id="fnref4"><sup>4</sup></a>.</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="cha-lightning-intro.html#cb53-1" tabindex="-1"></a>pump2 <span class="ot"><-</span> <span class="fu">nimbleModel</span>(<span class="at">code =</span> pumpCode, <span class="at">name =</span> <span class="st">"pump"</span>, <span class="at">constants =</span> pumpConsts,</span>
<span id="cb53-2"><a href="cha-lightning-intro.html#cb53-2" tabindex="-1"></a> <span class="at">data =</span> pumpData, <span class="at">inits =</span> pumpInits, <span class="at">buildDerivs =</span> <span class="cn">TRUE</span>)</span>
<span id="cb53-3"><a href="cha-lightning-intro.html#cb53-3" tabindex="-1"></a></span>
<span id="cb53-4"><a href="cha-lightning-intro.html#cb53-4" tabindex="-1"></a>pumpMCEM <span class="ot"><-</span> <span class="fu">buildMCEM</span>(<span class="at">model =</span> pump2, <span class="at">latentNodes =</span> <span class="st">"theta[1:10]"</span>)</span>
<span id="cb53-5"><a href="cha-lightning-intro.html#cb53-5" tabindex="-1"></a>Cpump <span class="ot"><-</span> <span class="fu">compileNimble</span>(pump2)</span>
<span id="cb53-6"><a href="cha-lightning-intro.html#cb53-6" tabindex="-1"></a>CpumpMCEM <span class="ot"><-</span> <span class="fu">compileNimble</span>(pumpMCEM, <span class="at">project =</span> pump2)</span>
<span id="cb53-7"><a href="cha-lightning-intro.html#cb53-7" tabindex="-1"></a></span>
<span id="cb53-8"><a href="cha-lightning-intro.html#cb53-8" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>)</span>
<span id="cb53-9"><a href="cha-lightning-intro.html#cb53-9" tabindex="-1"></a>pumpMLE <span class="ot"><-</span> CpumpMCEM<span class="sc">$</span><span class="fu">findMLE</span>()</span></code></pre></div>
<pre><code>## [Note] Iteration Number: 1.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.810166
## 1.11657
## [Note] Convergence Criterion: 0.655578.
## [Note] Iteration Number: 2.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.807775
## 1.19848
## [Note] Convergence Criterion: 0.0288968.
## [Note] Iteration Number: 3.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.801477
## 1.20974
## [Note] Convergence Criterion: 0.00314139.
## [Note] Iteration Number: 4.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.814074
## 1.25737
## [Note] Convergence Criterion: 0.00612404.
## [Note] Iteration Number: 5.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.823345
## 1.26626
## [Note] Convergence Criterion: 0.00132764.
## [Note] Iteration Number: 6.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.833545
## 1.29405
## [Note] Convergence Criterion: 0.00251191.
## [Note] Iteration Number: 7.
## [Note] Current number of MCMC iterations: 1000.
## [Note] Parameter Estimates:
## 0.81979
## 1.25567
## [Note] Convergence Criterion: 0.00414933.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## Monte Carlo error too big: increasing MCMC sample size.
## [Note] Iteration Number: 8.
## [Note] Current number of MCMC iterations: 9400.
## [Note] Parameter Estimates:
## 0.823025
## 1.26278
## [Note] Convergence Criterion: 0.000165076.</code></pre>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb55-1"><a href="cha-lightning-intro.html#cb55-1" tabindex="-1"></a>pumpMLE<span class="sc">$</span>par</span></code></pre></div>
<pre><code>## [1] 0.8230248 1.2627842</code></pre>
<p>Both estimates are within 0.01 of the values reported by
<span class="citation">George, Makov, and Smith (<a href="references.html#ref-George_Makov_Smith_1993">1993</a>)</span><a href="#fn5" class="footnote-ref" id="fnref5"><sup>5</sup></a>.
<!---
[George, E.I., Makov, U.E. \& Smith,
A.F.M. 1993. Conjugate likelihood
distributions. *Scand. J. Statist.* \textbf{20]:147-156.
-->
<!--- Their numbers were accidentally swapped in Table 2.}. -->
Some discrepancy is to be expected since it is a Monte Carlo algorithm.</p>
</div>
<div id="sec:creating-your-own" class="section level2 hasAnchor" number="2.8">
<h2><span class="header-section-number">2.8</span> Creating your own functions<a href="cha-lightning-intro.html#sec:creating-your-own" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Now let’s see an example of writing our own algorithm and using it on
the model. We’ll do something simple: simulating multiple values for a
designated set of nodes and calculating every part of the model that
depends on them. More details on programming in NIMBLE are in Part IV.</p>
<p>Here is our <em>nimbleFunction</em>:</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb57-1"><a href="cha-lightning-intro.html#cb57-1" tabindex="-1"></a>simNodesMany <span class="ot"><-</span> <span class="fu">nimbleFunction</span>(</span>
<span id="cb57-2"><a href="cha-lightning-intro.html#cb57-2" tabindex="-1"></a> <span class="at">setup =</span> <span class="cf">function</span>(model, nodes) {</span>
<span id="cb57-3"><a href="cha-lightning-intro.html#cb57-3" tabindex="-1"></a> mv <span class="ot"><-</span> <span class="fu">modelValues</span>(model)</span>
<span id="cb57-4"><a href="cha-lightning-intro.html#cb57-4" tabindex="-1"></a> deps <span class="ot"><-</span> model<span class="sc">$</span><span class="fu">getDependencies</span>(nodes)</span>
<span id="cb57-5"><a href="cha-lightning-intro.html#cb57-5" tabindex="-1"></a> allNodes <span class="ot"><-</span> model<span class="sc">$</span><span class="fu">getNodeNames</span>()</span>
<span id="cb57-6"><a href="cha-lightning-intro.html#cb57-6" tabindex="-1"></a> },</span>
<span id="cb57-7"><a href="cha-lightning-intro.html#cb57-7" tabindex="-1"></a> <span class="at">run =</span> <span class="cf">function</span>(<span class="at">n =</span> <span class="fu">integer</span>()) {</span>
<span id="cb57-8"><a href="cha-lightning-intro.html#cb57-8" tabindex="-1"></a> <span class="fu">resize</span>(mv, n)</span>
<span id="cb57-9"><a href="cha-lightning-intro.html#cb57-9" 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="cb57-10"><a href="cha-lightning-intro.html#cb57-10" tabindex="-1"></a> model<span class="sc">$</span><span class="fu">simulate</span>(nodes)</span>
<span id="cb57-11"><a href="cha-lightning-intro.html#cb57-11" tabindex="-1"></a> model<span class="sc">$</span><span class="fu">calculate</span>(deps)</span>
<span id="cb57-12"><a href="cha-lightning-intro.html#cb57-12" tabindex="-1"></a> <span class="fu">copy</span>(<span class="at">from =</span> model, <span class="at">nodes =</span> allNodes,</span>
<span id="cb57-13"><a href="cha-lightning-intro.html#cb57-13" tabindex="-1"></a> <span class="at">to =</span> mv, <span class="at">rowTo =</span> i, <span class="at">logProb =</span> <span class="cn">TRUE</span>)</span>
<span id="cb57-14"><a href="cha-lightning-intro.html#cb57-14" tabindex="-1"></a> }</span>
<span id="cb57-15"><a href="cha-lightning-intro.html#cb57-15" tabindex="-1"></a> })</span>
<span id="cb57-16"><a href="cha-lightning-intro.html#cb57-16" tabindex="-1"></a></span>
<span id="cb57-17"><a href="cha-lightning-intro.html#cb57-17" tabindex="-1"></a>simNodesTheta1to5 <span class="ot"><-</span> <span class="fu">simNodesMany</span>(pump, <span class="st">"theta[1:5]"</span>)</span>
<span id="cb57-18"><a href="cha-lightning-intro.html#cb57-18" tabindex="-1"></a>simNodesTheta6to10 <span class="ot"><-</span> <span class="fu">simNodesMany</span>(pump, <span class="st">"theta[6:10]"</span>)</span></code></pre></div>
<p>Here are a few things to notice about the nimbleFunction.</p>
<ol style="list-style-type: decimal">
<li>The <code>setup</code> function is written in R. It creates relevant
information specific to our model for use in the run-time code.<br />
</li>
<li>The <code>setup</code> code creates a <em>modelValues</em> object to hold multiple sets of
values for variables in the model provided.</li>
<li>The <code>run</code> function is written in NIMBLE. It carries out the
calculations using the information determined once for each set of
<code>model</code> and <code>nodes</code> arguments by the setup
code. The run-time code is what will be compiled.</li>
<li>The <code>run</code> code requires type information about the argument
<code>n</code>. In this case it is a scalar integer.<br />
</li>
<li>The for-loop looks just like R, but only sequential integer
iteration is allowed.</li>
<li>The functions <code>calculate</code> and <code>simulate</code>, which were
introduced above in R, can be used in NIMBLE.</li>
<li>The special function <code>copy</code> is used here to record values
from the model into the modelValues object.<br />
</li>
<li>Multiple instances, or ‘specializations’, can be made by
calling <code>simNodesMany</code> with different arguments. Above, <code>simNodesTheta1to5</code> has
been made by calling <code>simNodesMany</code> with the <code>pump</code> model and
nodes <code>"theta[1:5]"</code> as inputs to
the <code>setup</code> function, while <code>simNodesTheta6to10</code> differs by
providing <code>"theta[6:10]"</code> as an argument. The returned objects
are objects of a uniquely
generated R reference class with fields (member data) for the results of the
<code>setup</code> code and a <code>run</code> method (member function).
<!--- Arbitrary other methods can be provided with a `methods` argument, following the syntax of R's `setRefClass` function. -->
<!--- % NOTE: CJP removed previous sentence as I think it is too involved for the lightning intro - CJP --></li>
</ol>
<p>By the way, <code>simNodesMany</code> is very similar to a standard
<code>nimbleFunction</code> provided with NIMBLE, <code>simNodesMV</code>.</p>
<p>Now let’s execute this nimbleFunction in R, before compiling it.</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="cha-lightning-intro.html#cb58-1" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1</span>) <span class="co"># make the calculation repeatable</span></span>
<span id="cb58-2"><a href="cha-lightning-intro.html#cb58-2" tabindex="-1"></a>pump<span class="sc">$</span>alpha <span class="ot"><-</span> pumpMLE<span class="sc">$</span>par[<span class="dv">1</span>]</span>
<span id="cb58-3"><a href="cha-lightning-intro.html#cb58-3" tabindex="-1"></a>pump<span class="sc">$</span>beta <span class="ot"><-</span> pumpMLE<span class="sc">$</span>par[<span class="dv">2</span>]</span>
<span id="cb58-4"><a href="cha-lightning-intro.html#cb58-4" tabindex="-1"></a><span class="co"># make sure to update deterministic dependencies of the altered nodes</span></span>
<span id="cb58-5"><a href="cha-lightning-intro.html#cb58-5" tabindex="-1"></a>pump<span class="sc">$</span><span class="fu">calculate</span>(pump<span class="sc">$</span><span class="fu">getDependencies</span>(<span class="fu">c</span>(<span class="st">"alpha"</span>,<span class="st">"beta"</span>), <span class="at">determOnly =</span> <span class="cn">TRUE</span>))</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="cha-lightning-intro.html#cb60-1" tabindex="-1"></a>saveTheta <span class="ot"><-</span> pump<span class="sc">$</span>theta</span>
<span id="cb60-2"><a href="cha-lightning-intro.html#cb60-2" tabindex="-1"></a>simNodesTheta1to5<span class="sc">$</span><span class="fu">run</span>(<span class="dv">10</span>)</span>
<span id="cb60-3"><a href="cha-lightning-intro.html#cb60-3" tabindex="-1"></a>simNodesTheta1to5<span class="sc">$</span>mv[[<span class="st">"theta"</span>]][<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]</span></code></pre></div>
<pre><code>## [[1]]
## [1] 0.21801104 1.92849314 0.62185677 0.34145435 3.45061289 1.15835525
## [7] 0.99001994 0.30737332 0.09461909 0.15720154
##
## [[2]]
## [1] 0.82620401 0.08776004 0.34362701 0.29479110 0.14167506 1.15835525
## [7] 0.99001994 0.30737332 0.09461909 0.15720154</code></pre>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="cha-lightning-intro.html#cb62-1" tabindex="-1"></a>simNodesTheta1to5<span class="sc">$</span>mv[[<span class="st">"logProb_x"</span>]][<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>]</span></code></pre></div>
<pre><code>## [[1]]
## [1] -10.229575 -26.866943 -25.569779 -15.550101 -11.188351 -7.429868
## [7] -1.000761 -1.453644 -9.840589 -39.096527
##
## [[2]]
## [1] -60.920692 -1.057321 -11.034892 -11.727789 -3.427830 -7.429868
## [7] -1.000761 -1.453644 -9.840589 -39.096527</code></pre>
<p>In this code we have initialized the values of <code>alpha</code> and <code>beta</code>
to their MLE and then recorded the <code>theta</code> values to use below. Then we
have requested 10 simulations from
<code>simNodesTheta1to5</code>. Shown are the first two simulation results
for <code>theta</code> and the log probabilities of <code>x</code>. Notice that
<code>theta[6:10]</code> and the corresponding log probabilities for <code>x[6:10]</code> are unchanged because the nodes being simulated are only
<code>theta[1:5]</code>. In R, this function runs slowly.</p>
<p>Finally, let’s compile the function and run that version.</p>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="cha-lightning-intro.html#cb64-1" tabindex="-1"></a>CsimNodesTheta1to5 <span class="ot"><-</span> <span class="fu">compileNimble</span>(simNodesTheta1to5,</span>
<span id="cb64-2"><a href="cha-lightning-intro.html#cb64-2" tabindex="-1"></a> <span class="at">project =</span> pump, <span class="at">resetFunctions =</span> <span class="cn">TRUE</span>)</span>
<span id="cb64-3"><a href="cha-lightning-intro.html#cb64-3" tabindex="-1"></a>Cpump<span class="sc">$</span>alpha <span class="ot"><-</span> pumpMLE<span class="sc">$</span>par[<span class="dv">1</span>]</span>
<span id="cb64-4"><a href="cha-lightning-intro.html#cb64-4" tabindex="-1"></a>Cpump<span class="sc">$</span>beta <span class="ot"><-</span> pumpMLE<span class="sc">$</span>par[<span class="dv">2</span>]</span>
<span id="cb64-5"><a href="cha-lightning-intro.html#cb64-5" tabindex="-1"></a>Cpump<span class="sc">$</span><span class="fu">calculate</span>(Cpump<span class="sc">$</span><span class="fu">getDependencies</span>(<span class="fu">c</span>(<span class="st">"alpha"</span>,<span class="st">"beta"</span>), <span class="at">determOnly =</span> <span class="cn">TRUE</span>))</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="cha-lightning-intro.html#cb66-1" tabindex="-1"></a>Cpump<span class="sc">$</span>theta <span class="ot"><-</span> saveTheta</span>