-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimulator.R
More file actions
200 lines (174 loc) · 6.53 KB
/
Simulator.R
File metadata and controls
200 lines (174 loc) · 6.53 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
################################
# Author: Bulent Ozel
# e-mail: bulent.ozel@gmail.com
# Collaborators:
# Mario Eboli
# Andrea Teglio
# Andrea Toto
#################################
#################################
# Importnat Note: In order to use these drivers ModelLibrary.R needs to be loaded.
#################################
# Simulation Templates :::::::::::::::
# b.sheet.equity can be one of:
# - "homo", denoting that each bank has the same and equal equity, which is E0/N.
# - "hetero", denoting that equity of the bank depends on phi, eps and network position.
# - "planA.spec", holds only when K = 1, for K > 1 and it switches to "homo" configuration.
# shock.type can be one of:
# - "assets", the set of external assets is permuted for a shock sequence.
# Each bank owns a distinct subset of assets.
# - "banks", the set of banks in the system is permuted for a shock sequence.
# Set of all assets of a bank which is picked is incrementally hit before moving to the
# next bank in the sequence.
# - "full", the set of banks in the system is permuted for a shock sequence.
# All assets of a bank which is picked all at once.
connectivity.and.centralization <- function(N,
KMin = 1,
KMax = N,
rCentMin = 0,
rCentMax = N,
symmetric = 1,
eps = 0.1,
phi = 0.4,
A0 = 100,
E0 = 10,
b.sheet.equity = "hetero",
d.min = 1,
nRuns = N,
shock.type = "banks",
unit.shock = 1.0,
r.shock = 1.0,
n.sources = N) {
for (K in KMin:KMax) {
for (c in rCentMin:rCentMax) {
cent.level = c / N
### 1: Configure and create the network structure
##: First a K regular graph is cretaed. Then keeping the connectivity constant
##: the network is centralized to the desired level.
res <- centralize(N, K, cent.level, symm = symmetric)
net <- res$centralized
Centralization <- net %n% "Centralization"
Connectivity <- net %n% "Connectivity"
### 2: Initialize balancesheets
if (b.sheet.equity == "hetero") {
results <- initialize.balanceSheets(net, A0, phi, eps)
}
else if (b.sheet.equity == "homo") {
results <-
initialize.balanceSheets.planA.v1(net, A0, E0, d.min)
}
else if (b.sheet.equity == "planA.spec") {
if (K == 1) {
results <- initialize.balanceSheets.planA.v2(net, A0, E0, d.min)
}
else {
results <- initialize.balanceSheets.planA.v1(net, A0, E0, d.min)
}
}
else {
results <- initialize.balanceSheets(net, A0, phi, eps)
}
BS <- results[[1]]
net <- results[[2]]
defaults <- vector(mode = "numeric")
for (run in 1:nRuns) {
### 3: Initialize shock vector
shocks <- initialize.shockVector(r.shock,
n.sources,
net,
BS,
shock.size = unit.shock,
shock.type = shock.type)
### 4: Apply shocks and simulate the contagion
results <- simulate.contagion(net, BS, shocks)
### 5: Report results
if (INSPECT_MODE){
Contagion.State <- results[[1]]
}
Contagion.Process <- results[[2]]
outputs <- getContagionResults(Contagion.Process, N)
nPrimary <- outputs[[1]]
nSecondary <- outputs[[2]]
if (SCAN) {
Tk <- outputs[[3]]
Tf <- outputs[[4]]
outputs <-
as.vector(c(
Centralization,
Connectivity,
nPrimary,
nSecondary,
Tf,
Tk
))
}
else {
T1 <- outputs[[3]]
T2 <- outputs[[4]]
dT <- outputs[[5]]
outputs <-
as.vector(c(
dT,
T1,
T2,
Centralization,
Connectivity,
nPrimary,
nSecondary
))
}
defaults <- c(defaults, outputs)
}
### 5: Report results
if (SCAN) {
Out.m <- matrix(defaults, ncol = (5 + N), byrow = TRUE)
Out <- as.data.frame(Out.m)
ncolnames <-
c("Centralization",
"Connectivity",
"nPrimary",
"nSecondary",
"Tfin")
for (i in 1:N) {
ncolnames <- c(ncolnames, paste("T", i, sep = ""))
}
colnames(Out) <- ncolnames
}
else {
Out.m <- matrix(defaults, ncol = 7, byrow = TRUE)
Out <- as.data.frame(Out.m)
colnames(Out) <-
c("dT",
"T1",
"T2",
"Centralization",
"Connectivity",
"nPrimary",
"nSecondary")
#assign(paste("Out","_N",N,"_K",K,"_c",c, sep=""), Out)
}
fname = paste("Out", "_N", N, "_K", K, "_c", c, sep = "")
cap.text = paste("Results.")
create.Table(Out,
name = fname,
type = "csv",
cap = cap.text)
if (INSPECT_MODE){
fname = paste("BS", "_N", N, "_K", K, "_c", c, sep = "")
cap.text = paste("Balance Sheets.")
create.Table(BS,
name = fname,
type = "csv",
cap = cap.text)
fname = paste("Net", "_N", N, "_K", K, "_c", c, sep = "")
plot.Network.v2(net, f.name = fname)
fname = paste("Diffusion", "_N", N, "_K", K, "_c", c, sep = "")
cap.text = paste("Final diffusion state of the latest run.")
create.Table(Contagion.State,
name = fname,
type = "csv",
cap = cap.text)
}
}
}
}