-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstitutions.py
134 lines (112 loc) · 5.04 KB
/
institutions.py
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
from collections import defaultdict
from economicsl import Agent
from contracts import Tradable, Other, Loan, AssetType
from constraints import BankLeverageConstraint
from behaviours import do_delever, sell_assets_proportionally
class DefaultException(Exception):
# In general, there are LIQUIDITY, SOLVENCY, FAILED_MARGIN_CALL
# In this model, we are restricting it to SOLVENCY only.
def __init__(self, me, typeOfDefault):
self.typeOfDefault = typeOfDefault
class Bank(Agent):
def __init__(self, name, simulation):
# __init__() is the standard Python method for initializing an object
super().__init__(name, simulation)
# `availableActions` is a dictionary (aka hash map in other
# languages) that has the action types (sell asset, pay loan)
# as its keys and list of actions as its values.
# It is reconstructed from scratch at every step.
# E.g. {SellAsset: [sellasset1, sellasset2],
# PayLoan: [payloan1, payloan2, payloan3]}
self.availableActions = {}
self.do_trigger_default = False
self.leverageConstraint = BankLeverageConstraint(self)
def initialize_balance_sheet(self, model, assetMarket, assets, liabilities):
cash, corp_bonds, gov_bonds, other_asset = assets
loan, other_liability = liabilities
self.model = model
# Asset side
self.add_cash(cash)
# a1. Corporate bonds
# Construct the contract
cb_contract = Tradable(
self, AssetType.CORPORATE_BONDS,
assetMarket, corp_bonds)
# Add the contract to balance sheet
self.add(cb_contract)
# Register the contract amount to asset market. This is to be able to
# compute the price impact, which is based on the total market
# capitalisation, later.
assetMarket.total_quantities[AssetType.CORPORATE_BONDS] += corp_bonds
# a2. Goverment bonds
gb_contract = Tradable(
self, AssetType.GOV_BONDS,
assetMarket, gov_bonds)
self.add(gb_contract)
assetMarket.total_quantities[AssetType.GOV_BONDS] += gov_bonds
# a3. Other asset
self.add(Other(self, None, other_asset))
# Liability side
# l1. Loan
self.add(Loan(None, self, loan))
# l2. Other liability
self.add(Other(None, self, other_liability))
# trigger_default, is_insolvent, get_available_actions, choose_actions,
# step, act, get_all_actions_of_type are standard functions of the
# institutions in the full model.
def trigger_default(self):
self.do_trigger_default = False
# Sell everything
sell_assets_proportionally(self)
def is_insolvent(self):
# This has a particular implementation of just taking leverage ratio
# into account. In general, this would include the solvency condition
# from risk-weighted capital ratio.
return self.leverageConstraint.is_insolvent()
def get_available_actions(self):
# defaultdict is a convenient dictionary that
# automatically creates an entry
eligibleActions = defaultdict(list)
ldg = self.get_ledger()
for contract in (ldg.get_all_assets() + ldg.get_all_liabilities()):
if contract.is_eligible(self):
action = contract.get_action(self)
eligibleActions[type(action)].append(action)
return eligibleActions
def choose_actions(self):
# 0) If I'm insolvent, default.
if self.is_insolvent():
raise DefaultException(self, 'SOLVENCY')
do_delever(self)
def step(self):
# In most agent-based models, there is only step().
# We split it into step() and act() phases to ensure order independence
# in some conditions. In the full model, trigger_default() may contain
# a behavioural unit that does pull funding.
if self.do_trigger_default:
self.trigger_default()
if self.alive:
super().step()
def act(self):
if not self.alive:
return
self.availableActions = self.get_available_actions()
try:
self.choose_actions()
except DefaultException:
# In general, when a bank defaults, its default treatment
# may be order-dependent if executed immediately (e.g. when
# it performs bilateral pull funding in the full model), so
# it is best to delay it to the step() stage.
self.do_trigger_default = True
self.alive = False
# This is for record keeping.
self.simulation.bank_defaults_this_round += 1
def get_all_actions_of_type(self, actionType):
return self.availableActions[actionType]
def update_asset_price(self, assetType):
# design choice: accounting is done by the institution itself,
# not by the market.
for asset in self.get_ledger().get_assets_of_type(Tradable):
if asset.get_asset_type() == assetType:
asset.update_price()