-
Notifications
You must be signed in to change notification settings - Fork 7
/
julia.jl
331 lines (274 loc) · 9.54 KB
/
julia.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
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
#############################################################################
# Flux models
#############################################################################
abstract type AbstractDifferentiableJuliaModel <: AbstractDifferentiableModel end
using Flux
# Constructor
struct FluxModel <: Models.AbstractDifferentiableJuliaModel
model::Any
likelihood::Symbol
function FluxModel(model, likelihood)
if likelihood ∈ [:classification_binary,:classification_multi]
new(model, likelihood)
else
throw(ArgumentError("`type` should be in `[:classification_binary,:classification_multi]`"))
end
end
end
# Outer constructor method:
function FluxModel(model; likelihood::Symbol=:classification_binary)
FluxModel(model, likelihood)
end
# Methods
using SliceMap
function logits(M::FluxModel, X::AbstractArray)
return SliceMap.slicemap(x -> M.model(x), X, dims=[1,2])
end
function probs(M::FluxModel, X::AbstractArray)
if M.likelihood == :classification_binary
output = σ.(logits(M, X))
elseif M.likelihood == :classification_multi
output = softmax(logits(M, X))
end
return output
end
### Deep Ensemble
struct FluxEnsemble <: Models.AbstractDifferentiableModel
model::Any
likelihood::Symbol
end
# Outer constructor method:
function FluxEnsemble(model; likelihood::Symbol=:classification_binary)
FluxEnsemble(model, likelihood)
end
using Statistics, MLUtils
using SliceMap
function logits(M::FluxEnsemble, X::AbstractArray)
sum(map(nn -> SliceMap.slicemap(x -> nn(x), X, dims=[1,2]),M.model))/length(M.model)
end
using MLUtils
function probs(M::FluxEnsemble, X::AbstractArray)
if M.likelihood == :classification_binary
output = sum(map(nn -> SliceMap.slicemap(x -> σ.(nn(x)), X, dims=[1,2]),M.model))/length(M.model)
elseif M.likelihood == :classification_multi
output = sum(map(nn -> SliceMap.slicemap(x -> softmax(nn(x)), X, dims=[1,2]),M.model))/length(M.model)
end
return output
end
#############################################################################
# Laplace Redux
#############################################################################
using Flux, LaplaceRedux
# Constructor
struct LaplaceReduxModel <: Models.AbstractDifferentiableJuliaModel
model::Laplace
likelihood::Symbol
function LaplaceReduxModel(model, likelihood)
if likelihood == :classification_binary
new(model, likelihood)
elseif likelihood==:classification_multi
throw(ArgumentError("`type` should be `:classification_binary`. Support for multi-class Laplace Redux is not yet implemented."))
else
throw(ArgumentError("`type` should be in `[:classification_binary,:classification_multi]`"))
end
end
end
# Outer constructor method:
function LaplaceReduxModel(model; likelihood::Symbol=:classification_binary)
LaplaceReduxModel(model, likelihood)
end
# Methods
logits(M::LaplaceReduxModel, X::AbstractArray) = M.model.model(X)
probs(M::LaplaceReduxModel, X::AbstractArray)= LaplaceRedux.predict(M.model, X)
#############################################################################
# Gradient Boosted Trees
#############################################################################
using EvoTrees
struct EvoTreeModel <: Models.AbstractDifferentiableJuliaModel
model::EvoTrees.GBTree
likelihood::Symbol
function EvoTreeModel(model, likelihood)
if likelihood ∈ [:classification_binary,:classification_multi]
new(model, likelihood)
else
throw(ArgumentError("`type` should be in `[:classification_binary,:classification_multi]`"))
end
end
end
# Outer constructor method:
function EvoTreeModel(model; likelihood::Symbol=:classification_binary)
EvoTreeModel(model, likelihood)
end
# Methods
function logits(M::EvoTreeModel, X::AbstractArray)
p = probs(M, X)
if M.likelihood == :classification_binary
output = log.(p./(1 .- p))
else
output = log.(p)
end
return output
end
function probs(M::EvoTreeModel, X::AbstractArray{<:Number, 2})
output = EvoTrees.predict(M.model, X')'
if M.likelihood == :classification_binary
output = reshape(output[2,:],1,size(output,2)) # binary case
end
return output
end
function probs(M::EvoTreeModel, X::AbstractArray{<:Number, 1})
X = reshape(X, 1,length(X))
output = EvoTrees.predict(M.model, X)'
if M.likelihood == :classification_binary
output = reshape(output[2,:],1,size(output,2)) # binary case
end
return output
end
function probs(M::EvoTreeModel, X::AbstractArray{<:Number, 3})
output = SliceMap.slicemap(x -> probs(M,x), X, dims=[1,2])
return output
end
#############################################################################
# Baseline classifiers for illustrative purposes
#############################################################################
# -------- Linear Logistic Model:
"""
LogisticModel(W::Matrix,b::AbstractArray)
Constructs a logistic classifier based on arrays containing coefficients `w` and constant terms `b`.
# Examples
```julia-repl
w = [1.0 -2.0] # estimated coefficients
b = [0] # estimated constant
M = CounterfactualExplanations.Models.LogisticModel(w, b);
```
See also:
- [`logits(M::LogisticModel, X::AbstractArray)`](@ref)
- [`probs(M::LogisticModel, X::AbstractArray)`](@ref)
"""
struct LogisticModel <: Models.AbstractDifferentiableJuliaModel
W::Matrix
b::AbstractArray
likelihood::Symbol
end
LogisticModel(W,b;likelihood=:classification_binary) = LogisticModel(W,b,likelihood)
using MLUtils
# What follows are the two required outer methods:
"""
logits(M::LogisticModel, X::AbstractArray)
Computes logits as `WX+b`.
# Examples
```julia-repl
using CounterfactualExplanations.Models
w = [1.0 -2.0] # estimated coefficients
b = [0] # estimated constant
M = LogisticModel(w, b);
x = [1,1]
logits(M, x)
```
See also [`LogisticModel(W::Matrix,b::AbstractArray)`](@ref).
"""
function logits(M::LogisticModel, X::AbstractArray)
if ndims(X) == 3
n = size(X,3)
reshape(map(x -> (M.W*x .+ M.b)[1], [X[:,:,i] for i ∈ 1:n]),1,1,n)
# SliceMap.slicemap(x -> M.W*x .+ M.b, X, dims=(1,2))
else
M.W*X .+ M.b
end
end
"""
probs(M::LogisticModel, X::AbstractArray)
Computes predictive probabilities from logits as `σ(WX+b)` where 'σ' is the [sigmoid function](https://en.wikipedia.org/wiki/Sigmoid_function).
# Examples
```julia-repl
using CounterfactualExplanations.Models
w = [1.0 -2.0] # estimated coefficients
b = [0] # estimated constant
M = LogisticModel(w, b);
x = [1,1]
probs(M, x)
```
See also [`LogisticModel(W::Matrix,b::AbstractArray)`](@ref).
"""
probs(M::LogisticModel, X::AbstractArray) = Flux.σ.(logits(M, X))
# -------- Bayesian model:
"""
BayesianLogisticModel(μ::Matrix,Σ::Matrix)
Constructs a Bayesian logistic classifier based on maximum a posteriori (MAP) estimates `μ` (coefficients including constant term(s)) and `Σ` (covariance matrix).
# Examples
```julia-repl
using Random, LinearAlgebra
Random.seed!(1234)
μ = [0 1.0 -2.0] # MAP coefficients
Σ = Symmetric(reshape(randn(9),3,3).*0.1 + UniformScaling(1.0)) # MAP covariance matrix
M = CounterfactualExplanations.Models.BayesianLogisticModel(μ, Σ);
```
See also:
- [`logits(M::BayesianLogisticModel, X::AbstractArray)`](@ref)
- [`probs(M::BayesianLogisticModel, X::AbstractArray)`](@ref)
"""
struct BayesianLogisticModel <: Models.AbstractDifferentiableJuliaModel
μ::Matrix
Σ::Matrix
likelihood::Symbol
BayesianLogisticModel(μ, Σ, likelihood) = length(μ)^2 != length(Σ) ? throw(DimensionMismatch("Dimensions of μ and its covariance matrix Σ do not match.")) : new(μ, Σ, likelihood)
end
BayesianLogisticModel(μ,Σ;likelihood=:classification_binary) = BayesianLogisticModel(μ,Σ,likelihood)
# What follows are the three required outer methods:
"""
logits(M::BayesianLogisticModel, X::AbstractArray)
Computes logits as `μ[1ᵀ Xᵀ]ᵀ`.
# Examples
```julia-repl
using CounterfactualExplanations.Models
using Random, LinearAlgebra
Random.seed!(1234)
μ = [0 1.0 -2.0] # MAP coefficients
Σ = Symmetric(reshape(randn(9),3,3).*0.1 + UniformScaling(1.0)) # MAP covariance matrix
M = BayesianLogisticModel(μ, Σ);
x = [1,1]
logits(M, x)
```
See also [`BayesianLogisticModel(μ::Matrix,Σ::Matrix)`](@ref)
"""
function logits(M::BayesianLogisticModel, X::AbstractArray)
if !isa(X, AbstractMatrix)
X = reshape(X, length(X), 1)
end
X = vcat(ones(size(X)[2])', X) # add for constant
return M.μ * X
end
"""
probs(M::BayesianLogisticModel, X::AbstractArray)
Computes predictive probabilities using a Probit approximation.
# Examples
```julia-repl
using CounterfactualExplanations.Models
using Random, LinearAlgebra
Random.seed!(1234)
μ = [0 1.0 -2.0] # MAP coefficients
Σ = Symmetric(reshape(randn(9),3,3).*0.1 + UniformScaling(1.0)) # MAP covariance matrix
M = BayesianLogisticModel(μ, Σ);
x = [1,1]
probs(M, x)
```
See also [`BayesianLogisticModel(μ::Matrix,Σ::Matrix)`](@ref)
"""
function probs(M::BayesianLogisticModel, X::AbstractArray)
μ = M.μ # MAP mean vector
Σ = M.Σ # MAP covariance matrix
# Inner product:
z = logits(M, X)
# Probit approximation
if !isa(X, AbstractMatrix)
X = reshape(X, length(X), 1)
end
X = vcat(ones(size(X)[2])', X) # add for constant
v = [X[:,n]'Σ*X[:,n] for n=1:size(X)[2]]
κ = 1 ./ sqrt.(1 .+ π/8 .* v) # scaling factor for logits
z = κ' .* z
# Compute probabilities
p = Flux.σ.(z)
p = size(p)[2] == 1 ? vec(p) : p
return p
end