Simple Interbank Network Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This contains the actual implementation of a simple systemic risk modeller. | |
# It is essentially a network based computational model of systemic risk. | |
__author__="stuart" | |
__date__ ="$22 Jan 2014 9:19:08 PM$" | |
import csv | |
import random | |
from GraphDataStructure import Graph | |
from GraphDataStructure import Node | |
import math | |
class Bank(Node): | |
assets = 0 | |
deposits = 0 | |
totalShock = 0 | |
def __init__(self, n, a, d): | |
Node.__init__(self, n) | |
self.assets = a | |
self.deposits = d | |
self.totalShock = 0 | |
return | |
def getShock(self, shock): | |
if shock >= 0.025: | |
networkEffect = len(self.neighbours) + 1 | |
self.totalShock = self.totalShock + shock / networkEffect | |
for neighbour in self.neighbours: | |
neighbour.getShock(shock / networkEffect) | |
return | |
else: | |
self.totalShock = self.totalShock + shock | |
class InterbankNetwork(Graph): | |
numLinks = 0 | |
def __init__(self, banks): | |
Graph.__init__(self, banks) | |
def constructNetwork(self): | |
numBanks = len(self.Nodes) | |
self.numLinks = int(numBanks + random.random() * (numBanks * numBanks)) | |
for i in range(self.numLinks): | |
x = int(random.random() * numBanks) | |
y = int(random.random() * numBanks) | |
if x == y: | |
y = y - 1 | |
self.addTwoWayLink(self.Nodes[x].name, self.Nodes[y].name) | |
return | |
def printNetwork(self): | |
for bank in self.Nodes: | |
bank.printNeighbours() | |
def analyzeNetwork(self): | |
totalShocked = 1 | |
maxShock = 0.0 | |
minShock = 1.0 | |
for bank in self.Nodes: | |
if bank.totalShock > 0.0: | |
totalShocked = totalShocked + 1 | |
if bank.totalShock > maxShock: | |
maxShock = bank.totalShock | |
if bank.totalShock < minShock: | |
minShock = bank.totalShock | |
return Result(self.numLinks, totalShocked, maxShock, minShock) | |
def startShock(self, impacts): | |
numBanks = len(self.Nodes) | |
for i in range(impacts): | |
x = int(random.random() * numBanks) | |
self.Nodes[x].getShock(1.0) | |
def resetNetwork(self): | |
for bank in self.Nodes: | |
bank.totalShock = 0.0 | |
bank.neighbours = [] | |
class Result: | |
numLinks = 0 | |
totalShocked = 1 | |
maxShock = 0.0 | |
minShock = 1.0 | |
def __init__(self, nl, ts, max, min): | |
self.numLinks = nl | |
self.totalShocked = ts | |
self.maxShock = max | |
self.minShock = min |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment