-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpymc-usage.html
1720 lines (1580 loc) · 68.3 KB
/
pymc-usage.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 xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.6.41">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>pymc-usage – Nutpie</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
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; }
div.sourceCode { margin: 1em 0; }
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;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script><script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting-48ffa3e5b9d089919c6712c39e5b00f2.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap-e212f1f3c46809d555a1335ea96b499f.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "end",
"type": "overlay",
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==" crossorigin="anonymous"></script>
<script type="application/javascript">define('jquery', [],function() {return window.jQuery;})</script>
<link rel="stylesheet" href="styles.css">
</head>
<body class="nav-fixed">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg " data-bs-theme="dark">
<div class="navbar-container container-fluid">
<div class="navbar-brand-container mx-auto">
<a class="navbar-brand" href="./index.html">
<span class="navbar-title">Nutpie</span>
</a>
</div>
<div id="quarto-search" class="" title="Search"></div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" role="menu" aria-expanded="false" aria-label="Toggle navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav navbar-nav-scroll me-auto">
<li class="nav-item">
<a class="nav-link" href="./index.html">
<span class="menu-text">Home</span></a>
</li>
<li class="nav-item">
<a class="nav-link active" href="./pymc-usage.html" aria-current="page">
<span class="menu-text">Usage with PyMC</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./stan-usage.html">
<span class="menu-text">Usage with Stan</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./sampling-options.html">
<span class="menu-text">Sampling Options</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./nf-adapt.html">
<span class="menu-text">Normalizing flow adaptation</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./sample-stats.html">
<span class="menu-text">Diagnostic information</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./about.html">
<span class="menu-text">About</span></a>
</li>
</ul>
</div> <!-- /navcollapse -->
<div class="quarto-navbar-tools">
<a href="https://github.com/pymc-devs/nutpie" title="" class="quarto-navigation-tool px-1" aria-label=""><i class="bi bi-github"></i></a>
</div>
</div> <!-- /container-fluid -->
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">On this page</h2>
<ul>
<li><a href="#installation" id="toc-installation" class="nav-link active" data-scroll-target="#installation">Installation</a>
<ul class="collapse">
<li><a href="#using-vscode" id="toc-using-vscode" class="nav-link" data-scroll-target="#using-vscode">Using VSCode</a></li>
<li><a href="#using-jupyterlab" id="toc-using-jupyterlab" class="nav-link" data-scroll-target="#using-jupyterlab">Using JupyterLab</a></li>
</ul></li>
<li><a href="#defining-and-sampling-a-simple-model" id="toc-defining-and-sampling-a-simple-model" class="nav-link" data-scroll-target="#defining-and-sampling-a-simple-model">Defining and Sampling a Simple Model</a>
<ul class="collapse">
<li><a href="#model-definition" id="toc-model-definition" class="nav-link" data-scroll-target="#model-definition">Model Definition</a></li>
<li><a href="#sampling" id="toc-sampling" class="nav-link" data-scroll-target="#sampling">Sampling</a></li>
<li><a href="#choosing-the-backend" id="toc-choosing-the-backend" class="nav-link" data-scroll-target="#choosing-the-backend">Choosing the backend</a></li>
<li><a href="#changing-the-dataset-without-recompilation" id="toc-changing-the-dataset-without-recompilation" class="nav-link" data-scroll-target="#changing-the-dataset-without-recompilation">Changing the dataset without recompilation</a></li>
</ul></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Usage with PyMC models</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>This document shows how to use <code>nutpie</code> with PyMC models. We will use the <code>pymc</code> package to define a simple model and sample from it using <code>nutpie</code>.</p>
<section id="installation" class="level2">
<h2 class="anchored" data-anchor-id="installation">Installation</h2>
<p>The recommended way to install <code>pymc</code> is through the <code>conda</code> ecosystem. A good package manager for conda packages is <code>pixi</code>. See for the <a href="https://pixi.sh">pixi documentation</a> for instructions on how to install it.</p>
<p>We create a new project for this example:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">pixi</span> new pymc-example</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>This will create a new directory <code>pymc-example</code> with a <code>pixi.toml</code> file, that you can edit to add meta information.</p>
<p>We then add the <code>pymc</code> and <code>nutpie</code> packages to the project:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">cd</span> pymc-example</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="ex">pixi</span> add pymc nutpie arviz</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>You can use Visual Studio Code (VSCode) or JupyterLab to write and run our code. Both are excellent tools for working with Python and data science projects.</p>
<section id="using-vscode" class="level3">
<h3 class="anchored" data-anchor-id="using-vscode">Using VSCode</h3>
<ol type="1">
<li>Open VSCode.</li>
<li>Open the <code>pymc-example</code> directory created earlier.</li>
<li>Create a new file named <code>model.ipynb</code>.</li>
<li>Select the pixi kernel to run the code.</li>
</ol>
</section>
<section id="using-jupyterlab" class="level3">
<h3 class="anchored" data-anchor-id="using-jupyterlab">Using JupyterLab</h3>
<ol type="1">
<li>Add jupyter labs to the project by running <code>pixi add jupyterlab</code>.</li>
<li>Open JupyterLab by running <code>pixi run jupyter lab</code> in your terminal.</li>
<li>Create a new Python notebook.</li>
</ol>
</section>
</section>
<section id="defining-and-sampling-a-simple-model" class="level2">
<h2 class="anchored" data-anchor-id="defining-and-sampling-a-simple-model">Defining and Sampling a Simple Model</h2>
<p>We will define a simple Bayesian model using <code>pymc</code> and sample from it using <code>nutpie</code>.</p>
<section id="model-definition" class="level3">
<h3 class="anchored" data-anchor-id="model-definition">Model Definition</h3>
<p>In your <code>model.ipypy</code> file or Jupyter notebook, add the following code:</p>
<div id="dbca6234" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pymc <span class="im">as</span> pm</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> nutpie</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>coords <span class="op">=</span> {<span class="st">"observation"</span>: <span class="bu">range</span>(<span class="dv">3</span>)}</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> pm.Model(coords<span class="op">=</span>coords) <span class="im">as</span> model:</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># Prior distributions for the intercept and slope</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> intercept <span class="op">=</span> pm.Normal(<span class="st">"intercept"</span>, mu<span class="op">=</span><span class="dv">0</span>, sigma<span class="op">=</span><span class="dv">1</span>)</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> slope <span class="op">=</span> pm.Normal(<span class="st">"slope"</span>, mu<span class="op">=</span><span class="dv">0</span>, sigma<span class="op">=</span><span class="dv">1</span>)</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># Likelihood (sampling distribution) of observations</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a> mu <span class="op">=</span> intercept <span class="op">+</span> slope <span class="op">*</span> x</span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a> y <span class="op">=</span> pm.Normal(<span class="st">"y"</span>, mu<span class="op">=</span>mu, sigma<span class="op">=</span><span class="fl">0.1</span>, observed<span class="op">=</span>[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>], dims<span class="op">=</span><span class="st">"observation"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="sampling" class="level3">
<h3 class="anchored" data-anchor-id="sampling">Sampling</h3>
<p>We can now compile the model using the numba backend:</p>
<div id="74540a7e" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>compiled <span class="op">=</span> nutpie.compile_pymc_model(model)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>trace <span class="op">=</span> nutpie.sample(compiled)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<style>
:root {
--column-width-1: 40%; /* Progress column width */
--column-width-2: 15%; /* Chain column width */
--column-width-3: 15%; /* Divergences column width */
--column-width-4: 15%; /* Step Size column width */
--column-width-5: 15%; /* Gradients/Draw column width */
}
.nutpie {
max-width: 800px;
margin: 10px auto;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
//color: #333;
//background-color: #fff;
padding: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
border-radius: 8px;
font-size: 14px; /* Smaller font size for a more compact look */
}
.nutpie table {
width: 100%;
border-collapse: collapse; /* Remove any extra space between borders */
}
.nutpie th, .nutpie td {
padding: 8px 10px; /* Reduce padding to make table more compact */
text-align: left;
border-bottom: 1px solid #888;
}
.nutpie th {
//background-color: #f0f0f0;
}
.nutpie th:nth-child(1) { width: var(--column-width-1); }
.nutpie th:nth-child(2) { width: var(--column-width-2); }
.nutpie th:nth-child(3) { width: var(--column-width-3); }
.nutpie th:nth-child(4) { width: var(--column-width-4); }
.nutpie th:nth-child(5) { width: var(--column-width-5); }
.nutpie progress {
width: 100%;
height: 15px; /* Smaller progress bars */
border-radius: 5px;
}
progress::-webkit-progress-bar {
background-color: #eee;
border-radius: 5px;
}
progress::-webkit-progress-value {
background-color: #5cb85c;
border-radius: 5px;
}
progress::-moz-progress-bar {
background-color: #5cb85c;
border-radius: 5px;
}
.nutpie .progress-cell {
width: 100%;
}
.nutpie p strong { font-size: 16px; font-weight: bold; }
@media (prefers-color-scheme: dark) {
.nutpie {
//color: #ddd;
//background-color: #1e1e1e;
box-shadow: 0 4px 6px rgba(0,0,0,0.2);
}
.nutpie table, .nutpie th, .nutpie td {
border-color: #555;
color: #ccc;
}
.nutpie th {
background-color: #2a2a2a;
}
.nutpie progress::-webkit-progress-bar {
background-color: #444;
}
.nutpie progress::-webkit-progress-value {
background-color: #3178c6;
}
.nutpie progress::-moz-progress-bar {
background-color: #3178c6;
}
}
</style>
</div>
<div class="cell-output cell-output-display">
<div class="nutpie">
<p><strong>Sampler Progress</strong></p>
<p>Total Chains: <span id="total-chains">6</span></p>
<p>Active Chains: <span id="active-chains">0</span></p>
<p>
Finished Chains:
<span id="active-chains">6</span>
</p>
<p>Sampling for now</p>
<p>
Estimated Time to Completion:
<span id="eta">now</span>
</p>
<progress id="total-progress-bar" max="8400" value="8400">
</progress>
<table>
<thead>
<tr>
<th>Progress</th>
<th>Draws</th>
<th>Divergences</th>
<th>Step Size</th>
<th>Gradients/Draw</th>
</tr>
</thead>
<tbody id="chain-details">
<tr>
<td class="progress-cell">
<progress max="1400" value="1400">
</progress>
</td>
<td>1400</td>
<td>0</td>
<td>0.59</td>
<td>3</td>
</tr>
<tr>
<td class="progress-cell">
<progress max="1400" value="1400">
</progress>
</td>
<td>1400</td>
<td>0</td>
<td>0.65</td>
<td>9</td>
</tr>
<tr>
<td class="progress-cell">
<progress max="1400" value="1400">
</progress>
</td>
<td>1400</td>
<td>0</td>
<td>0.55</td>
<td>1</td>
</tr>
<tr>
<td class="progress-cell">
<progress max="1400" value="1400">
</progress>
</td>
<td>1400</td>
<td>0</td>
<td>0.58</td>
<td>15</td>
</tr>
<tr>
<td class="progress-cell">
<progress max="1400" value="1400">
</progress>
</td>
<td>1400</td>
<td>0</td>
<td>0.67</td>
<td>7</td>
</tr>
<tr>
<td class="progress-cell">
<progress max="1400" value="1400">
</progress>
</td>
<td>1400</td>
<td>0</td>
<td>0.58</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>Alternatively, we can also sample through the <code>pymc</code> API:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> model:</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> trace <span class="op">=</span> pm.sample(model, nuts_sampler<span class="op">=</span><span class="st">"nutpie"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>While sampling, nutpie shows a progress bar for each chain. It also includes information about how each chain is doing:</p>
<ul>
<li>It shows the current number of draws</li>
<li>The step size of the integrator (very small stepsizes are typically a bad sign)</li>
<li>The number of divergences (if there are divergences, that means that nutpie is probably not sampling the posterior correctly)</li>
<li>The number of gradient evaluation nutpie uses for each draw. Large numbers (100 to 1000) are a sign that the parameterization of the model is not ideal, and the sampler is very inefficient.</li>
</ul>
<p>After sampling, this returns an <code>arviz</code> InferenceData object that you can use to analyze the trace.</p>
<p>For example, we should check the effective sample size:</p>
<div id="7a0b20fe" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> arviz <span class="im">as</span> az</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>az.ess(trace)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewbox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewbox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
*
*/
:root {
--xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--xr-border-color: var(--jp-border-color2, #e0e0e0);
--xr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--xr-background-color: var(--jp-layout-color0, white);
--xr-background-color-row-even: var(--jp-layout-color1, white);
--xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}
html[theme="dark"],
html[data-theme="dark"],
body[data-theme="dark"],
body.vscode-dark {
--xr-font-color0: rgba(255, 255, 255, 1);
--xr-font-color2: rgba(255, 255, 255, 0.54);
--xr-font-color3: rgba(255, 255, 255, 0.38);
--xr-border-color: #1f1f1f;
--xr-disabled-color: #515151;
--xr-background-color: #111111;
--xr-background-color-row-even: #111111;
--xr-background-color-row-odd: #313131;
}
.xr-wrap {
display: block !important;
min-width: 300px;
max-width: 700px;
}
.xr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}
.xr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--xr-border-color);
}
.xr-header > div,
.xr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}
.xr-obj-type,
.xr-array-name {
margin-left: 2px;
margin-right: 10px;
}
.xr-obj-type {
color: var(--xr-font-color2);
}
.xr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 150px auto auto 1fr 0 20px 0 20px;
}
.xr-section-item {
display: contents;
}
.xr-section-item input {
display: inline-block;
opacity: 0;
height: 0;
}
.xr-section-item input + label {
color: var(--xr-disabled-color);
}
.xr-section-item input:enabled + label {
cursor: pointer;
color: var(--xr-font-color2);
}
.xr-section-item input:focus + label {
border: 2px solid var(--xr-font-color0);
}
.xr-section-item input:enabled + label:hover {
color: var(--xr-font-color0);
}
.xr-section-summary {
grid-column: 1;
color: var(--xr-font-color2);
font-weight: 500;
}
.xr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}
.xr-section-summary-in:disabled + label {
color: var(--xr-font-color2);
}
.xr-section-summary-in + label:before {
display: inline-block;
content: "►";
font-size: 11px;
width: 15px;
text-align: center;
}
.xr-section-summary-in:disabled + label:before {
color: var(--xr-disabled-color);
}
.xr-section-summary-in:checked + label:before {
content: "▼";
}
.xr-section-summary-in:checked + label > span {
display: none;
}
.xr-section-summary,
.xr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}
.xr-section-inline-details {
grid-column: 2 / -1;
}
.xr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}
.xr-section-summary-in:checked ~ .xr-section-details {
display: contents;
}
.xr-array-wrap {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 20px auto;
}
.xr-array-wrap > label {
grid-column: 1;
vertical-align: top;
}
.xr-preview {
color: var(--xr-font-color3);
}
.xr-array-preview,
.xr-array-data {
padding: 0 5px !important;
grid-column: 2;
}
.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
display: none;
}
.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
display: inline-block;
}
.xr-dim-list {
display: inline-block !important;
list-style: none;
padding: 0 !important;
margin: 0;
}
.xr-dim-list li {
display: inline-block;
padding: 0;
margin: 0;
}
.xr-dim-list:before {
content: "(";
}
.xr-dim-list:after {
content: ")";
}
.xr-dim-list li:not(:last-child):after {
content: ",";
padding-right: 5px;
}
.xr-has-index {
font-weight: bold;
}
.xr-var-list,
.xr-var-item {
display: contents;
}
.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
background-color: var(--xr-background-color-row-even);
margin-bottom: 0;
}
.xr-var-item > .xr-var-name:hover span {
padding-right: 5px;
}
.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
background-color: var(--xr-background-color-row-odd);
}
.xr-var-name {
grid-column: 1;
}
.xr-var-dims {
grid-column: 2;
}
.xr-var-dtype {
grid-column: 3;
text-align: right;
color: var(--xr-font-color2);
}
.xr-var-preview {
grid-column: 4;
}
.xr-index-preview {
grid-column: 2 / 5;
color: var(--xr-font-color2);
}
.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
overflow: visible;
width: auto;
z-index: 1;
}
.xr-var-attrs,
.xr-var-data,
.xr-index-data {
display: none;
background-color: var(--xr-background-color) !important;
padding-bottom: 5px !important;
}
.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data,
.xr-index-data-in:checked ~ .xr-index-data {
display: block;
}
.xr-var-data > table {
float: right;
}
.xr-var-name span,
.xr-var-data,
.xr-index-name div,
.xr-index-data,
.xr-attrs {
padding-left: 25px !important;
}
.xr-attrs,
.xr-var-attrs,
.xr-var-data,
.xr-index-data {
grid-column: 1 / -1;
}
dl.xr-attrs {
padding: 0;
margin: 0;
display: grid;
grid-template-columns: 125px auto;
}
.xr-attrs dt,
.xr-attrs dd {
padding: 0;
margin: 0;
float: left;
padding-right: 10px;
width: auto;
}
.xr-attrs dt {
font-weight: normal;
grid-column: 1;
}
.xr-attrs dt:hover span {
display: inline-block;
background: var(--xr-background-color);
padding-right: 10px;
}
.xr-attrs dd {
grid-column: 2;
white-space: pre-wrap;
word-break: break-all;
}
.xr-icon-database,
.xr-icon-file-text2,
.xr-no-icon {
display: inline-block;
vertical-align: middle;
width: 1em;
height: 1.5em !important;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
</style><pre class="xr-text-repr-fallback"><xarray.Dataset> Size: 16B
Dimensions: ()
Data variables:
intercept float64 8B 1.517e+03
slope float64 8B 1.517e+03</pre><div class="xr-wrap" style="display:none"><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-59a64daf-4315-40a6-ae84-20ebdcf610bc" class="xr-section-summary-in" type="checkbox" disabled=""><label for="section-59a64daf-4315-40a6-ae84-20ebdcf610bc" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-c95744a9-c440-4a78-88db-ff443003e15b" class="xr-section-summary-in" type="checkbox" disabled=""><label for="section-c95744a9-c440-4a78-88db-ff443003e15b" class="xr-section-summary" title="Expand/collapse section">Coordinates: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"></ul></div></li><li class="xr-section-item"><input id="section-8e1fd148-c877-44fa-8deb-1b9c8b7fdda8" class="xr-section-summary-in" type="checkbox" checked=""><label for="section-8e1fd148-c877-44fa-8deb-1b9c8b7fdda8" class="xr-section-summary">Data variables: <span>(2)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>intercept</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">1.517e+03</div><input id="attrs-07acd05e-88a3-48e8-a73e-7e07120a4f4b" class="xr-var-attrs-in" type="checkbox" disabled=""><label for="attrs-07acd05e-88a3-48e8-a73e-7e07120a4f4b" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use href="#icon-file-text2"></use></svg></label><input id="data-386a88c4-0c5c-4a30-b2e8-e50d11eb3325" class="xr-var-data-in" type="checkbox"><label for="data-386a88c4-0c5c-4a30-b2e8-e50d11eb3325" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array(1517.4729764)</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span>slope</span></div><div class="xr-var-dims">()</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">1.517e+03</div><input id="attrs-aaac9ff0-37a6-4230-aab9-b366ad741344" class="xr-var-attrs-in" type="checkbox" disabled=""><label for="attrs-aaac9ff0-37a6-4230-aab9-b366ad741344" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use href="#icon-file-text2"></use></svg></label><input id="data-596b21ed-fccf-469d-ad0f-090f8e562331" class="xr-var-data-in" type="checkbox"><label for="data-596b21ed-fccf-469d-ad0f-090f8e562331" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"></dl></div><div class="xr-var-data"><pre>array(1517.44085063)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-1d7d17fc-b653-488e-a086-1ec75a001b89" class="xr-section-summary-in" type="checkbox" disabled=""><label for="section-1d7d17fc-b653-488e-a086-1ec75a001b89" class="xr-section-summary" title="Expand/collapse section">Indexes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"></ul></div></li><li class="xr-section-item"><input id="section-7164a24f-cea2-4565-ace6-6bcd59c1b707" class="xr-section-summary-in" type="checkbox" disabled=""><label for="section-7164a24f-cea2-4565-ace6-6bcd59c1b707" class="xr-section-summary" title="Expand/collapse section">Attributes: <span>(0)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"></dl></div></li></ul></div></div>
</div>
</div>
<p>and take a look at a trace plot:</p>
<div id="181126a5" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>az.plot_trace(trace)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="pymc-usage_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="choosing-the-backend" class="level3">
<h3 class="anchored" data-anchor-id="choosing-the-backend">Choosing the backend</h3>
<p>Right now, we have been using the numba backend. This is the default backend for <code>nutpie</code>, when sampling from pymc models. It tends to have relatively long compilation times, but samples small models very efficiently. For larger models the <code>jax</code> backend sometimes outperforms <code>numba</code>.</p>
<p>First, we need to install the <code>jax</code> package:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ex">pixi</span> add jax</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>We can select the backend by passing the <code>backend</code> argument to the <code>compile_pymc_model</code>:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>compiled_jax <span class="op">=</span> nutpie.compiled_pymc_model(model, backend<span class="op">=</span><span class="st">"jax"</span>)</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>trace <span class="op">=</span> nutpie.sample(compiled_jax)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Or through the pymc API:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> model:</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> trace <span class="op">=</span> pm.sample(</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> model,</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> nuts_sampler<span class="op">=</span><span class="st">"nutpie"</span>,</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> nuts_sampler_kwargs<span class="op">=</span>{<span class="st">"backend"</span>: <span class="st">"jax"</span>},</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>If you have an nvidia GPU, you can also use the <code>jax</code> backend with the <code>gpu</code>. We will have to install the <code>jaxlib</code> package with the <code>cuda</code> option</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="ex">pixi</span> add jaxlib <span class="at">--build</span> <span class="st">'cuda12'</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Restart the kernel and check that the GPU is available:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> jax</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Should list the cuda device</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a>jax.devices()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>Sampling again, should now use the GPU, which you can observe by checking the GPU usage with <code>nvidia-smi</code> or <code>nvtop</code>.</p>
</section>
<section id="changing-the-dataset-without-recompilation" class="level3">
<h3 class="anchored" data-anchor-id="changing-the-dataset-without-recompilation">Changing the dataset without recompilation</h3>
<p>If you want to use the same model with different datasets, you can modify datasets after compilation. Since jax does not like changes in shapes, this is only recommended with the numba backend.</p>
<p>First, we define the model, but put our dataset in a <code>pm.Data</code> structure:</p>
<div id="629172a7" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> pm.Model() <span class="im">as</span> model:</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> pm.Data(<span class="st">"x"</span>, [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>])</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> intercept <span class="op">=</span> pm.Normal(<span class="st">"intercept"</span>, mu<span class="op">=</span><span class="dv">0</span>, sigma<span class="op">=</span><span class="dv">1</span>)</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> slope <span class="op">=</span> pm.Normal(<span class="st">"slope"</span>, mu<span class="op">=</span><span class="dv">0</span>, sigma<span class="op">=</span><span class="dv">1</span>)</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> mu <span class="op">=</span> intercept <span class="op">+</span> slope <span class="op">*</span> x</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> y <span class="op">=</span> pm.Normal(<span class="st">"y"</span>, mu<span class="op">=</span>mu, sigma<span class="op">=</span><span class="fl">0.1</span>, observed<span class="op">=</span>[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We can now compile the model:</p>
<div id="e865b2bd" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>compiled <span class="op">=</span> nutpie.compile_pymc_model(model)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>trace <span class="op">=</span> nutpie.sample(compiled)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<style>
:root {
--column-width-1: 40%; /* Progress column width */
--column-width-2: 15%; /* Chain column width */
--column-width-3: 15%; /* Divergences column width */
--column-width-4: 15%; /* Step Size column width */
--column-width-5: 15%; /* Gradients/Draw column width */
}
.nutpie {
max-width: 800px;
margin: 10px auto;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
//color: #333;
//background-color: #fff;
padding: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
border-radius: 8px;
font-size: 14px; /* Smaller font size for a more compact look */
}
.nutpie table {
width: 100%;
border-collapse: collapse; /* Remove any extra space between borders */
}
.nutpie th, .nutpie td {
padding: 8px 10px; /* Reduce padding to make table more compact */
text-align: left;
border-bottom: 1px solid #888;
}
.nutpie th {
//background-color: #f0f0f0;
}
.nutpie th:nth-child(1) { width: var(--column-width-1); }
.nutpie th:nth-child(2) { width: var(--column-width-2); }
.nutpie th:nth-child(3) { width: var(--column-width-3); }
.nutpie th:nth-child(4) { width: var(--column-width-4); }
.nutpie th:nth-child(5) { width: var(--column-width-5); }
.nutpie progress {
width: 100%;
height: 15px; /* Smaller progress bars */
border-radius: 5px;
}
progress::-webkit-progress-bar {
background-color: #eee;
border-radius: 5px;
}
progress::-webkit-progress-value {
background-color: #5cb85c;
border-radius: 5px;
}
progress::-moz-progress-bar {
background-color: #5cb85c;
border-radius: 5px;
}
.nutpie .progress-cell {
width: 100%;
}
.nutpie p strong { font-size: 16px; font-weight: bold; }
@media (prefers-color-scheme: dark) {
.nutpie {
//color: #ddd;
//background-color: #1e1e1e;
box-shadow: 0 4px 6px rgba(0,0,0,0.2);
}
.nutpie table, .nutpie th, .nutpie td {
border-color: #555;
color: #ccc;
}
.nutpie th {
background-color: #2a2a2a;
}
.nutpie progress::-webkit-progress-bar {
background-color: #444;
}
.nutpie progress::-webkit-progress-value {
background-color: #3178c6;
}
.nutpie progress::-moz-progress-bar {
background-color: #3178c6;
}
}
</style>
</div>
<div class="cell-output cell-output-display">
<div class="nutpie">
<p><strong>Sampler Progress</strong></p>
<p>Total Chains: <span id="total-chains">6</span></p>
<p>Active Chains: <span id="active-chains">0</span></p>