-
Notifications
You must be signed in to change notification settings - Fork 1
/
MC_produce_22122019.jl
285 lines (261 loc) · 10.2 KB
/
MC_produce_22122019.jl
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
using Distributions
using LinearAlgebra
using Statistics
using Random
using BenchmarkTools
using CSV, DataFrames
using HypothesisTests
include("Shapley.jl")
### HELPER FUNCTIONS
# (d+1)x(d+1) correlation matrix with uniform correlations c
M0(c,d) = [Float64(c) + (i == j)*(1-Float64(c)) for i in 1:(d+1), j in 1:(d+1)]
wish(c, d, df) = Wishart(df, M0(c,d))
W0(wdist) = rand(wdist, 1)[1]
# Get summary statistics about CI width and coverage
function CI_measures(within, width)
if (mean(within) == 0)
covrCI = (0,0)
elseif (mean(within) == 1)
covrCI = (1,1)
else
covrCI = confint(BinomialTest(within))
end
widthCI = quantile!(width, [0.025, 0.975])
return mean(within), covrCI[1], covrCI[2], mean(width),
std(width), widthCI[1], widthCI[2]
end
# Function to save results to csv
function save_sim_csv(sim, type, d_)
CSV.write("results/$d_/sim$type/coverage.csv", DataFrame(sim[1]), writeheader=false)
CSV.write("results/$d_/sim$type/covrgCI_L.csv", DataFrame(sim[2]), writeheader=false)
CSV.write("results/$d_/sim$type/covrgCI_U.csv", DataFrame(sim[3]), writeheader=false)
CSV.write("results/$d_/sim$type/width_mu.csv", DataFrame(sim[4]), writeheader=false)
CSV.write("results/$d_/sim$type/width_sd.csv", DataFrame(sim[5]), writeheader=false)
CSV.write("results/$d_/sim$type/widthCI_L.csv", DataFrame(sim[6]), writeheader=false)
CSV.write("results/$d_/sim$type/widthCI_U.csv", DataFrame(sim[7]), writeheader=false)
end
# Simulate N shapley values and variances, with sample size n
function shapley_simulate(dist, n, N; vals = 1, α = 0.05)
shap, avar = [], []
for i in 1:N
Zᵢ = rand(dist, n)'
shapᵢ, avarᵢ = calc_shapley(Zᵢ, vals = vals)
push!(shap, shapᵢ)
push!(avar, avarᵢ)
end
CIℓ, CIu = calc_CIs(shap, avar, n; α = α)
return shap, avar, CIℓ, CIu
end
# Simulation calculates N shapley values and their bootsrap CIs,
# using a sample size n and number of resamples Nb
function bootshap_simulate(dist, n, N, Nb; vals = 1, α = 0.05)
shap = Vector{Float64}()
CIℓ = Vector{Float64}()
CIu = Vector{Float64}()
for i in 1:N
Zᵢ = rand(dist, n)'
shapᵢ = just_shapley(Zᵢ, vals = vals)
push!(shap, shapᵢ)
bootshap = Vector{Float64}()
for r in 1:Nb
Zᵢᵣ = Zᵢ[sample(1:n, n),:]
push!(bootshap, just_shapley(Zᵢᵣ; vals = vals))
end
not_nan = map(x -> !isnan(x), bootshap)
CIℓᵢ, CIuᵢ = quantile!(bootshap[not_nan], [α/2, 1-α/2])
push!(CIℓ, CIℓᵢ)
push!(CIu, CIuᵢ)
end
return shap, CIℓ, CIu
end
# Simulate N shapley values and variances, with sample size n
# but use a different covariance matrix each time, so this
# function needs to calculate true shapley values too
function shapley_simulate_wish(n, N, c, d, wdf; vals = 1, α = 0.05)
shap, avar, v = [], [], []
for i in 1:N
dist = mvn(W0(wish(c,d,wdf)))
Zᵢ = rand(dist, n)'
shapᵢ, avarᵢ = calc_shapley(Zᵢ, vals = vals)
push!(shap, shapᵢ)
push!(avar, avarᵢ)
push!(v, true_shapley(dist; vals = vals))
end
CIℓ, CIu = calc_CIs(shap, avar, n; α = α)
return shap, avar, v, CIℓ, CIu
end
# Simulation calculates N shapley values and their bootsrap CIs,
# using a sample size n and number of resamples Nb
function bootshap_simulate_wish(n, N, c, d, wdf, Nb; vals = 1, α = 0.05)
shap = Vector{Float64}()
CIℓ = Vector{Float64}()
CIu = Vector{Float64}()
v = Vector{Float64}()
for i in 1:N
dist = mvn(W0(wish(c,d,wdf)))
Zᵢ = rand(dist, n)'
shapᵢ = just_shapley(Zᵢ, vals = vals)
push!(shap, shapᵢ)
bootshap = Vector{Float64}()
for r in 1:Nb
Zᵢᵣ = Zᵢ[sample(1:n, n),:]
push!(bootshap, just_shapley(Zᵢᵣ; vals = vals))
end
not_nan = map(x -> !isnan(x), bootshap)
CIℓᵢ, CIuᵢ = quantile!(bootshap[not_nan], [α/2, 1-α/2])
push!(CIℓ, CIℓᵢ)
push!(CIu, CIuᵢ)
push!(v, true_shapley(dist; vals = vals))
end
return shap, CIℓ, CIu, v
end
##################################################
#### ASYMPTOTIC RESULT simulations
##################################################
function vary_nc_shapley_simulate(n, c)
nn, nc = length(n), length(c)
coverage, covrCIℓ, covrCIu = zeros(nn, nc), zeros(nn, nc), zeros(nn, nc)
width_μ, width_sd = zeros(nn, nc), zeros(nn, nc)
widthCIℓ, widthCIu = zeros(nn, nc), zeros(nn, nc)
for nᵢ in 1:nn, cⱼ in 1:nc
shap, avar, v, CIℓ, CIu = shapley_simulate_wish(n[nᵢ], N, c[cⱼ], d, wdf; α = α)
width = CIu .- CIℓ
within = CIℓ .< v .< CIu
measures = CI_measures(within, width)
coverage[nᵢ, cⱼ] = measures[1]
covrCIℓ[nᵢ, cⱼ] = measures[2]
covrCIu[nᵢ, cⱼ] = measures[3]
width_μ[nᵢ, cⱼ] = measures[4]
width_sd[nᵢ, cⱼ] = measures[5]
widthCIℓ[nᵢ, cⱼ] = measures[6]
widthCIu[nᵢ, cⱼ] = measures[7]
end
return coverage, covrCIℓ, covrCIu,
width_μ, width_sd, widthCIℓ, widthCIu
end
function vary_nc_shapley_simulate(n, c, dist_func, cov_func)
nn, nc = length(n), length(c)
coverage, covrCIℓ, covrCIu = zeros(nn, nc), zeros(nn, nc), zeros(nn, nc)
width_μ, width_sd = zeros(nn, nc), zeros(nn, nc)
widthCIℓ, widthCIu = zeros(nn, nc), zeros(nn, nc)
for nᵢ in 1:nn, cⱼ in 1:nc
dist = dist_func(cov_func(c[cⱼ], d))
v = true_shapley(dist)
shap, avar, CIℓ, CIu = shapley_simulate(dist, n[nᵢ], N; vals = 1, α = α)
width = CIu .- CIℓ
within = CIℓ .< v .< CIu
measures = CI_measures(within, width)
coverage[nᵢ, cⱼ] = measures[1]
covrCIℓ[nᵢ, cⱼ] = measures[2]
covrCIu[nᵢ, cⱼ] = measures[3]
width_μ[nᵢ, cⱼ] = measures[4]
width_sd[nᵢ, cⱼ] = measures[5]
widthCIℓ[nᵢ, cⱼ] = measures[6]
widthCIu[nᵢ, cⱼ] = measures[7]
end
return coverage, covrCIℓ, covrCIu,
width_μ, width_sd, widthCIℓ, widthCIu
end
##################################################
#### BOOTSTRAP simulations
##################################################
function vary_nc_bootshap_simulate(n, c)
nn, nc = length(n), length(c)
coverage, covrCIℓ, covrCIu = zeros(nn, nc), zeros(nn, nc), zeros(nn, nc)
width_μ, width_sd = zeros(nn, nc), zeros(nn, nc)
widthCIℓ, widthCIu = zeros(nn, nc), zeros(nn, nc)
for nᵢ in 1:nn, cⱼ in 1:nc
shap, CIℓ, CIu, v = bootshap_simulate_wish(n[nᵢ], N, c[cⱼ], d, wdf, Nb)
width = CIu .- CIℓ
within = CIℓ .< v .< CIu
measures = CI_measures(within, width)
coverage[nᵢ, cⱼ] = measures[1]
covrCIℓ[nᵢ, cⱼ] = measures[2]
covrCIu[nᵢ, cⱼ] = measures[3]
width_μ[nᵢ, cⱼ] = measures[4]
width_sd[nᵢ, cⱼ] = measures[5]
widthCIℓ[nᵢ, cⱼ] = measures[6]
widthCIu[nᵢ, cⱼ] = measures[7]
end
return coverage, covrCIℓ, covrCIu,
width_μ, width_sd, widthCIℓ, widthCIu
end
function vary_nc_bootshap_simulate(n, c, dist_func, cov_func)
nn, nc = length(n), length(c)
coverage, covrCIℓ, covrCIu = zeros(nn, nc), zeros(nn, nc), zeros(nn, nc)
width_μ, width_sd = zeros(nn, nc), zeros(nn, nc)
widthCIℓ, widthCIu = zeros(nn, nc), zeros(nn, nc)
for nᵢ in 1:nn, cⱼ in 1:nc
dist = dist_func(cov_func(c[cⱼ], d))
v = true_shapley(dist)
shap, CIℓ, CIu = bootshap_simulate(dist, n[nᵢ], N, Nb)
width = CIu .- CIℓ
within = CIℓ .< v .< CIu
measures = CI_measures(within, width)
coverage[nᵢ, cⱼ] = measures[1]
covrCIℓ[nᵢ, cⱼ] = measures[2]
covrCIu[nᵢ, cⱼ] = measures[3]
width_μ[nᵢ, cⱼ] = measures[4]
width_sd[nᵢ, cⱼ] = measures[5]
widthCIℓ[nᵢ, cⱼ] = measures[6]
widthCIu[nᵢ, cⱼ] = measures[7]
end
return coverage, covrCIℓ, covrCIu,
width_μ, width_sd, widthCIℓ, widthCIu
end
##################################################
#### PARAMETERS and DISTRIBUTIONS
##################################################
# Parameters
const d = 3 # features
const tdf = 100 # degrees of freedom for t-distribution
const wdf = 100 # degrees of freedom for Wishart distribution
const α = 0.05 # significance
const N = 1000 # iterations
const nᵥ = 100:100:5000 # sample sizes
const nₛ = 5:5:100 # small sample sizes
const cᵥ = vcat(collect(0.0:0.1:0.9), 0.99) # correlations
# Multivariate normal distribution with mean 0 and covariance matrix M
mvn(M) = MvNormal(M)
# Multivariate t distribution
mvt(df, M) = MvTDist(df, M)
mvt(M) = MvTDist(tdf, M)
##################################################
#### ASYMPTOTIC RESULT simulations
##################################################
#### MC STUDY A (Normal)
simA = vary_nc_shapley_simulate(nᵥ, cᵥ, mvn, M0)
simA_small = vary_nc_shapley_simulate(nₛ, cᵥ, mvn, M0)
save_sim_csv(simA, "A", "22122019")
save_sim_csv(simA_small, "As", "22122019")
#### MC STUDY B (t-distribution)
simB = vary_nc_shapley_simulate(nᵥ, cᵥ, mvt, M0)
simB_small = vary_nc_shapley_simulate(nₛ, cᵥ, mvt, M0)
save_sim_csv(simB, "B", "22122019")
save_sim_csv(simB_small, "Bs", "22122019")
#### MC STUDY C (Wishart normal)
simC = vary_nc_shapley_simulate(nᵥ, cᵥ)
simC_small = vary_nc_shapley_simulate(nₛ, cᵥ)
save_sim_csv(simC, "C", "22122019")
save_sim_csv(simC_small, "Cs", "22122019")
##################################################
#### BOOTSTRAP simulations
##################################################
const Nb = 1000 # bootstrap resamples
const nbᵥ = 100:100:2000 # sample sizes
const cbᵥ = [0,0.1,0.2,0.3,0.6,0.9,0.99] # correlations
#### MC STUDY A (Normal)
simA_b = vary_nc_bootshap_simulate(nbᵥ, cbᵥ, mvn, M0)
simA_small_b = vary_nc_bootshap_simulate(nₛ, cbᵥ, mvn, M0)
save_sim_csv(simA_b, "A_b", "22122019")
save_sim_csv(simA_small_b, "As_b", "22122019")
#### MC STUDY B (t-distribution)
simB_b = vary_nc_bootshap_simulate(nbᵥ, cbᵥ, mvt, M0)
simB_small_b = vary_nc_bootshap_simulate(nₛ, cbᵥ, mvt, M0)
save_sim_csv(simB_b, "B_b", "22122019")
save_sim_csv(simB_small_b, "Bs_b", "22122019")
#### MC STUDY C (Wishart normal)
simC_b = vary_nc_bootshap_simulate(nbᵥ, cbᵥ)
simC_small_b = vary_nc_bootshap_simulate(nₛ, cbᵥ)
save_sim_csv(simC_b, "C_b", "22122019")
save_sim_csv(simC_small_b, "Cs_b", "22122019")