-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapsheet-conformal-pi.html
773 lines (755 loc) · 44.3 KB
/
scrapsheet-conformal-pi.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
<!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.3.340">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>scrapsheet-conformal-pi</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 { display: inline-block; 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 { 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="scrapsheet-conformal-pi_files/libs/clipboard/clipboard.min.js"></script>
<script src="scrapsheet-conformal-pi_files/libs/quarto-html/quarto.js"></script>
<script src="scrapsheet-conformal-pi_files/libs/quarto-html/popper.min.js"></script>
<script src="scrapsheet-conformal-pi_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="scrapsheet-conformal-pi_files/libs/quarto-html/anchor.min.js"></script>
<link href="scrapsheet-conformal-pi_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="scrapsheet-conformal-pi_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="scrapsheet-conformal-pi_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="scrapsheet-conformal-pi_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="scrapsheet-conformal-pi_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<section id="scrapsheet-conformal-prediction" class="level1">
<h1>Scrapsheet Conformal Prediction</h1>
<section id="multinomial" class="level2">
<h2 class="anchored" data-anchor-id="multinomial">Multinomial</h2>
<ul>
<li><p>Non-Conformity Score (<span class="math inline">\(s_i\)</span>): <em>1 — predicted class probability</em> for the given label</p></li>
<li><p>Threshold step</p>
<ul>
<li>Scores calculated using <u>only</u> the predicted probability for the true label on the Validation set (aka Calibration set)</li>
</ul></li>
<li><p>The threshold determines what <em>coverage</em> our predicted labels will have</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>number_of_samples <span class="op">=</span> <span class="bu">len</span>(X_Cal)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>alpha <span class="op">=</span> <span class="fl">0.05</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>qlevel <span class="op">=</span> (<span class="dv">1</span> <span class="op">-</span> alpha) <span class="op">*</span> ((number_of_samples <span class="op">+</span> <span class="dv">1</span>) <span class="op">/</span> number_of_samples)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>threshold <span class="op">=</span> np.percentile(si_scores, qlevel<span class="op">*</span><span class="dv">100</span>)</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f'Threshold: </span><span class="sc">{</span>threshold<span class="sc">:0.3f}</span><span class="ss">'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<ul>
<li>Finite sample correction for the 95th quantile: multiply 0.95 by (n+1)/n</li>
</ul></li>
<li><p>Threshold is then used to get predicted labels of the test set</p>
<ul>
<li>Conformal scores are calculated for each label using the predicted probabilities</li>
<li>The predicted label for an instance is determined by whether a label has a score below the threshold.</li>
<li>Therefore, an instance may have 1 or more predicted labels or 0 predicted labels.</li>
</ul></li>
<li><p><em>Average set size</em> is the average number of predicted classes per instance.</p></li>
<li><p>Overall coverage (i.e. for all labels) will be at or very near 95% but coverage for individual classes may vary.</p>
<ul>
<li>Solution: Get thresholds for each class.</li>
<li>Also possible do this for subgroups of data, such as ensuring equal coverage for a diagnostic across racial groups, if we found coverage using a shared threshold led to problems.</li>
</ul></li>
</ul>
<p><a href="https://towardsdatascience.com/how-to-handle-uncertainty-in-forecasts-86817f21bb54" class="uri">https://towardsdatascience.com/how-to-handle-uncertainty-in-forecasts-86817f21bb54</a> <a href="https://towardsdatascience.com/mapie-explained-exactly-how-you-wished-someone-explained-to-you-78fb8ce81ff3" class="uri">https://towardsdatascience.com/mapie-explained-exactly-how-you-wished-someone-explained-to-you-78fb8ce81ff3</a> <a href="https://towardsdatascience.com/conformal-prediction-in-julia-351b81309e30" class="uri">https://towardsdatascience.com/conformal-prediction-in-julia-351b81309e30</a></p>
<ul>
<li><p>CQR requires three sets of data:</p>
<ul>
<li>Training data: data on which the quantile regression model learns.</li>
<li>Calibration data: data on which CQR calibrates the intervals.
<ul>
<li>Maybe “calibration” data is the validation set</li>
<li>In the example, he split the data into 3 equal sets</li>
</ul></li>
<li>Test data: data on which we evaluate the goodness of intervals.</li>
</ul></li>
<li><p>Steps</p>
<ol type="1">
<li>Fit quantile regression model on training data.</li>
<li>Use the model obtained at previous step to predict intervals on calibration data.
<ul>
<li>PIs are predictions at the quantiles:
<ul>
<li>(alpha/2)*100) (e.g 0.025, alpha = 0. 05)</li>
<li>(1-(alpha/2))*100) (e.g. 0.975)</li>
</ul></li>
</ul></li>
<li>Compute conformity scores on calibration data and intervals obtained at the previous step.
<ul>
<li>residuals are calculated for the PI vectors</li>
<li>Scores are calculated by taking the row-wise maximum of both (upper/lower quantile) residual vectors</li>
</ul></li>
<li>Get 1-alpha quantile from the distribution of conformity scores
<ul>
<li>This point will be the threshold</li>
</ul></li>
<li>Use the model obtained at step 1 to make predictions on test data.
<ul>
<li>Use test data to compute PI vectors (i.e. predictions at the previously stated quantiles)</li>
<li>i.e. same calculation as with the calibration data in step 2</li>
</ul></li>
<li>Compute lower/upper end of the interval by subtracting/adding the threshold from/to the quantile predictions (aka PIs)
<ul>
<li>lower conformity interval is test set lower PI - threshold</li>
<li>upper conformity interval is test set upper PI + threshold</li>
</ul></li>
</ol></li>
<li><p>Py code</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> skgarden <span class="im">import</span> RandomForestQuantileRegressor</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>alpha <span class="op">=</span> <span class="fl">.05</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. Fit quantile regression model on training data</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>model <span class="op">=</span> RandomForestQuantileRegressor().fit(X_train, y_train)</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="co"># 2. Make prediction on calibration data</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>y_cal_interval_pred <span class="op">=</span> np.column_stack([</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> model.predict(X_cal, quantile<span class="op">=</span>(alpha<span class="op">/</span><span class="dv">2</span>)<span class="op">*</span><span class="dv">100</span>), </span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> model.predict(X_cal, quantile<span class="op">=</span>(<span class="dv">1</span><span class="op">-</span>alpha<span class="op">/</span><span class="dv">2</span>)<span class="op">*</span><span class="dv">100</span>)])</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a><span class="co"># 3. Compute conformity scores on calibration data</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>y_cal_conformity_scores <span class="op">=</span> np.maximum(</span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a> y_cal_interval_pred[:,<span class="dv">0</span>] <span class="op">-</span> y_cal, </span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> y_cal <span class="op">-</span> y_cal_interval_pred[:,<span class="dv">1</span>])</span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a><span class="co"># 4. Threshold: Get 1-alpha quantile from the distribution of conformity scores</span></span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a><span class="co"># Note: this is a single number</span></span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a>quantile_conformity_scores <span class="op">=</span> np.quantile(</span>
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a> y_cal_conformity_scores, <span class="dv">1</span><span class="op">-</span>alpha)</span>
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a><span class="co"># 5. Make prediction on test data</span></span>
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a>y_test_interval_pred <span class="op">=</span> np.column_stack([</span>
<span id="cb2-26"><a href="#cb2-26" aria-hidden="true" tabindex="-1"></a> model.predict(X_test, quantile<span class="op">=</span>(alpha<span class="op">/</span><span class="dv">2</span>)<span class="op">*</span><span class="dv">100</span>), </span>
<span id="cb2-27"><a href="#cb2-27" aria-hidden="true" tabindex="-1"></a> model.predict(X_test, quantile<span class="op">=</span>(<span class="dv">1</span><span class="op">-</span>alpha<span class="op">/</span><span class="dv">2</span>)<span class="op">*</span><span class="dv">100</span>)])</span>
<span id="cb2-28"><a href="#cb2-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-29"><a href="#cb2-29" aria-hidden="true" tabindex="-1"></a><span class="co"># 6. Compute left (right) end of the interval by</span></span>
<span id="cb2-30"><a href="#cb2-30" aria-hidden="true" tabindex="-1"></a><span class="co"># subtracting (adding) the quantile to the predictions</span></span>
<span id="cb2-31"><a href="#cb2-31" aria-hidden="true" tabindex="-1"></a>y_test_interval_pred_cqr <span class="op">=</span> np.column_stack([</span>
<span id="cb2-32"><a href="#cb2-32" aria-hidden="true" tabindex="-1"></a> y_test_interval_pred[:,<span class="dv">0</span>] <span class="op">-</span> quantile_conformity_scores,</span>
<span id="cb2-33"><a href="#cb2-33" aria-hidden="true" tabindex="-1"></a> y_test_interval_pred[:,<span class="dv">1</span>] <span class="op">+</span> quantile_conformity_scores])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div></li>
<li><p>Data usage summary</p>
<ul>
<li>The model is fit on the training data</li>
<li>The scores are calculated on the calibration data along with the threshold</li>
<li>For the conformity intervals, predictions from a model fit on the test data add/subtract the threshold from those predictions</li>
</ul></li>
<li><p>Positive threshold means the PIs get widened by the threshold amount, upper and lower.</p>
<ul>
<li>e.g. quantile DL model</li>
</ul></li>
<li><p>Negative threshold means the PIs get shrank by the threshold amount, upper and lower.</p>
<ul>
<li>e.g quantile RF model</li>
</ul></li>
<li><p>So future data (just like the test set) uses the threshold amount obtained using the training and calibration data to calculate the PIs for the those estimates</p></li>
<li><p>Description</p>
<ul>
<li>Conformity scores express the (signed) distance between each observation and the nearest extreme of the interval.</li>
<li>The sign is given by the position of the point, whether it falls inside or outside the interval.
<ul>
<li>When the point lies within the interval, the sign of conformity score is negative</li>
<li>When the point lies outside the interval, the sign of conformity score is positive.</li>
<li>When the point lies exactly on the interval, the conformity score is zero</li>
</ul></li>
<li>Since the maximum is taken to get the overall conformity score for that data point,
<ul>
<li>Both the lower quantile prediction and upper quantile predictions have to lie within the interval for the overall score to be negative at that data point</li>
<li>Either or both quantile predictions have to lie outside the interval for the overall score to be positive at the data point.</li>
<li>Both quantile predictions have to lie exactly on the interval for the overall score to be zero at the data point</li>
<li>Therefore, all things being equal, the threshold is more likely to expand the interval than contract it.
<ul>
<li>The alpha can be adjusted if need be to get the desired coverage</li>
</ul></li>
</ul></li>
</ul></li>
</ul>
<p>Boosted models only able to fit one quantile at a time. no xgboost quantile regression <code>gbm::gbm(distribution = list(name = "quantile",alpha = 0.25)</code></p>
<ul>
<li>where alpha is the quantile</li>
</ul>
<p>lightgbm</p>
<ul>
<li>args: objective = “quantile”, alpha = 0.50,</li>
<li>(maybe) metric = “quantile”
<ul>
<li>“metric(s) to be evaluated on the evaluation set(s)”</li>
</ul></li>
</ul>
<p>catboost</p>
<ul>
<li>catboost.train(params = list(c(‘Quantile:alpha=0.1’))
<ul>
<li>params syntax might be wrong. R documentation isn’t even half-assed. It’s like 1% -assed</li>
</ul></li>
</ul>
<p>rf</p>
<ul>
<li>{grf}, {ranger}, {quantregForest}, {quantregRanger}</li>
<li>grf’s quantile_forest method does not actually implement Meinshausen’s quantile regression forest algorithm. A major difference is that grf makes splits that are sensitive to quantiles, whereas Meinshausen’s method uses standard CART splits. (<a href="https://arxiv.org/pdf/1610.01271.pdf">Paper</a>)</li>
</ul>
<p>Example: ranger rf quantile regression (shalloway article)</p>
<ul>
<li><p>model</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>rf_mod <span class="ot"><-</span> <span class="fu">rand_forest</span>() <span class="sc">%>%</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">set_engine</span>(<span class="st">"ranger"</span>, <span class="at">importance =</span> <span class="st">"impurity"</span>, <span class="at">seed =</span> <span class="dv">63233</span>, <span class="at">quantreg =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">set_mode</span>(<span class="st">"regression"</span>)</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">63233</span>)</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>rf_wf <span class="ot"><-</span> workflows<span class="sc">::</span><span class="fu">workflow</span>() <span class="sc">%>%</span> </span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_model</span>(rf_mod) <span class="sc">%>%</span> </span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_recipe</span>(rf_recipe) <span class="sc">%>%</span> </span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">fit</span>(ames_train)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div></li>
<li><p>quantile predictions</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>preds_bind <span class="ot"><-</span> <span class="cf">function</span>(data_fit, <span class="at">lower =</span> <span class="fl">0.05</span>, <span class="at">upper =</span> <span class="fl">0.95</span>){</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">predict</span>(</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> rf_wf<span class="sc">$</span>fit<span class="sc">$</span>fit<span class="sc">$</span>fit, <span class="co"># parsnip::extract_fit_engine available now</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> workflows<span class="sc">::</span><span class="fu">pull_workflow_prepped_recipe</span>(rf_wf) <span class="sc">%>%</span> <span class="fu">bake</span>(data_fit), <span class="co"># preprocessed data</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"quantiles"</span>,</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="at">quantiles =</span> <span class="fu">c</span>(lower, upper, <span class="fl">0.50</span>)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> </span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">with</span>(predictions) <span class="sc">%>%</span> </span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">as_tibble</span>() <span class="sc">%>%</span> </span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">set_names</span>(<span class="fu">paste0</span>(<span class="st">".pred"</span>, <span class="fu">c</span>(<span class="st">"_lower"</span>, <span class="st">"_upper"</span>, <span class="st">""</span>))) <span class="sc">%>%</span> </span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">contains</span>(<span class="st">".pred"</span>), <span class="sc">~</span><span class="dv">10</span><span class="sc">^</span>.x)) <span class="sc">%>%</span> </span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">bind_cols</span>(data_fit) <span class="sc">%>%</span> </span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">contains</span>(<span class="st">".pred"</span>), Sale_Price, Lot_Area, Neighborhood, Years_Old, Gr_Liv_Area, Overall_Qual, Total_Bsmt_SF, Garage_Area)</span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a>rf_preds_test <span class="ot"><-</span> <span class="fu">preds_bind</span>(ames_holdout)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div></li>
<li><p>Visualize<br>
<img src="./_resources/Confidence_&_Prediction_Intervals.resources/unnamed-chunk-3-1.png" class="img-fluid" width="532"></p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">1234</span>)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>rf_preds_test <span class="sc">%>%</span> </span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">pred_interval =</span> ggplot2<span class="sc">::</span><span class="fu">cut_number</span>(Sale_Price, <span class="dv">10</span>)) <span class="sc">%>%</span> </span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(pred_interval) <span class="sc">%>%</span> </span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample_n</span>(<span class="dv">2</span>) <span class="sc">%>%</span> </span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> .pred))<span class="sc">+</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">y =</span> .pred, <span class="at">color =</span> <span class="st">"prediction interval"</span>))<span class="sc">+</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_errorbar</span>(<span class="fu">aes</span>(<span class="at">ymin =</span> .pred_lower, <span class="at">ymax =</span> .pred_upper, <span class="at">color =</span> <span class="st">"prediction interval"</span>))<span class="sc">+</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">y =</span> Sale_Price, <span class="at">color =</span> <span class="st">"actuals"</span>))<span class="sc">+</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_log10</span>(<span class="at">labels =</span> scales<span class="sc">::</span>dollar)<span class="sc">+</span> <span class="co"># target variable is log10 transformed</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(<span class="at">labels =</span> scales<span class="sc">::</span>dollar)<span class="sc">+</span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"90% Prediction intervals on a holdout dataset"</span>,</span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Random Forest Model"</span>,</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Sale_Price prediction intervals and actuals"</span>)<span class="sc">+</span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>()<span class="sc">+</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_fixed</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div></li>
<li></li>
</ul>
<p>Coverage and interval length tuning</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode py code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> compute_coverage_len(y_test, y_lower, y_upper):</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="st">""" Compute average coverage and length of prediction intervals</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="st"> Parameters</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="st"> ----------</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="st"> y_test : numpy array, true labels (n)</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="st"> y_lower : numpy array, estimated lower bound for the labels (n)</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="st"> y_upper : numpy array, estimated upper bound for the labels (n)</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="st"> Returns</span></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a><span class="st"> -------</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a><span class="st"> coverage : float, average coverage</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a><span class="st"> avg_length : float, average length</span></span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a><span class="st"> """</span></span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> in_the_range <span class="op">=</span> np.<span class="bu">sum</span>((y_test <span class="op">>=</span> y_lower) <span class="op">&</span> (y_test <span class="op"><=</span> y_upper))</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> coverage <span class="op">=</span> in_the_range <span class="op">/</span> <span class="bu">len</span>(y_test) <span class="op">*</span> <span class="dv">100</span></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> avg_length <span class="op">=</span> np.mean(<span class="bu">abs</span>(y_upper <span class="op">-</span> y_lower))</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> coverage, avg_length</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<ul>
<li><p>Notes</p>
<ul>
<li>training set_1 = 0.80, test set = 0.20</li>
<li>training set_1 divided into train_2 and calibration sets (50/50)
<ul>
<li>row indexes: idx_train, idx_cal</li>
<li>The train_2 of the split is split further for figuring out the quantiles with correct coverage
<ul>
<li>train_3/test = 95/5</li>
</ul></li>
</ul></li>
<li>Predictions from tuned quantiles used to calculate scores</li>
<li>Uses alpha as the threshold index</li>
<li>hardcoded values
<ul>
<li>coverage_factor would have to be something guess-timated or based on some previous knowledge about how “off” the algrithm’s PI quantiles’ coverage is</li>
<li>initial best_avg_length would require some knowledge of a baseline PI length and scale of the data</li>
<li>Maybe these don’t have to be hardcoded</li>
</ul></li>
<li>Values
<ul>
<li>quantiles: [0.05, 0.95]</li>
<li>quantile factor = 0.85 * (0.95 - 0.05) = 0.76</li>
<li>alpha = 0.10 (i.e. acceptable miscoverage rate; “significance” in the code)</li>
</ul></li>
<li>asymmetric score function (QuantileRegAsymmetricErrFunc) available</li>
</ul></li>
<li><p>run_icp (cqr/helper.py)</p>
<ul>
<li><p>nc, x_train, y_train, x_test, idx_train, idx_cal, alpha</p></li>
<li><p>instantiates IcpRegressor (nonconformist/icp.py)</p>
<ul>
<li>input: nc_function = RegressorNC(QuantileForestRegressorAdapter (cqr/helper.py), QuantileRegErrFunc (nonconformist/nc.py))
<ul>
<li>RegressorNC (nonconformist/nc.py)
<ul>
<li>inherits
<ul>
<li>BaseModelNc(model, errorfunc)
<ul>
<li>methods
<ul>
<li>fit via model.fit</li>
<li>score
<ul>
<li>computes nonconformity scores</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>inherits
<ul>
<li>BaseICP
<ul>
<li>inherits sklearn BaseEstimater
<ul>
<li>methods: get_params, set_params</li>
</ul></li>
<li>method
<ul>
<li>fit via nc.fit</li>
<li>calibrate</li>
<li>other calibrate stuff</li>
</ul></li>
</ul></li>
</ul></li>
<li>method: predict</li>
</ul></li>
<li><p>Runs via IcpRegressor (nonconformist/icp.py)</p>
<ul>
<li>fit
<ul>
<li>input: X_train[idx_train,:], y_train[idx_train]</li>
<li>QuantileForestRegressorAdapter (cqr/helper.py)
<ul>
<li>inherits RegressorAdapter (noncomformist/base.py)
<ul>
<li>inherits BaseModelAdaptor
<ul>
<li>inherits BaseEstimator</li>
<li>methods: fit, predict, underlying_predict</li>
</ul></li>
<li>methods: underlying_predict</li>
</ul></li>
<li>methods
<ul>
<li>fit
<ul>
<li>inputs: X_train[idx_train,:], y_train[idx_train]</li>
<li>target_coverage = quantiles[0] - quantiles[1] (quantiles = [0.05, 0.95]) = 0.90</li>
<li>coverage_factor = 0.85</li>
<li>target_coverage = coverage_factor * target_coverage = 0.765</li>
<li>range_vals = 30</li>
<li>num_vals = 10</li>
<li>grid_q = grid of quantiles to search
<ul>
<li>lower = 0.05 to 0.35 (num_vals = 10 evenly spaced values) (i.e. 0.05 to 0.05 + range_vals)</li>
<li>upper = 0.95 to 0.65 (num_vals = 10 evenly spaced values) (i.e. 0.95 to 0.95 - range_vals)</li>
<li>concantenated pairwise (0.35, 0.65 gets fit, etc.), so I think he’s fitting 10 models</li>
</ul></li>
<li>calls CV_quntiles_rf (cqr/tune_params_cv.py)
<ul>
<li>inputs: rf_params, x, y, target_coverage, grid_q, test_ratio, coverage_factor)
<ul>
<li>test_ratio = 0.05</li>
<li>So train/test = 95/5</li>
</ul></li>
<li>process
<ul>
<li>rf fit on training data</li>
<li>best_avg_length = 1e10 (initial value)</li>
<li>loop (row in row of grid_q)</li>
<li>predicts lower and upper quantile vectors (i.e. row of the grid_q) on test data</li>
<li>calls compute_coverage_len (cqr/helper.py)</li>
<li> inputs: y_test, y_lower (i.e. lower quantile), y_upper (i.e. upper quantile)</li>
<li> computes coverage and avg length of interval</li>
<li> check if coverage > target_coverage AND avg length < best_avg_length</li>
<li> if so, set best_q = row of grid_q, best avg length = avg length</li>
<li>return best_q</li>
</ul></li>
<li>stored in attribute, self.cv_quantiles </li>
</ul></li>
</ul></li>
<li>predict
<ul>
<li>uses self.cv_quantiles</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>calibrate
<ul>
<li>input: X_train[idx_cal,:], y_train[idx_cal]</li>
<li>inherits BaseIcp
<ul>
<li>method:calibrate
<ul>
<li>uses nc.function.score which is RegressorNc (nonconformist/nc.py)
<ul>
<li>inherits BaseModelNc
<ul>
<li>Calc predictions with QuantileForestRegressorAdapter (cqr/helper.py) predict method
<ul>
<li>So it does use tuned quantiles to compute scores</li>
</ul></li>
<li>sends predictions to QuantileRegErrFunc (nonconformist/nc.py) to calc scores
<ul>
<li>max(\hat{q}_low - y, y - \hat{q}_high)</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>Sorts (desc), and stores scores in self.cal_scores</li>
</ul></li>
</ul></li>
<li>predict
<ul>
<li>input: X_test, alpha</li>
<li>IcpRegressor.predict makes no sense to me. Looks to me that it would return an array of zeros. ¯\_(ツ)_/¯</li>
<li>It uses nc.function.predict(X_test, self.cal_scores, significance (aka alpha)) in the code, but the conditional doesn’t make sense to me
<ul>
<li>RegressorNC.predict
<ul>
<li>Calc predictions with QuantileForestRegressorAdapter (cqr/helper.py) predict method</li>
<li>Uses alpha to find the index of threshold score value through a QuantileRegErrFunc method</li>
<li>he has
<ul>
<li>lower PI = lower_quantile_pred - threshold score</li>
<li>upper PI = upper_quantile_pred + threshold score</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>
<p>Classification</p>
<ul>
<li>Notes from paper, “<a href="https://arxiv.org/pdf/2006.02544.pdf">Classification with Valid and Adaptive Coverage</a>”</li>
<li>Marginal Coverage<br>
<img src="./_resources/Confidence_&_Prediction_Intervals.resources/Screenshot%20(728).png" class="img-fluid">
<ul>
<li>The probability that an future observed value is in a predicted set is greater than equal some miscoverage rate.</li>
</ul></li>
<li>Conditional Coverage<br>
<img src="./_resources/Confidence_&_Prediction_Intervals.resources/Screenshot%20(727).png" class="img-fluid">
<ul>
<li>Valid coverage conditional on a specific observed value of the features X.
<ul>
<li>Stronger statement than marginal coverage and cannot be achieved in theory without strong modeling assumptions</li>
</ul></li>
</ul></li>
<li>The only restrictions are the data are exchangeable and the training algorithm treats all samples exchangeably; i.e., it should be invariant to their order.
<ul>
<li>No typical ML algorithm fails this</li>
</ul></li>
<li>Adaptive classification with CV+ calibration
<ul>
<li>Sample Ui ∼ Uniform(0, 1) for each i ∈ {1, . . . , n + 1}, independently of everything else</li>
<li>Typical k-fold CV
<ul>
<li>train model on all folds except k, etc.</li>
</ul></li>
<li>Construct prediction set, <span class="math inline">\(CCV+n,α\)</span><br>
<img src="./_resources/Confidence_&_Prediction_Intervals.resources/Screenshot%20(757).png" class="img-fluid">
<ul>
<li>Says we sweep over all possible labels y ∈ Y and include y in the final prediction set CCV+ n,α (Xn+1) if the corresponding score E(Xn+1, y, Un+1; ˆπ k(i) ) is smaller than (1 − α)(n + 1) hold-out scores E(Xi , Yi , Ui ; ˆπ k(i) ) evaluated on the true labeled data</li>
<li></li>
</ul></li>
</ul></li>
</ul>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>