-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEstimated_SV.R
More file actions
288 lines (233 loc) · 11.6 KB
/
Copy pathEstimated_SV.R
File metadata and controls
288 lines (233 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Libraries -------------------------------------------------------------------------------------------------------
library(data.table)
library(stringr)
source("./shapr_functions.R")
# Functions -------------------------------------------------------------------------------------------------------
compute_SV_values <- function(X_now, dt_all_coalitions, dt_vS, shap_names) {
# Get the weight matrix
W_now <- weight_matrix(X = X_now, normalize_W_weights = TRUE)
# Use the pre-computed v(S) data and only extract the relevant rows (combinations)
dt_vS_now <- as.matrix(dt_vS[X_now[, id_combination_full], -"id_combination"])
# Compute the new Shapley values
dt_kshap <- data.table::as.data.table(t(W_now %*% dt_vS_now))
colnames(dt_kshap) <- c("none", shap_names)
return(dt_kshap)
}
create_X_dt_unique_and_paired <- function(m, presampled_coalitions, dt_all_coalitions, weight_zero_m = 10^6) {
# Find weights for given number of features
n_features <- seq(m - 1)
n <- sapply(n_features, choose, n = m)
w <- shapley_weights(m = m, N = n, n_features) * n
p <- w / sum(w)
# String version
# Insert all sampled coalitions into a data table and find their frequencies
dt_freq <- data.table::data.table(features = presampled_coalitions)[, .(shapley_weight = .N), by = features]
# Get the number of features in each coalition
dt_freq[, n_features := stringr::str_count(features, ",") + 1]
# Add the number of coalitions of each size
dt_freq[, N := n[n_features]]
dt_freq[, p := p[n_features]]
# Get the id_combination if we had used all combinations
dt_freq[, id_combination_full := dt_all_coalitions[dt_freq, id, on = "features"]]
# Convert from string to list of integer vectors. stringr is faster than base and stringi
dt_freq[, features := lapply(stringr::str_split(features, ","), as.integer)]
# Add the empty and grand coalitions
dt_freq <- rbindlist(
list(
data.table(
features = list(integer(0)),
shapley_weight = weight_zero_m,
n_features = 0L,
N = 1L,
p = NA,
id_combination_full = 1
),
dt_freq,
data.table(
features = list(1:m),
shapley_weight = weight_zero_m,
n_features = as.integer(m),
N = 1L,
p = NA,
id_combination_full = 2^m
)
)
)
data.table::setorder(dt_freq, "id_combination_full")
dt_freq[, id_combination := .I]
data.table::setcolorder(
dt_freq,
c("id_combination", "id_combination_full", "features", "n_features", "N", "shapley_weight", "p")
)
# Optional to match the old setup
dt_freq[, N := as.integer(N)]
dt_freq[, shapley_weight := as.integer(shapley_weight)]
dt_freq[, n_features := as.integer(n_features)]
return(dt_freq)
}
create_X_dt_PySHAP <- function(m,
presampled_coalitions,
prefixed_coalitions,
dt_all_coalitions,
weight_zero_m = 10^6,
version_scaled = TRUE) {
# Find weights for given number of features
n_features <- seq(m - 1)
n <- sapply(n_features, choose, n = m)
w <- shapley_weights(m = m, N = n, n_features) * n
p <- w / sum(w)
# Weight the different coalition sizes (PySHAP version)
num_subset_sizes <- as.integer(ceiling((m - 1) / 2))
num_paired_subset_sizes <- as.integer(floor((m - 1) / 2))
weight_vector <- sapply(seq(num_subset_sizes), function(i) (m - 1.0) / (i * (m - i)))
weight_vector[seq(num_paired_subset_sizes)] <- 2 * weight_vector[seq(num_paired_subset_sizes)]
weight_vector <- weight_vector / sum(weight_vector)
# Get the sampled coalitions for which we have to compute the frequencies
if (!is.null(prefixed_coalitions)) {
presampled_coal_wo_prefixed_coal <- presampled_coalitions[-seq(nrow(prefixed_coalitions))]
} else {
presampled_coal_wo_prefixed_coal <- presampled_coalitions
}
# String version
# Insert all sampled coalitions into a data table and find their frequencies
dt_freq <- data.table::data.table(features = presampled_coal_wo_prefixed_coal)[, .(shapley_weight = .N), by = features]
# Fix the weights according to the technique in PySHAP
if (version_scaled) {
if (is.null(prefixed_coalitions)) {
num_full_subsets <- 0
weight_left <- sum(weight_vector)
} else {
num_full_subsets <- length(prefixed_coalitions[.N - 1, features][[1]]) # This relies on the list version
weight_left <- sum(weight_vector[-seq(num_full_subsets)])
}
dt_freq[, shapley_weight := shapley_weight * weight_left / sum(shapley_weight)]
}
# Convert the list column to a comma-separated string for each row
if (!is.null(prefixed_coalitions)) {
prefixed_coalitions[, features := sapply(features, function(x) paste(unlist(x), collapse = ","))]
setnames(prefixed_coalitions, "w", "shapley_weight")
}
# Put together with the prefixed samples
dt_freq <- rbind(prefixed_coalitions, dt_freq)
# Get the number of features in each coalition
dt_freq[, n_features := stringr::str_count(features, ",") + 1]
# Add the number of coalitions of each size
dt_freq[, N := n[n_features]]
dt_freq[, p := p[n_features]]
# Get the id_combination if we had used all combinations
dt_freq[, id_combination_full := dt_all_coalitions[dt_freq, id, on = "features"]]
# Convert from string to list of integer vectors. stringr is faster than base and stringi
dt_freq[, features := lapply(stringr::str_split(features, ","), as.integer)]
# Add the empty and grand coalitions
dt_freq <- rbindlist(
list(
data.table(features = list(integer(0)), shapley_weight = weight_zero_m, n_features = 0L, N = 1L, p = NA, id_combination_full = 1),
dt_freq,
data.table(features = list(1:m), shapley_weight = weight_zero_m, n_features = as.integer(m), N = 1L, p = NA, id_combination_full = 2^m)
)
)
data.table::setorder(dt_freq, "id_combination_full")
dt_freq[, id_combination := .I]
data.table::setcolorder(dt_freq, c("id_combination", "id_combination_full", "features", "n_features", "N", "shapley_weight", "p"))
# dt_freq
# Optional to match the old setup
dt_freq[, N := as.integer(N)]
dt_freq[, n_features := as.integer(n_features)]
# plot(dt_freq[-c(1, .N), id_combination_full], dt_freq[-c(1, .N), shapley_weight])
#
# dt_freq[, sum(shapley_weight), by = N][-1]
return(dt_freq)
}
create_X_dt_PySHAPstar <- function(m,
presampled_coalitions,
prefixed_coalitions,
dt_all_coalitions,
weight_zero_m = 10^6) {
# Find weights for given number of features
n_features <- seq(m - 1)
n <- sapply(n_features, choose, n = m)
w <- shapley_weights(m = m, N = n, n_features) * n
p <- w / sum(w)
# Weight the different coalition sizes (PySHAP version)
num_subset_sizes <- as.integer(ceiling((m - 1) / 2))
num_paired_subset_sizes <- as.integer(floor((m - 1) / 2))
weight_vector <- sapply(seq(num_subset_sizes), function(i) (m - 1.0) / (i * (m - i)))
weight_vector[seq(num_paired_subset_sizes)] <- 2 * weight_vector[seq(num_paired_subset_sizes)]
weight_vector <- weight_vector / sum(weight_vector)
# Get the sampled coalitions for which we have to compute the frequencies
if (!is.null(prefixed_coalitions)) {
presampled_coal_wo_prefixed_coal <- presampled_coalitions[-seq(nrow(prefixed_coalitions))]
num_full_subsets <- length(prefixed_coalitions[.N - 1, features][[1]]) # This relies on the list version
weights_remaining <- weight_vector[-seq(num_full_subsets)]
weight_left <- sum(weights_remaining)
} else {
presampled_coal_wo_prefixed_coal <- presampled_coalitions
num_full_subsets <- 0
weights_remaining <- weight_vector
weight_left <- sum(weight_vector)
}
# Get the number of coalitions of all sizes
n_coal_of_each_size <- choose(m, seq(m - 1))
# Get the number of coalitions in the sizes that are not deterministically included
if (num_full_subsets >= floor(m / 2)) stop("Too many full subsets. No sampling is done.")
n_coal_of_each_size_reamaining <- n_coal_of_each_size[seq(num_full_subsets + 1, m - 1 - num_full_subsets)]
# Get the shapley kernel weights for the remaining coalition sizes
p_reamaining <- p[seq(num_full_subsets + 1, m - 1 - num_full_subsets)]
p_reamaining <- p_reamaining / sum(p_reamaining)
# Get the shapley kernel weight for each coalition
shapley_kernel_weight_reweighted <- p_reamaining / n_coal_of_each_size_reamaining
# Pad it such that index corresponds to caolition size
shapley_kernel_weight_reweighted <- c(rep(0, num_full_subsets), shapley_kernel_weight_reweighted, rep(0, num_full_subsets))
# Convert the list column to a comma-separated string for each row
if (!is.null(prefixed_coalitions)) {
prefixed_coalitions[, features := sapply(features, function(x) paste(unlist(x), collapse = ","))]
setnames(prefixed_coalitions, "w", "shapley_weight")
# Add that these coalitions are NOT sampled
prefixed_coalitions[, sampled := FALSE]
}
# String version
# Insert all sampled coalitions into a data table and find their frequencies
dt_freq <- data.table::data.table(features = presampled_coal_wo_prefixed_coal)[, .(shapley_weight = .N), by = features]
# Add that these coalitions are sampled
dt_freq[, sampled := TRUE]
# Put together with the prefixed samples
dt_freq <- rbind(prefixed_coalitions, dt_freq)
# Get the number of features in each coalition
dt_freq[, n_features := stringr::str_count(features, ",") + 1]
# Add the number of coalitions of each size
dt_freq[, N := n[n_features]]
dt_freq[, p := p[n_features]]
# Get the id_combination if we had used all combinations and sort it
dt_freq[, id_combination_full := dt_all_coalitions[dt_freq, id, on = "features"]]
data.table::setorder(dt_freq, "id_combination_full")
# Get the number of samples it took the sample the `dt_freq[sampled == TRUE, .N]` unique
# coalitions for those coalitions that are not deterministically included.
K <- dt_freq[sampled == TRUE, sum(shapley_weight)]
# Ensure that the shapley weight column is numeric, as it is int when prefixed_coalitions is NULL
dt_freq[, shapley_weight := as.numeric(shapley_weight)]
# Add the reweighted Shapley kernel weights
dt_freq[sampled == TRUE, shapley_weight := shapley_kernel_weight_reweighted[n_features]]
# Compute the corrected values shapley weights (see paper)
dt_freq[sampled == TRUE, shapley_weight := 2 * shapley_weight / (1 - (1 - 2 * shapley_weight)^(K / 2))]
# Reweight the weights such that they sums to the remaining weight
dt_freq[sampled == TRUE, shapley_weight := weight_left * shapley_weight / sum(shapley_weight)]
# Convert from string to list of integer vectors. stringr is faster than base and stringi
dt_freq[, features := lapply(stringr::str_split(features, ","), as.integer)]
# Remove the sampled column as we no longer need it
dt_freq[, sampled := NULL]
# Add the empty and grand coalitions
dt_freq <- rbindlist(
list(
data.table(features = list(integer(0)), shapley_weight = weight_zero_m, n_features = 0L, N = 1L, p = NA, id_combination_full = 1),
dt_freq,
data.table(features = list(1:m), shapley_weight = weight_zero_m, n_features = as.integer(m), N = 1L, p = NA, id_combination_full = 2^m)
)
)
# Create new id column and reorder the columns
dt_freq[, id_combination := .I]
data.table::setcolorder(dt_freq, c("id_combination", "id_combination_full", "features", "n_features", "N", "shapley_weight", "p"))
# Optional to match the old setup
dt_freq[, N := as.integer(N)]
dt_freq[, n_features := as.integer(n_features)]
return(dt_freq)
}