-
Notifications
You must be signed in to change notification settings - Fork 8
/
Network.cpp
executable file
·163 lines (146 loc) · 5.48 KB
/
Network.cpp
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
//
// Created by tituskc on 4/14/17.
//
#include "Network.h"
#include <random>
#include <iostream>
#include <algorithm>
#include <boost/foreach.hpp>
#include <fstream>
#include <math.h>
#define foreach BOOST_FOREACH
Network::Network(unsigned long N, double p, double A, double theta, double gamma, bool generatePlots)
: N(N), p(p), generatePlots(generatePlots) {
initializeAdjacencyMatrix();
initiateNetwork(A, theta, gamma);
}
Network::~Network() {};
void Network::initializeAdjacencyMatrix() {
std::default_random_engine generator;
generator.seed((unsigned long) rand());
std::uniform_real_distribution<double> distribution(0.0, 1.0);
inverseAdjacencyMatrix.resize(N, std::vector<int>(N, 0));
prunedAdjacencyMatrix.resize(N, std::vector<int>(N, 0));
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
if (i != j && distribution(generator) > p)
inverseAdjacencyMatrix[i][j] = 1;
int v = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
v = inverseAdjacencyMatrix[i][j] * (1 - inverseAdjacencyMatrix[j][i]);
if (v == 1) {
prunedAdjacencyMatrix[i][j] = v;
links += 1;
}
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
inverseAdjacencyMatrix[i][j] = prunedAdjacencyMatrix[j][i];
}
}
}
void Network::initiateNetwork(double A, double theta, double gamma) {
double e_bk = links == 0 ? 0 : theta * A / links;
std::string prefix = "B";
banks = std::vector<Bank>(N);
for (int i = 0; i < N; ++i) {
Bank bank = Bank(prefix + std::to_string(i));
bank.interbankBorrowing =
accumulate(prunedAdjacencyMatrix[i].begin(), prunedAdjacencyMatrix[i].end(), 0) * e_bk;
bank.interbankAssets = accumulate(inverseAdjacencyMatrix[i].begin(), inverseAdjacencyMatrix[i].end(), 0) * e_bk;
double excessAssets = A / N - bank.interbankAssets;
bank.externalAssets = excessAssets >= 0 ? excessAssets : 0;
bank.capital = (bank.interbankAssets + bank.externalAssets) * gamma;
bank.customerDeposits = bank.externalAssets + bank.interbankAssets - bank.capital - bank.interbankBorrowing;
banks[i] = bank;
}
if(e_bk>0)
entropy += e_bk*links*log(e_bk);
}
std::vector<Bank> Network::getBanks() {
return banks;
}
static double min(const double a, const double b) {
return a <= b ? a : b;
}
static double max(const double a, const double b) {
return a >= b ? a : b;
}
void Network::simulateShock(int pos, double shock, bool isInitial) {
Bank shockedBank = banks[pos];
if (shockedBank.defaults) {
return;
} else {
if (isInitial)
banks[pos].externalAssets -= shock;
else if (shockedBank.capital > 0)
banks[pos].interbankAssets -= shock;
banks[pos].affected = true;
double resCapShock = shock - shockedBank.capital;
banks[pos].capital = max(shockedBank.capital-shock, 0);
banks[pos].visits = shockedBank.visits + 1;
if (resCapShock>0) {
banks[pos].defaults = true;
failures += 1;
double resInterBankShock = resCapShock - shockedBank.interbankBorrowing;
banks[pos].interbankBorrowing = max(shockedBank.interbankBorrowing-resCapShock, 0);
if (resInterBankShock > 0) {
banks[pos].customerDeposits = max(shockedBank.customerDeposits-resInterBankShock, 0);
marketLoss += shockedBank.customerDeposits-banks[pos].customerDeposits;
}
if (generatePlots) writeNetworkData();
std::vector<int> expVec = getExposureVector(prunedAdjacencyMatrix[pos], pos);
int k = (int) expVec.size();
double toTransmit = min(resCapShock, shockedBank.interbankBorrowing);
if (toTransmit > 0 && k > 0) {
foreach (int expPos, expVec) {
simulateShock(expPos, toTransmit / k, false);
}
}
} else if (generatePlots) writeNetworkData();
}
}
std::vector<int> Network::getExposureVector(std::vector<int> expVec, int pos) {
std::vector<int> positions;
for (int i = 0; i < N; ++i) {
if (i != pos && expVec[i] != 0 && !banks[i].defaults) {
positions.push_back(i);
}
}
return positions;
}
std::vector<std::vector<int>> Network::getPrunedAdjacencyMatrix() {
return prunedAdjacencyMatrix;
}
int Network::getFailures() {
return failures;
}
void Network::writeNetworkData() {
std::ofstream myfile;
myfile.open("csvfiles/bankData" + std::to_string(index++) + ".csv");
myfile << BankHeader;
std::vector<Bank> banks = getBanks();
for (int i = 0; i < N; ++i) {
Bank b = banks[i];
myfile << b.name << COMMA << b.externalAssets << COMMA
<< b.interbankAssets << COMMA << b.interbankBorrowing << COMMA
<< b.customerDeposits << COMMA << b.capital << COMMA << b.getAssets()
<< COMMA << b.getLiabilities() << COMMA << (b.affected ? 'Y' : 'N') << "\n";
}
myfile.close();
}
void Network::writeMetaData(double initialShock) {
std::ofstream myfile;
myfile.open("csvfiles/metaData.csv");
myfile << "index,shock\n";
myfile << index << COMMA << initialShock << "\n";
myfile.close();
}
double Network::getMarketLoss() {
return marketLoss;
}
double Network::getEntropy(){
return entropy;
}