-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize_data_flux.jl
308 lines (254 loc) · 8.6 KB
/
optimize_data_flux.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
# ## load packages
# using Pkg
# Pkg.add("DataFrames")
# Pkg.add("XLSX")
# Pkg.add("Missings")
# Pkg.add("NLsolve")
# Pkg.add("Complementarity")
# Pkg.add("NLopt")
# Pkg.add("JuMP")
# Pkg.add("Ipopt")
# Pkg.add("SpecialFunctions")
# Pkg.add("ForwardDiff")
# Pkg.add("AmplNLWriter")
# Pkg.add("Cbc")
# Pkg.add("FFTW")
# Pkg.build("FFTW")
# ENV["GRDIR"]="" ; Pkg.build("GR")
# Pkg.add("PATHSolver")
# Pkg.build("PATHSolver")
# Pkg.add("SLEEFPirates")
# Pkg.build("SLEEFPirates")
# Pkg.add("IterTools")
# Pkg.add("Tracker")
# Pkg.add("Zygote")
# Pkg.add("SpecialFunctions")
# Pkg.add("DataFrames")
# Pkg.add("XLSX")
# Pkg.add("Missings")
# Pkg.add("NLsolve")
# Pkg.add("Complementarity")
# Pkg.add("NLopt")
# Pkg.add("JuMP")
# Pkg.add("Ipopt")
# Pkg.add("ForwardDiff")
# Pkg.add("GraphRecipes")
# Pkg.add("Gadfly")
# Pkg.add("LightGraphs")
# Pkg.add("SimpleWeightedGraphs")
# Pkg.add("CUDA")
# Pkg.add("CuArrays")
# Pkg.add("Distributed")
# Pkg.add("Surrogates")
# Pkg.add("QuadGK")
# Pkg.add("DistributionsAD")
# Pkg.add("StatsFuns")
using LinearAlgebra, DataFrames, XLSX, Missings
using Distributions, DistributionsAD, Random
using CUDA
using Flux
using Flux.CUDA
using Flux: cpu, gpu
using Flux: @epochs
using Test
# using GraphRecipes
# using Plots
# using Gadfly
# using LightGraphs
# using SimpleWeightedGraphs
# using ForwardDiff
# using NLsolve, Complementarity
# using NLopt
# using JuMP
# using Ipopt
# using Zygote
# using StatsFuns
# using IterTools: ncycle
# CUDA.allowscalar(false)
#Pkg.add(PackageSpec(url="https://github.com/FluxML/Flux.jl", rev="master"))
#= network primitives from the data
p_bar = total liabilities
c = outside assets
assets = total assets
w = net worth
b = outside liabilities
=#
## load data
xf = XLSX.readxlsx("node_stats_forsimulation_all.xlsx")
data = vcat( [(XLSX.eachtablerow(xf[s]) |> DataFrames.DataFrame) for s in XLSX.sheetnames(xf)]... )
unique!(data) # delete duplicate rows, use `nonunique(data)' to see if there are any duplicates
#data[(isequal.(data.tkr,"JPM") .| isequal.(data.tkr,"AIG")) .& isequal.(data.qt_dt,192),:]
#data = data[isless.(0,data.w), :]
data = data[isequal.(data.qt_dt,192), :] # keep quarter == 195 = 2008q4
sort!(data, :assets, rev = true)
data = data[1:3,:] # keep small number of nodes, for testing
N = size(data,1) # number of nodes
# rescale units
units = 1e6;
data[:,[:w, :c, :assets, :p_bar, :b]] .= data[!,[:w, :c, :assets, :p_bar, :b]]./units
# fake missing data to make problem feasible
data.b[:] .= missing
data.c[:] .= missing
# create network variables
#data.f = data.p_bar .- data.b # inside liabilities
#data.d = data.assets .- data.c # inside assets
# keep track of missing variables
col_with_miss = names(data)[[any(ismissing.(col)) for col = eachcol(data)]] # columns with at least one missing
#data_nm = dropmissing(data, disallowmissing=true) # drop missing
data_nm = coalesce.(data, data.w .+ 0.01) # replace missing by a value
nm_c = findall(x->x==0,ismissing.(data.c))
nm_b = findall(x->x==0,ismissing.(data.b))
dropmissing(data, [:delta, :delta_alt, :w, :assets, :p_bar]) # remove type missing
# take a look
names(data) # column names
describe(data)
show(data, true)
# function shock(c)
# # draw shocks that produce the probability of default δ
# a = 1
# b = log.(data.delta)./(log.(1 .-data.w./c))
# dist = Beta.(a,[b...])
# draws = rand.(dist, 1)
# vcat(draws...).*c
# #rand(Float64,N) # normal
# end
## Optimization
# parameters
CUDA.allowscalar(true)
temp = convert(Array{Float32},data[:,[:delta, :delta_alt, :w, :assets, :p_bar]])
delta = copy(temp[:,1]); #delta = gpu(delta)
delta_alt = copy(temp[:,2]); #delta_alt = gpu(delta_alt)
w= copy(temp[:,3]); #w = gpu(w)
assets= copy(temp[:,4]); #assets = gpu(assets)
p_bar = copy(temp[:,5]); #p_bar = gpu(p_bar)
g0 = Float32[0.0f0] #|> gpu # bankruptcy cost
rng = Random.seed!(123)
dist = Beta(2,2)
dist = convert(Beta{Float32},dist) #|> gpu
x = convert(Array{Float32},rand.(dist, 1000000)) #|> gpu
σA = convert(Array{Float32},rand(rng,N,N)) #|> gpu;
σA[diagind(σA)] .= 0
σb = fill(0.0f0,N) #|> gpu
σc = fill(0.0f0,N) #|>gpu
## fixed point
function contraction(p,x)
A = (NNlib.hardtanh.(σA).+cu(1))./cu(2)
b = NNlib.sigmoid.(σb).*p_bar
c = NNlib.sigmoid.(σc).*assets
min.(p_bar, max.((1 .+g0).*(A'*p .+ c .- x.*c) .- g0.*p_bar,0))
end
contraction_iter(x, n::Integer) = n <= 0 ? p_bar : contraction(contraction_iter(x,n-1),x)
α=1 ;β=2;
betapdf(x) = x^(α - 1) * (1 - x)^(β - 1)
#loss_x(x) = -sum(contraction_iter(x,10)).*exp.(logpdf.(dist,x))
loss_x(x) = -sum(contraction_iter(x,10)).*betapdf(x)
# Δx = 0.01
# gridx = 0:Δx:1
# function loss_int(σA,σb,σc)
# A = (NNlib.hardtanh.(σA).+1)./2
# b = NNlib.sigmoid.(σb).*p_bar
# c = NNlib.sigmoid.(σc).*assets
# sum(loss_x.(gridx,Ref(σA),Ref(σb),Ref(σc)).*exp.(logpdf.(dist,gridx))*Δx )
# end
# σb = CuArrays.fill(0.0f0,N)
# σc = CUDA.fill(0.0f0,N)
# function test!(gg)
# for i=1:length(gg)
# gg[i] = 2.0f0
# end
# return
# end
# @cuda test!(σc)
# @cuda test!(σb)
# cpus = length(Sys.cpu_info())
# gpus = length(devices())
# CUDA.memory_status()
# versioninfo(verbose=true)
function penalty(x)
A = (NNlib.hardtanh.(σA).+1)./2
b = NNlib.sigmoid.(σb).*p_bar
c = NNlib.sigmoid.(σc).*assets
con1 = sum(A,dims=2).*p_bar .- p_bar .+ b
con2 = A' * p_bar .- assets .+ c
con3 = sum(LowerTriangular(A).*LowerTriangular(transpose(A)))
sum(abs2,con1)+sum(abs2,con2)+abs(con3)
end
function lossFlux(x)
loss_x(x) + penalty(x)
end
ps = Flux.params(σA,σb,σc)
opt = Flux.ADAM(0.1)
cbP = function ()
l = penalty(x)
println("Current loss is: $l")
end
cbP()
@epochs 100 Flux.train!(penalty, ps , x, opt, cb = cbP)
for d in x
display(d )
end
Zygote.gradient(()->penalty(x),σb)
ps = Params(ps)
for d in xx
gs = Zygote.gradient(ps) do
training_loss = penalty(d...)
# Code inserted here will be differentiated, unless you need that gradient information
# it is better to do the work outside this block.
return training_loss
end
end
# Insert whatever code you want here that needs training_loss, e.g. logging.
# logging_callback(training_loss)
# Insert what ever code you want here that needs gradient.
# E.g. logging with TensorBoardLogger.jl as histogram so you can see if it is becoming huge.
#update!(opt, ps, gs)
# Here you might like to check validation set accuracy, and break out to do early stopping.
#end
Asol = (NNlib.hardtanh.(σA).+1)./2
bsol = NNlib.sigmoid.(σb).*p_bar
csol = NNlib.sigmoid.(σc).*assets
tol = 1e-6
@testset "check solution" begin
@test norm( sum(Asol,dims=2).* p_bar .- (p_bar .- bsol)) < tol
@test norm( Asol' * p_bar .- (assets .- csol)) < tol
@test norm(diag(Asol)) < tol
@test norm([Asol[i,j]*Asol[j,i] for i=1:N , j=1:N]) < tol
@test all(0 .<=Asol.<=1)
@test all(0 .<=bsol.<=p_bar)
@test all(0 .<=csol.<=assets)
end
Gadfly.spy(Asol)
# Pkg.add("JLD")
# using JLD
# save("/home/ec2-user/SageMaker/Test-AWS/net_opt.jld", "Asol", Asol,"data",data)
## plot
Aplot = deepcopy(Asol)
Aplot[Aplot.<1e-3] .=0
# attributes here: https://docs.juliaplots.org/latest/generated/graph_attributes/
#method `:spectral`, `:sfdp`, `:circular`, `:shell`, `:stress`, `:spring`, `:tree`, `:buchheim`, `:arcdiagram` or `:chorddiagram`.
graphplot(LightGraphs.DiGraph(Aplot),
nodeshape=:circle,
markersize = 0.05,
node_weights = assets,
markercolor = range(colorant"yellow", stop=colorant"red", length=N),
names = data.nm_short,
fontsize = 8,
linecolor = :darkgrey,
edgewidth = (s,d,w)->5*Asol[s,d],
arrow=true,
method= :circular, #:chorddiagram,:circular,:shell
)
## some tests
tol = 1e-6
x_test = fill(0.0,N,1)
pinf = nlsolve(p->contraction(p, x_test)-p, p_bar, autodiff = :forward)
@test norm(contraction_iter(x_test,100)-pinf.zero)<tol
Zygote.gradient(c -> -sum(contraction(p_bar,x_test)),data_nm.c)
ForwardDiff.gradient(c -> -sum(contraction(p_bar,x_test)),data_nm.c)
Zygote.gradient(c -> -sum(contraction_iter(x_test,20)),data_nm.c)
ForwardDiff.gradient(c -> -sum(contraction_iter(x_test,20)),data_nm.c)
Zygote.gradient(loss_x(x_test),data_nm.c)
Zygote.gradient(c -> -sum(contraction_iter(x0,3,c).*exp.(logpdf.(dist,0.0))),data_nm.c)
loss(data_nm.c)
zy=Zygote.gradient(loss, data_nm.c)
fd=ForwardDiff.gradient(loss, data_nm.c)