-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_old.html
2006 lines (1716 loc) · 78.6 KB
/
index_old.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="en">
<title>Interventional Tree Explainer</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-9Nhn55MVVN0/4OFx7EE5kpFBPsEMZxKTCnA+4fqDmg12eCTqGi6+BB2LjY8brQxJ" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
<script language="JavaScript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.2.1/math.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>d3v4 = d3;</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<style>
hover a {
color: "Olive";
}
.sidenav {
height: 100%;
width: 300px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 20px;
}
.sidenav a {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
}
.sidenav a:hover {
color: #f1f1f1;
}
body {font-family: "Lato", sans-serif}
p {font-size: 20px}
ul {font-size: 20px}
caption {font-size: 18px}
figcaption {font-size: 18px}
.no-margin {
margin:0px;
font-family:courier;
font-size:16px;
}
.responsive {
width: 100%;
height: auto;
}
/*Figure 1*/
.bar {
fill: #4DAF51;
}
.bar:hover {
fill: #019788;
}
.axis {
font-size: 16px;
}
.axis path,
.axis line {
fill: none;
display: none;
shape-rendering: crispEdges;
}
.label {
color: #4DAF51;
font-size: 16px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
#table_container {
margin: 0 auto;
background-color : #F4F6F6;
}
/* Slider code */
.slidecontainer {
width: 100%;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
/* End slider code */
/* D3 Trees*/
.node circle {
fill: #fff;
stroke: #ccc;
stroke-width: 4px;
}
.node text { font: 12px sans-serif; }
.link {
fill: none;
/*stroke: #ccc;*/
stroke-width: 3px;
}
div.tooltip {
position: absolute;
}
.container {
width: 760px;
/*margin: auto;*/
}
.first {
width: 380px;
float: left;
height: 630px;
/*background-color: blue;*/
}
.second {
width: 380px;
float: left;
height: 630px;
/*background-color: green;*/
}
</style>
<body>
<div class="sidenav">
<a href="#" class="w3-justify w3-button"><h3>Top</h3></a>
<a href="#introduction" class="w3-justify w3-button"><h3>Introduction</h3></a>
<a href="#motivation" class="w3-justify w3-button" style="margin-left:20px"><h5>Motivation</h5></a>
<a href="#background" class="w3-justify w3-button"><h3>1. Background</h3></a>
<a href="#shapley_values" class="w3-justify w3-button" style="margin-left:20px"><h5>1.1 Shapley values</h5></a>
<a href="#shap_values" class="w3-justify w3-button" style="margin-left:20px"><h5>1.2 SHAP values</h5></a>
<a href="#background_distribution" class="w3-justify w3-button" style="margin-left:20px"><h5>1.3 SHAP values with a <br>Background Distribution</h5></a>
<a href="#algorithm" class="w3-justify w3-button"><h3>2. Algorithm</h3></a>
<a href="#brute_force" class="w3-justify w3-button" style="margin-left:20px"><h5>2.1 Brute force <br>implementation</h5></a>
<!-- <a href="#an_example" class="w3-justify w3-button" style="margin-left:20px"><h5>2.2 An example</h5></a> -->
<a href="#naive_implementation" class="w3-justify w3-button" style="margin-left:20px"><h5>2.2 Naive implementation</h5></a>
<a href="#dynamic_programming_implementation" class="w3-justify w3-button" style="margin-left:20px"><h5>2.3 Dynamic programming <br> implementation</h5></a>
<a href="#comparison_of_methods" class="w3-justify w3-button" style="margin-left:20px"><h5>2.4 Comparison of SHAP <br> Methods</h5></a>
<a href="#references" class="w3-justify w3-button"><h3>References</h3></a>
</div>
<!-- Page content -->
<div class="w3-content" style="max-width:2000px;margin-left:300px">
<div class="w3-display-container w3-center">
<img src="images/trees.jpg" style="width:100%">
<div class="w3-display-bottommiddle w3-container w3-text-white w3-hide-small">
<h2><strong>A fast and exact game-theoretic algorithm to explain trees</strong></h2>
<!-- <h2>Exact game-theoretic explanations of trees in linear time</h2> -->
<h2>Hugh Chen</h2>
</div>
</div>
<!-- Introduction -->
<div class="w3-container w3-content w3-center w3-padding-16" style="max-width:800px" id="abstract">
<h2 class="w3-justify" id="introduction">Introduction</h2>
<p class="w3-opacity w3-justify">
Nowadays, machine learning (ML) is widespread. In this field, one of the most prolific class ML models are trees. As evidence, a recent survey of data scientists and researchers found that tree models were both the second and third most popular class of method, beaten only by Logistic Regression (Figure 1). Although small tree models can be interpretable (<a href="#rudin2019">Rudin 2019</a>), most tree models are generally large and hard for humans to interpret.
</p>
<div id="table_container" style="overflow-x:auto;align:center;">
<p class="w3-opacity" style="font-size:20px;margin:20px 20px 0px 20px"><strong>Figure 1</strong>: The most popular data science methods according to a <a href="https://www.kaggle.com/surveys/2017">2017 Kaggle survey</a> (based on a total of 7,301 responses).</p>
<div id="fig1"></div>
</div>
<p class="w3-opacity w3-justify">
In order to explain these models, we use Shapley values - a unique game-theoretic solution for spreading credit between features. First, we discuss SHAP values, an extension of Shapley values to machine learning models (<a href="#background">Section 1</a>). However, exactly computing SHAP values for an arbitrary model is NP-hard (<a href="#matsui2001NP">Matsui et. al. 2001</a>), but by focusing on explaining tree models, it is possible to compute them exactly in linear time (<a href="#algorithm">Section 2</a>).
</p>
<!-- <h3 class="w3-justify" id="motivation">Motivation</h3> -->
<p class="w3-opacity w3-justify">
<i>What is the goal of this article?</i>
<p class="w3-opacity w3-justify">
Given that there is a long history of model explanations going awry when users do not understand what an explanation means (e.g., p-values for linear models (<a href="#schervish1996">Schervish 1996</a>)), it is critical to have a broadly accessible explanation of SHAP values and how they are obtained for tree models to ensure that they are not misused. To this end, we aim to provide an easily accessible answer to two questions: 1.) What are SHAP values? 2.) How can we obtain SHAP values for trees? In particular, we focus on explaining an algorithm that was empirically evaluated in <a href="#lundberg2020fromlocal">Lundberg (2020)</a>.
</p>
</div>
<!-- The Background Section -->
<div class="w3-container w3-content w3-center w3-padding-16" style="max-width:800px">
<h2 class="w3-justify" id="background">1. Background</h2>
<p class="w3-opacity w3-justify">
In this section, we describe Shapley values and a few versions that have been used to explain machine learning models. Then, we use a linear model example to justify a specific extension of the Shapley values. With this formulation, we show how obtaining the SHAP values reduces to a average over so-called single reference SHAP values that can be thought of as explanations with respect to a single foreground sample (explicand) and a single background sample (reference).
</p>
<!-- What are Shapley Values? -->
<h3 class="w3-justify" id="shapley_values">1.1 Shapley values</h3>
<p class="w3-opacity w3-justify">
Shapley values are a method to spread credit among players in a "coalitional game". We can define the players to be a set \(N=\{1,\cdots,d\}\). Then, the coalitional game is a function that maps subsets of the players to a scalar value:
$$
v(S):2^N\to\mathbb{R}^1
$$
</p>
<p class="w3-opacity w3-justify">
To make these concepts more concrete, we can imagine a company that makes a profit \(v(S)\) that is determined by what combination of individuals they employ \(S\). Furthermore, let's assume we know \(v(S)\) for all possible combinations of employees. Then, the Shapley values assign credit to an individual \(i\) by taking a weighted average of how much the profit increases when \(i\) works with a group \(S\) versus when he does not work with \(S\). Repeating this for all possible subsets \(S\) gives us the Shapley Values:
<!-- (include a simple graphic here?) -->
$$
\overbrace{\phi_i(v)}^{\text{Shapley value of }i}=\sum_{S\subseteq N\setminus\{i\}}\underbrace{\frac{|S|!(|N|-|S|-1)!}{|N|!}}_{\text{Weight }W(|S|,|N|)}(\overbrace{v(S\cup\{i\})-v(S)}^{\text{Profit individual }i\text{ adds}})
$$
<a href="#example1">Example 1</a> computes this summation for an arbitrary coalitional game with three players.
</p>
<div id="table_container" style="overflow-x:auto;align:center;">
<p id="example1" class="w3-opacity w3-left" align="left" style="margin:20px"><strong>Example 1</strong>: Shapley values for a company that makes a profit \(v(S)\) based on it's three prospective employees \(Ava\), \(Ben\), and \(Cat\).</p>
<table class="w3-opacity w3-center" style="width:300px;display:inline;margin-right:25px;font-size:18px;">
<caption align="top" style="font-size:20px;"><strong>Coalitional game</strong></caption>
<col width="200px"/>
<col width="150px"/>
<col width="50px"/>
<tr>
<th>Subset \(S\)</th>
<th>Profit \(v(S)\)</th>
<th></th>
</tr>
<tr>
<td>\(\{\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_n" onchange="calcSV()"></td>
<td><span id="s_n_out"></span></td>
</tr>
<tr>
<td>\(\{Ava\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_a" onchange="calcSV()"></td>
<td><span id="s_a_out"></span></td>
</tr>
<tr>
<td>\(\{Ben\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_b" onchange="calcSV()"></td>
<td><span id="s_b_out"></span></td>
</tr>
<tr>
<td>\(\{Cat\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_c" onchange="calcSV()"></td>
<td><span id="s_c_out"></span></td>
</tr>
<tr>
<td>\(\{Ava,Ben\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_ab" onchange="calcSV()"></td>
<td><span id="s_ab_out"></span></td>
</tr>
<tr>
<td>\(\{Ava,Cat\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_ac" onchange="calcSV()"></td>
<td><span id="s_ac_out"></span></td>
</tr>
<tr>
<td>\(\{Ben,Cat\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_bc" onchange="calcSV()"></td>
<td><span id="s_bc_out"></span></td>
</tr>
<tr>
<td>\(\{Ava,Ben,Cat\}\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="s_abc" onchange="calcSV()"></td>
<td><span id="s_abc_out"></span></td>
</tr>
</table>
<table class="w3-opacity w3-center fixed" style="width:200px;display:inline;margin-left:25px;font-size:20px;">
<caption align="top" style="font-size:20px;"><strong>Shapley values</strong></caption>
<col width="100px"/>
<col width="100px"/>
<tr>
<td>\(\phi_{Ava}(v)\)</td>
<td id="phi_a">0</td>
</tr>
<tr>
<td>\(\phi_{Ben}(v)\)</td>
<td id="phi_b">0</td>
</tr>
<tr>
<td>\(\phi_{Cat}(v)\)</td>
<td id="phi_c">0</td>
</tr>
</table>
<br><br>
<input type="button" value="Preset A" class="w3-button w3-teal" id="ex1_presetA" style="font-size:20px;" onclick="ex1_presetA()">
<input type="button" value="Preset B" class="w3-button w3-green" id="ex1_presetB" style="font-size:20px;" onclick="ex1_presetB()">
<input type="button" value="Preset C" class="w3-button w3-green" id="ex1_presetC" style="font-size:20px;" onclick="ex1_presetC()">
<input type="button" value="Preset D" class="w3-button w3-green" id="ex1_presetD" style="font-size:20px;" onclick="ex1_presetD()">
<br>
<p class="w3-opacity w3-center" align="center" id="ex1_preset_text" style="margin:20px">No credit.</p>
</div>
<p class="w3-opacity w3-justify">
The Shapley values consider how much an individual increases profit when they work together with all other possible teams. Furthermore, they are a unique solution to spreading credit as defined by several desirable properties (<a href="#young_uniquesol">Young 1985</a>):
<ul class="w3-opacity w3-justify">
<li>Local Accuracy/Efficiency: The sum of Shapley values for all employees adds up to the profit with all employees minus the profit with no employees:</li>
$$
\sum_{i\in N} \phi_i(v)=v(N)-v(\{\})
$$
<li>Consistency/Monotonicity: If an employee \(i\) always increases company \(v_1\)'s profit more than they would company \(v_2\) for all teams of other employees, then \(i\)'s attribution for \(v_1\) should be greater than or equal to their attribution in \(v_2\):</li>
$$
v_1(S\cup {i})-v_1(S)\geq v_2(S\cup {i})-v_2(S) \forall S \implies \phi_i(v_1)\geq \phi_i(v_2)
$$
<li>Missingness: Employees \(i\) that don't help or hurt the company's profit must have no attribution.</li>
$$
v(S\cup {i})=v(S)\forall S\implies \phi_i(v)=0
$$
</ul>
</p>
<p class="w3-opacity w3-justify">
<a href="#lundberg_unified">Lundberg and Lee (2017)</a> extend Shapley values to explain ML models (<a href="#shap_values">Section 1.2</a>).
</p>
<!-- What are SHAP Values? -->
<h3 class="w3-justify" id="shap_values">1.2 SHAP values</h3>
<p class="w3-opacity w3-justify">
SHAP values are a variant of Shapley values to explain ML models. For SHAP values, the game \(v(S)\) is now related to a machine learning model \(f(x)\) and the set of players is now a feature vector \(x:=\{x_1,\cdots,x_d\}\in\mathbb{R}^d\).
</p>
<p class="w3-opacity w3-justify">
In contrast, Shapley values define a game's output \(v(S)\) to be the value of the game with some players "present" and the remaining players "missing". Here, "missing" is naturally defined: whether or not a player \(i\) is present in the set \(S\) (or, as in our example, whether an employee was working for the company).
</p>
<p class="w3-opacity w3-justify">
In comparison, ML models generally require a fixed length input with continuous values which makes setting features to be "missing" or "present" less straightforward. One simple approach is to impute the "missing" features by masking them by a fixed background sample \(x^b \in \mathbb{R}^d\).
$$
v(S)=f(h^S)\text{, where } h^S_i = x_i\text{ if }i\in S\text{, }h^S_i = x^b_i\text{ otherwise}
$$
For instance, if we want to set the feature <i>Height</i> to "missing", we could replace it with a specific <i>background value</i>. One natural option of <i>background value</i> is zero. However, this is unsatisfying because a <i>Height</i> of zero is impossible. Another option of <i>background value</i> could be the average human height. This is arguably a better choice, however it means that explanations for people who are exactly average height will be biased to give no weight to height. In fact, masking with any single background sample will introduce bias to SHAP values. Alternatively, instead of using a single background sample, we can use an entire background distribution to define missingness.
</p>
<p class="w3-opacity w3-justify">
Then, a very natural approach to impute features is with conditional expectation. Instead of simply replacing "missing" features with a fixed value, we condition on the set of features that are "present" as if we know them and use those to guess at the "missing" features. If we define \(D\) to be the background (underlying) distribution our samples are drawn from, the value of the game is:
$$
v(S)=\mathbb{E}_D[f(x)|x_{S}]
$$
</p>
<p class="w3-opacity w3-justify">
One caveat is that getting this conditional expectation for actual data is very difficult. Furthermore, even if you do manage to do so, the resulting explanations can end up having undesirable characteristics (more on this later). Because our goal is to focus on explaining the model itself, an arguably more natural approach is to use causal inference's <i>interventional conditional expectation</i>:
$$
v(S)=\mathbb{E}_D[f(x)|\text{do}(x_{S})]
$$
The <i>do</i> notation is Judea Pearl's <i>do</i>-operator (<a href="pearl_2000_causality">Pearl 2000</a>). In words, we break the dependence between the features in \(S\) and the remaining features, which is analogous to <i>intervening</i> on the remaining features (more on this in <a href="#background_distribution">Section 1.3</a>). The motivation behind this decision comes from <a href="#janzing2019feature">Janzing et. al. (2019)</a> which is also very close to Random Baseline Shapley in <a href="#sundararajan2019many">Sundararajan et. al. (2019)</a>). Additionally, this is exactly the assumption made by <a href="https://shap.readthedocs.io/en/latest/#shap.KernelExplainer">Kernel Explainer</a> and <a href="https://shap.readthedocs.io/en/latest/#shap.SamplingExplainer">Sampling Explainer</a> from the SHAP package.
</p>
<p class="w3-opacity w3-justify">
In <a href="#example2">Example 2</a> we highlight tradeoffs between the conditional expectation and the interventional conditional expectation by devising a simple example with a linear function and a multivariate normally distributed background distribution. In general, computing the conditional expectation SHAP value is difficult; however, because we choose a multivariate normal distribution the conditional expectation is well defined. In addition, we choose a linear function because the conditional expectation of the function equals the function applied to the conditional expectation.
</p>
<div id="table_container" style="overflow-x:auto;align:center;">
<p id="example2" class="w3-opacity w3-left" align="left" style="margin:20px">
<strong>Example 2</strong>: Comparing choices of set function for SHAP values: conditional expectation (CE) and interventional conditional expectation (ICE). We make two simplifying assumptions:
</p>
<ul class="w3-opacity" align="left" style="margin:20px">
<li>The function is linear (\(f=\beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3 + \beta_4 x_4\))</li>
<li>The data-generating distribution is multivariate normal (\(D\sim \mathcal{N}_4(0,C)\))</li>
</ul>
<table class="w3-opacity" style="width:300px;display:inline;margin-right:25px;font-size:20px;">
<col width="60px">
<col width="60px">
<tr align="center">
<td colspan="2"><strong>Linear</strong></td>
</tr>
<tr align="center">
<td colspan="2"><strong>Model</strong></td>
</tr>
<tr align="center">
<td>\(\beta_1\)</td>
<td><input class="w3-input" type="text" id="ex2_b1" value="1"></td>
</tr>
<tr align="center">
<td>\(\beta_2\)</td>
<td><input class="w3-input" type="text" id="ex2_b2" value="2"></td>
</tr>
<tr align="center">
<td>\(\beta_3\)</td>
<td><input class="w3-input" type="text" id="ex2_b3" value="3"></td>
</tr>
<tr align="center">
<td>\(\beta_4\)</td>
<td><input class="w3-input" type="text" id="ex2_b4" value="4"></td>
</tr>
</table>
<table class="w3-opacity" style="width:300px;display:inline;margin-right:25px;font-size:20px;">
<col width="50px">
<col width="60px">
<col width="60px">
<col width="60px">
<col width="60px">
<tr align="center">
<td colspan="5"><strong>Covariance \(C\)</strong></td>
</tr>
<tr align="center">
<th></th>
<th>\(x_1\)</th>
<th>\(x_2\)</th>
<th>\(x_3\)</th>
<th>\(x_4\)</th>
</tr>
<tr align="center">
<td>\(x_1\)</td>
<td><input class="w3-input" type="text" id="ex2_cor11" value="1"></td>
<td><input class="w3-input" type="text" id="ex2_cor12" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor13" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor14" value="0"></td>
</tr>
<tr align="center">
<td>\(x_2\)</td>
<td><input class="w3-input" type="text" id="ex2_cor21" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor22" value="1"></td>
<td><input class="w3-input" type="text" id="ex2_cor23" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor24" value="0"></td>
</tr>
<tr align="center">
<td>\(x_3\)</td>
<td><input class="w3-input" type="text" id="ex2_cor31" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor32" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor33" value="1"></td>
<td><input class="w3-input" type="text" id="ex2_cor34" value="0"></td>
</tr>
<tr align="center">
<td>\(x_4\)</td>
<td><input class="w3-input" type="text" id="ex2_cor41" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor42" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor43" value="0"></td>
<td><input class="w3-input" type="text" id="ex2_cor44" value="1"></td>
</tr>
</table>
<table class="w3-opacity" style="width:300px;display:inline;margin-right:25px;font-size:20px;">
<col width="60px">
<col width="60px">
<tr align="center">
<td colspan="2"><strong>Explicand</strong></td>
</tr>
<tr align="center">
<td colspan="2"><strong>Sample</strong></td>
</tr>
<tr align="center">
<td>\(x_1\)</td>
<td><input class="w3-input" type="text" id="ex2_x1" value="1"></td>
</tr>
<tr align="center">
<td>\(x_2\)</td>
<td><input class="w3-input" type="text" id="ex2_x2" value="1"></td>
</tr>
<tr align="center">
<td>\(x_3\)</td>
<td><input class="w3-input" type="text" id="ex2_x3" value="1"></td>
</tr>
<tr align="center">
<td>\(x_4\)</td>
<td><input class="w3-input" type="text" id="ex2_x4" value="1"></td>
</tr>
</table>
<br><br>
<table class="w3-opacity" style="width:300px;display:inline;margin-right:25px;font-size:20px;">
<col width="180px">
<col width="80px">
<col width="80px">
<col width="80px">
<col width="80px">
<tr align="center">
<td></td>
<td>\(\phi_1\)</td>
<td>\(\phi_2\)</td>
<td>\(\phi_3\)</td>
<td>\(\phi_4\)</td>
</tr>
<tr align="center">
<td><strong>CE Shapley Values</strong></td>
<td id="ex2_CE_phi1">1</td>
<td id="ex2_CE_phi2">2</td>
<td id="ex2_CE_phi3">3</td>
<td id="ex2_CE_phi4">4</td>
</tr>
<tr align="center">
<td><strong>ICE Shapley Values</strong></td>
<td id="ex2_ICE_phi1">1</td>
<td id="ex2_ICE_phi2">2</td>
<td id="ex2_ICE_phi3">3</td>
<td id="ex2_ICE_phi4">4</td>
</tr>
</table>
<br><br>
<input type="button" value="Preset A" class="w3-button w3-teal" style="font-size:20px;" id="ex2_presetA" onclick="ex2_presetA()">
<input type="button" value="Preset B" class="w3-button w3-green" style="font-size:20px;" id="ex2_presetB" onclick="ex2_presetB()">
<input type="button" value="Preset C" class="w3-button w3-green" style="font-size:20px;" id="ex2_presetC" onclick="ex2_presetC()">
<input type="button" value="Preset D" class="w3-button w3-green" style="font-size:20px;" id="ex2_presetD" onclick="ex2_presetD()">
<p class="w3-opacity w3-center" align="center" id="ex2_preset_text" style="margin:20px">Independent variables.</p>
</div>
<!-- Proof -->
<div style="background-color:aliceblue;">
<div style="padding-left:10px;padding-right:10px">
<p class="w3-justify"><a onclick="hideshow('proof_linearmodel')"><strong>Computing CE and ICE SHAP values Proof (Click)</strong></a></p>
<div id="proof_linearmodel", style="display:none">
<p class="w3-opacity w3-justify">
Computing SHAP values for a linear model is much easier than for other model classes. This is how we compute them for <a href="#example2">Example 2</a>.
</p>
<p class="w3-opacity w3-justify">
First, to compute Interventional Conditional Expectation (ICE) SHAP values for a linear model, we can start with:
$$
\begin{aligned}
\phi_i(f,x)&= \sum_{S\in C } W(|S|,|N|)(\mathbb{E}_{D}[f(x)|do(x_{S\cup \{i\}})] {-} \mathbb{E}_{D}[f(x)|do(x_{S})])\\
&=\sum_{S\in C } W(|S|,|N|)(\mathbb{E}_{D}[\beta x|do(x_{S\cup \{i\}})] {-} \mathbb{E}_{D}[\beta x|do(x_{S})])\\
&=\sum_{S\in C } W(|S|,|N|) \beta (\mathbb{E}_{D}[x|do(x_{S\cup \{i\}})] {-} \mathbb{E}_{D}[x|do(x_{S})])\\
&=\sum_{S\in C } W(|S|,|N|) \beta (h^{S\cup \{i\}} {-} h^S)\\
&=\sum_{S\in C } W(|S|,|N|) \beta_i x^f_i\\
&= \beta_i x^f_i\\
\end{aligned}
$$
</p>
<p class="w3-opacity w3-justify">
Second, to compute Conditional Expectation (CE) SHAP values for a linear model, we assume the background distribution \(D\) is multivariate normal with zero mean and covariance \(C\), we can start with:
$$
\begin{aligned}
\phi_i(f,x)&= \sum_{S\in C } W(|S|,|N|)(\mathbb{E}_{D}[f(x)|x_{S\cup \{i\}}] {-} \mathbb{E}_{D}[f(x)|x_{S}])\\
&= \sum_{S\in C } W(|S|,|N|)(\mathbb{E}_{D}[\beta x|x_{S\cup \{i\}}] {-} \mathbb{E}_{D}[\beta x|x_{S}])\\
&= \sum_{S\in C } W(|S|,|N|)\beta (\mathbb{E}_{D}[x|x_{S\cup \{i\}}] {-} \mathbb{E}_{D}[x|x_{S}])\\
\end{aligned}
$$
Here, we know that \(\mathbb{E}_{D}[x|x_{S}])=C_{N\setminus S,S} C_{S,S}^{-1} x_{S}\).
</p>
</div>
</div>
</div>
<!-- Background distribution -->
<h3 class="w3-justify" id="background_distribution">1.3 Single reference SHAP values</h3>
<p class="w3-opacity w3-justify">
As we saw earlier, to compute \(\phi_i(f,x^f)\) we evaluate the interventional conditional expectation. However, this depends on a <i>background distribution</i> \(D\) that the sample we are explaining, otherwise known as the foreground sample \(x^f\), will be compared to.
</p>
<p class="w3-opacity w3-justify">
One natural definition of the background distribution is a uniform distribution over a population sample. For instance, in machine learning, you could assign equal probability to every sample in your training set. With this background distribution, we can re-write the SHAP value as an average of <i>single reference SHAP values</i> (<a href="#chen2019explaining">Chen et. al. 2019</a>):
$$
\phi_i(f,x^f)=\frac{1}{|D|}\sum_{x^b\in D}\phi_i(f,x^f,x^b)
$$
This is in part because the interventional conditional expectation has a very natural definition when the background distribution is a single sample \(x^b\) (\(D_{x^b}\)):
$$
\mathbb{E}_{D_{x^b}}[f(x^f)|\text{do}(x_{S})]=h^S
$$
Where \(h^S\in\mathbb{R}^d\) with \(h^S_i=x^f_i\) if \(i\in S\) otherwise \(h^S_i=x^b_i\). In words, the interventional conditional expectation of \(x^f\) given a set of features \(S\) and \(x^b\) is a hybrid sample where the features in \(S\) are from \(x^f\) and the remaining features are from \(x^b\). This is as if <i>we intervene on features in the foreground sample with features from the background sample</i>.
<!-- $$
\mathbb{E}_\mathcal{D_{x^b}}[f(x)|\text{do}(x_{S})]=\mathcal{X}(x^f,x^b,S)
$$
Where \(\mathcal{X}(x,x^b,S)\) to return a hybrid sample \(h\) where the features in \(S\) are from \(x\) and the remaining features are from \(x^b\). In words, the interventional conditional expectation of \(x\) given a set of features \(S\), is a hybrid sample where the features in \(S\) are from \(x\) and the remaining features are from \(x^b\). This is as if <i>we intervene on features in the foreground sample with features from the background sample</i>. -->
</p>
<!-- Proof -->
<div style="background-color:aliceblue;">
<div style="padding-left:10px;padding-right:10px">
<p class="w3-justify"><a onclick="hideshow('proof_backgrounddist')"><strong>Single reference SHAP values Proof (Click)</strong></a></p>
<div id="proof_backgrounddist", style="display:none">
<p class="w3-opacity w3-justify">
Define \(C\) to be all combinations of the set \(N \setminus \{i\}\) and \(P\) to be all permutations of \(N \setminus \{i\}\). Starting with the definition of SHAP values:
$$
\begin{aligned}
\phi_i(f,x^f)&= \sum_{S\in C } W(|S|,|N|)(\mathbb{E}_{D}[f(x^f)|\text{do}(x_{S\cup \{i\}})] {-} \mathbb{E}_{D}[f(x^f)|\text{do}(x_{S})])\\
&=\frac{1}{|P|}\sum_{S\subseteq P} \mathbb{E}_D[f(x^f)|\text{do}(x_{S \cup \{i\}})] {-} \mathbb{E}_D[f(x^f)|\text{do}(x_{S})]\\
&= \frac{1}{|P|}\sum_{S\subseteq P}\frac{1}{|D|}\sum_{x^b\in D} f(h^{S\cup \{i\}}) {-} f(h^{S})\\
&= \frac{1}{|D|}\sum_{x^b\in D} \frac{1}{|P|}\sum_{S\subseteq P} f(h^{S\cup \{i\}}) {-} f(h^{S})\\
&= \frac{1}{|D|}\sum_{x^b\in D} \underbrace{\sum_{S\subseteq C} W(|S|,|N|)f(h^{S\cup \{i\}}) {-} f(h^{S})}_{\text{Single reference SHAP value}}\\
&=\frac{1}{|D|}\sum_{x^b\in D}\phi_i(f,x^f,x^b)
\end{aligned}
$$
</p>
</div>
</div>
</div>
<p class="w3-opacity w3-justify">
In summary, we reduce the problem of obtaining \(\phi_i(f,x^f)\) to an average of simpler problems \(\phi_i(f,x^f,x^b)\) where our foreground sample \(x^f\) is compared to a distribution with only one background sample \(x^b\). This new problem formulation will prove to be an easy problem to tackle for tree models.
</p>
</div>
<!-- The Algorithm Section -->
<div class="w3-container w3-content w3-center w3-padding-16" style="max-width:800px">
<h2 class="w3-justify" id="algorithm">2. Algorithm</h2>
<p class="w3-opacity w3-justify">
Now our goal is to tackle the simpler problem of obtaining single reference SHAP values \(\phi_i(f,x^f,x^b)\) that are attributions for a single foreground (sample being explained) and background sample (sample being compared to). In this section, we consider a specific foreground sample, background sample, and tree as specified in <a href="#example3">Example 3</a>.
</p>
<div id="table_container" style="overflow-x:auto;align:center;">
<p id="example3" class="w3-opacity w3-left" align="left" style="margin:20px">
<strong>Example 3</strong>: Choose \(x^f\), \(x^b\), and tree parameters for the remainder of this section.
</p>
<table class="w3-center" style="width:300px;display:inline;margin-right:25px;font-size:18px;">
<col width="100px"/>
<col width="150px"/>
<col width="50px"/>
<col width="150px"/>
<col width="50px"/>
<col width="50px"/>
<tr class="w3-opacity">
<th>Variable</th>
<th colspan="2">Foreground Sample \(x^f\)</th>
<th colspan="2">Background Sample \(x^b\)</th>
<th></th>
</tr>
<tr>
<td class="w3-opacity">\(x_1\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="fx1"></td>
<td class="w3-opacity" align="left" id="fx1_out"></td>
<td><input type="range" min="-15" max="15" value="10" class="slider" id="bx1"></td>
<td class="w3-opacity" align="left" id="bx1_out"></td>
<td></td>
</tr>
<tr>
<td class="w3-opacity">\(x_2\)</td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="fx2"></td>
<td class="w3-opacity" align="left" id="fx2_out"></td>
<td><input type="range" min="-15" max="15" value="10" class="slider" id="bx2"></td>
<td class="w3-opacity" align="left" id="bx2_out"></td>
<td><input type="button" value="Reset" class="w3-button w3-green" style="font-size:20px;" onclick="ex3_reset()"> </td>
</tr>
<tr>
<td class="w3-opacity">\(x_3\)</td>
<td><input type="range" min="-15" max="15" value="10" class="slider" id="fx3"></td>
<td class="w3-opacity" align="left" id="fx3_out"></td>
<td><input type="range" min="-15" max="15" value="0" class="slider" id="bx3"></td>
<td class="w3-opacity" align="left" id="bx3_out"></td>
<td></td>
</tr>
</table>
<br><br>
<p class="w3-opacity w3-center" style="line-height:0;font-size:18px">
<strong>Tree Parameters* (Click nodes)</strong> <br>
</p>
<div id="graphic"></div>
<div class="tooltip" id="tooltip"></div>
<p class="w3-opacity w3-center" style="margin:-30px 0px 30px 0px;font-size:18px">
*Green links indicate the foreground sample goes down a particular split, red indicates the background sample does, and blue indicates both samples do.
</p>
</div>
<!-- Brute force -->
<h3 class="w3-justify" id="brute_force">2.1 Brute force</h3>
<p class="w3-opacity w3-justify">
Based on the proof in <a href="#background_distribution">Section 1.3</a>, the brute force approach would be to compute the following:
$$
\phi_i(f,x^f,x^b)=\sum_{S\subseteq N\setminus\{i\}} \underbrace{W(|S|,|N|)}_{W}\underbrace{f(h^{S\cup \{i\}})}_{\text{\textcolor{green}{Pos} term}} {-} \underbrace{f(h^S)}_{\text{\textcolor{red}{Neg} term}})
$$
</p>
<p class="w3-opacity w3-justify">
If we assume the computational cost of computing the weight \(W\) is constant, then the complexity of the brute force method is the number of terms in the summation multiplied by the cost of making a prediction (on the order of the depth of the tree \((D)\)). Then, the computational complexity of the brute force approach is \(O(D\times2^{d})\).
</p>
<p class="w3-opacity w3-justify">
In order to compute \(\phi_i(f,x^f,x^b)\) for all features, we have to re-run the entire algorithm \(d\) times, giving us a complexity of \(O(d\times D\times 2^{d})\).
</p>
<div id="table_container" style="overflow-x:auto;align:center;font-size:18px">
<p class="w3-opacity w3-left" align="left" style="margin:20px">
<strong>Example 4</strong>: Brute force algorithm for the tree and samples specified in Example 3.
</p>
</div>
<div id="table_container" style="overflow-x:auto;align:center">
<div class="container">
<div class="first" style="width:460px;height:300px">
<p class="w3-opacity w3-center" style="margin: 0px 0px 0px 20px;font-size:18px;"><strong>Tree Parameters</strong></p>
<div id="ex4_divtree" style="margin-top:10px;"></div>
</div>
<div class="second" style="width:300px;height:300px">
<p class="w3-opacity w3-center" style="margin: 0px 0px 0px -100px;font-size:18px;"><strong>Foreground & background sample</strong></p>
<table class="w3-opacity" cellpadding="10" style="margin: 10px 0px 0px -10px">
<col width="40px"/>
<col width="50px"/>
<col width="50px"/>
<tr>
<th></th>
<th>\(x_1\)</th>
<th>\(x_2\)</th>
<th>\(x_3\)</th>
</tr>
<tr>
<td>\(x^f\)</td>
<td id="ex4_fx1">0</td>
<td id="ex4_fx2">0</td>
<td id="ex4_fx3">10</td>
</tr>
<tr>
<td>\(x^b\)</td>
<td id="ex4_bx1">10</td>
<td id="ex4_bx2">10</td>
<td id="ex4_bx3">0</td>
</tr>
<tr id="ex4_hS">
<td>\(h^S\)</td>
<td id="ex4_hs1"></td>
<td id="ex4_hs2"></td>
<td id="ex4_hs3"></td>
</tr>
<tr id="ex4_hSi">
<td>\(h^{S\cup i}\)</td>
<td id="ex4_hsi1"></td>
<td id="ex4_hsi2"></td>
<td id="ex4_hsi3"></td>
</tr>
</table>
</div>
<div class="clear"></div>
<div class="container">
<div class="first" style="width:460px;height:280px">
<p class="w3-opacity w3-center" style="margin: 0px 0px 0px 20px;font-size:18px;"><strong>Brute Force Values</strong></p>
<table class="w3-opacity" cellpadding="10" style="margin: 10px 0px 0px 90px">
<col width="40px"/>
<col width="50px"/>
<col width="50px"/>
<col width="60px"/>
<col width="60px"/>
<thead>
<tr>
<th>\(W\)</th>
<th>\(S\)</th>
<th id="ex4_fxS">\(f(x_S)\)</th>
<th>\(S\cup{i}\)</th>
<th id="ex4_fxSi">\(f(x_{S\cup{i}})\)</th>
</tr>
</thead>
<tbody>
<tr id="ex4_s1_row">
<td>1/3</td>
<td id="s1"></td>
<td id="s1_val"></td>
<td id="s1i"></td>
<td id="s1i_val"></td>
</tr>
<tr id="ex4_s2_row">
<td>1/6</td>
<td id="s2"></td>
<td id="s2_val"></td>
<td id="s2i"></td>
<td id="s2i_val"></td>
</tr>
<tr id="ex4_s3_row">
<td>1/6</td>
<td id="s3"></td>
<td id="s3_val"></td>
<td id="s3i"></td>
<td id="s3i_val"></td>
</tr>
<tr id="ex4_s4_row">
<td>1/3</td>
<td id="s4"></td>
<td id="s4_val"></td>
<td id="s4i"></td>
<td id="s4i_val"></td>
</tr>
</tbody>
</table>
</div>
<div class="second" style="width:300px;height:280px">
<p class="w3-opacity w3-center" style="margin: 0px 0px 0px -110px;font-size:18px;"><strong>Attribution Values</strong></p>
<table class="w3-opacity" cellpadding="10" style="margin: 10px 0px 0px -10px">
<col width="60px"/>
<col width="80px"/>
<thead>
<!-- <tr style="background-color:#8BC34A;"> -->
<tr id="ex4_phi1">
<td>\(\phi_1(f,x^f,x^b)\)</td>
<td id="ex4_phi1_val">0</td>
</tr>
<tr id="ex4_phi2">
<td>\(\phi_2(f,x^f,x^b)\)</td>
<td id="ex4_phi2_val">0</td>
</tr>
<tr id="ex4_phi3">
<td>\(\phi_3(f,x^f,x^b)\)</td>
<td id="ex4_phi3_val">0</td>
</tr>
</thead>
</table>
</div>
</div>
<input style="font-size:20px;" type="button" class="w3-button w3-green" value="<<" onclick="ex4_reset()">
<input style="font-size:20px;" type="button" class="w3-button w3-green" value=">" onclick="bruteForceStep()">
<input style="font-size:20px;" type="button" class="w3-button w3-green" value=">>" onclick="bruteForceRunAll()">
<br><br>
<div class="clear"></div>
</div>
</div>
<p class="w3-opacity w3-justify">
However, if we <i>constrain \(f(x)\) to be a tree-based model</i> (e.g., XGBoost, decision trees, random forests, etc.), then we can come up with a polynomial time algorithm to compute \(\phi_i(f,x^f,x^b)\) exactly. Why is this the case? Well, looking at Example 4, we can see that even for explaining a single feature, the brute force algorithm may consider a particular path multiple times. However, to compute the SHAP value for a single feature, it turns out that we only need to consider each path once. This insight leads us to the naive algorithm in <a href="#naive_implementation">Section 2.2</a>.
</p>
<!-- -->
<!-- Naive Implementation -->
<!-- -->
<h3 class="w3-justify" id="naive_implementation">2.2 Naive Implementation</h3>
<p class="w3-opacity w3-justify">
Before we get into the algorithm, we first describe a theorem that is the basis for this naive implementation.
</p>
<p class="w3-opacity w3-justify">
<strong>Theorem 1: To calculate \(\phi_i(f,x,x^b)\), we can calculate attributions for each path from the root to each leaf.</strong> For a given path \(P\), we define \(N_P\) to be the "unique" features encountered and \(S_P\) to be the "unique" features that came from \(x\). Finally, define \(v\) to be the value of the path's leaf. Then, the attribution of the path is:
$$
\phi_i^P(f,x,x^b)=
\begin{cases}
0 & \text{if}\ i\notin N_P \\
\textcolor{green}{W(|S_P|-1,|N_P|)\times v} & \text{if}\ i\in S_P \\
\textcolor{red}{-W(|S_P|,|N_P|)\times v} & \text{o.w.}
\end{cases}
$$
</p>
<!-- Proof -->
<div style="background-color:aliceblue;">
<div style="padding-left:10px;padding-right:10px">
<p class="w3-justify"><a onclick="hideshow('proof')"><strong>Theorem 1 Sketch of Proof (Click)</strong></a></p>
<div id="proof", style="display:none">
<p class="w3-opacity w3-justify">
If we treat each path in the tree from the root to the leaf as a separate model \(f'(x)\) that returns the value of the leaf if that path is traversed by \(x\) or zero otherwise, then we have \(L\) models that operate on disjoint parts of the input space. Then, \(f(x)=\sum f'(x)\) and by the additivity of SHAP values, \(\phi_i(f,x,x^b)=\sum_f\phi_i(f',x,x^b)\). Then, we can simply calculate \(\phi_i\) for each path model. Since the path model is zero everywhere except for the associated path, it is easy to arrive to the solution in Theorem 1.
</p>
</div>
</div>
</div>
<p class="w3-opacity w3-justify">
Then the goal of the algorithm is to obtain \(N_P\) and \(S_P\) for each path by recursively traversing the tree. We will start by explaining the algorithm via an example:
</p>
<div id="table_container" style="overflow-x:auto;">
<figure>
<figcaption class="w3-opacity">Figure 3: Green paths are associated with \(\textcolor{green}{x^f}\), red paths are \(\textcolor{red}{x^b}\), and blue paths are associated with both.</figcaption>
<br>
<img src="images/tree_example3.png" alt="Tree Example 3" width="360">
</figure>
</div>
<p class="w3-opacity w3-justify">
In the naive algorithm, we maintain lists \(N_P\) and \(S_P\) as we traverse the tree. At each internal node (Cases 2-4) we update the lists and then pass them to the node's children. At the leaf nodes (Case 1), we calculate the attribution for each path. In Figure 3, we see four possible cases:
<ul class="w3-opacity w3-justify">
<li>Case 1: \(n\) is a leaf</li>
<ul>
<li>Return the attribution in Theorem 1 based on \(N_P\) and \(S_P\)</li>
</ul>
<li>Case 2: The feature has been encountered already (\(n_{feature}\in N_P\))</li>
<ul>
<li>Depending on if we split on \(x^f\) or \(x^b\), we compare either \(x^f_{n_{feature}}\) or \(x^b_{n_{feature}}\) to \(n_{threshold}\) and go down the appropriate child</li>
<li>Pass down \(N_P\) and \(S_P\) without modifications because we did not add a new feature</li>
</ul>
<li>Case 3: Both \(x\) and \(x^b\) are on the same side of \(n\)'s split</li>
<ul>
<li>Pass down \(N_P\) and \(S_P\) without modifications because relative to \(x\) and \(x^b\) it's as if this node doesn't exist</li>
</ul>
<li>Case 4: \(x\) and \(x^b\) go to different children</li>
<ul>
<li>Add \(n_{feature}\) to both \(N_P\) and \(S_P\) and pass both lists to the \(x\) child</li>
<li>Only add \(n_{feature}\) to \(N_P\) and pass both lists to the \(x^b\) child</li>
</ul>
</ul>
</p>
<div id="table_container" style="overflow-x:auto;align:center;font-size:18px">
<p class="w3-opacity w3-left" align="left" style="margin:20px">
<strong>Example 5</strong>: Naive algorithm for the tree and samples from Example 3.
</p>
</div>
<div id="table_container" style="overflow-x:auto;align:center">
<p class="w3-opacity w3-center" style="margin: 0px 0px 0px 20px;font-size:18px;"><strong>Tree Parameters</strong></p>
<div id="ex5_divtree" style="margin-top:10px;"></div>
</div>
<div id="table_container" style="overflow-x:auto;align:center">