Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

1 #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions banks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bank_id,CET1E,leverage,debt_sec,govbonds
B01 25000 4.00 40000 20000
B02 25000 6.00 40000 20000
B03 25000 3.90 40000 20000
B04 25000 3.10 40000 20000
4 changes: 3 additions & 1 deletion markets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np

from contracts import eps
from behaviours import pay_off_liabilities

# This represents a sale order in an asset market's orderbook.
class Order:
Expand All @@ -19,7 +20,8 @@ def settle(self):
# Sell the asset at the mid-point price
value_sold = quantity_sold * (self.asset.price + old_price) / 2
if value_sold >= eps:
self.asset.assetParty.add_cash(value_sold)
#self.asset.assetParty.add_cash(value_sold)
pay_off_liabilities(self.asset.assetParty, value_sold)


# The key functions are clear_the_market() and compute_price_impact()
Expand Down
10 changes: 5 additions & 5 deletions reinforcement_learning/MARL/NaiveA2C/ddpg_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ def sample(self):
"""Randomly sample a batch of experiences from memory."""
experiences = random.sample(self.memory, k=self.batch_size)

states = torch.from_numpy(np.vstack([e.state.astype(float) for e in experiences if e is not None])).float().to(device)
actions = torch.from_numpy(np.vstack([e.action.astype(float) for e in experiences if e is not None])).float().to(device)
rewards = torch.from_numpy(np.vstack([e.reward for e in experiences if e is not None])).float().to(device)
next_states = torch.from_numpy(np.vstack([e.next_state.astype(float) for e in experiences if e is not None])).float().to(
states = torch.from_numpy(np.asarray([e.state.astype(float) for e in experiences if e is not None], dtype='float64')).float().to(device)
actions = torch.from_numpy(np.asarray([e.action.astype(float) for e in experiences if e is not None], dtype='float64')).float().to(device)
rewards = torch.from_numpy(np.asarray([e.reward for e in experiences if e is not None], dtype='float64')).float().to(device)
next_states = torch.from_numpy(np.asarray([e.next_state.astype(float) for e in experiences if e is not None], dtype='float64')).float().to(
device)
dones = torch.from_numpy(np.vstack([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(
dones = torch.from_numpy(np.asarray([e.done for e in experiences if e is not None], dtype='float64').astype(np.uint8)).float().to(
device)

return (states, actions, rewards, next_states, dones)
Expand Down
9 changes: 4 additions & 5 deletions reinforcement_learning/MARL/NaiveA2C/model_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ def MA_obs_to_bank_obs(obs, bank):

RLagent_dict = {}
env = RLModelEnv()
env.parameters.INITIAL_SHOCK = 0.01
env.parameters.SIMULTANEOUS_FIRESALE = False
bank_names = 'AT01 AT02 BE03 BE04 DK05 DK06 DK07 FI08 FR09 FR10 FR11 FR12 FR13 FR14 DE15 DE16 DE17 DE18 DE19 DE20 DE21 DE22 HU23 IE24 IE25 IT26 IT27 IT28 IT29 NL30 NL31 NL32 NL33 NO34 PL35 PL36 ES37 ES38 ES39 ES40 SE41 SE42 SE43 SE44 UK45 UK46 UK47 UK48'.split()

bank_names = [f'B0{i}' for i in range(1, 5)]

for idx, name in enumerate(bank_names):
agent = Agent(state_size=3, action_size=2, random_seed=idx, name=name)
RLagent_dict[name] = agent

average_lifespans = []
for episode in range(1000):
for episode in range(10000):

if episode == 0 or episode % 100 == 0:
print(f'=========================================Episode {episode}===============================================')
current_obs = env.reset()
play, max_play = 0, 15
play, max_play = 0, 5
num_default = []
while play < max_play:
actions = {}
Expand Down
17 changes: 10 additions & 7 deletions reinforcement_learning/gym_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ def choose_actions(self, action):
# 0) If I'm insolvent, default.
if self.is_insolvent():
raise DefaultException(self, 'SOLVENCY')
balance = self.get_cash_()
# 1. Pay off liabilities to delever
amountToDeLever = self.leverageConstraint.get_amount_to_delever()
if amountToDeLever > 0:
deLever = pay_off_liabilities(self, amountToDeLever)
balance -= deLever
amountToDeLever -= deLever
# Moved to Order.settle()

# 2. Raise liquidity to delever later
sellAssetActions = self.get_all_actions_of_type(SellAsset)
Expand Down Expand Up @@ -61,7 +56,7 @@ def reset(self):
self.allAgents_dict = {}
self.assetMarket = AssetMarket(self)
obs = {}
with open('EBA_2018.csv', 'r') as data:
with open('/Users/mmw/Documents/GitHub/firesale_stresstest/banks.csv', 'r') as data:
self.bank_balancesheets = data.read().strip().split('\n')[1:]
for bs in self.bank_balancesheets:
# This steps consist of loading balance sheet from data file
Expand Down Expand Up @@ -144,6 +139,8 @@ def step(self, action_dict):

def stupid_action(bank):
action = {}
CB_qty = bank.get_ledger().get_asset_valuation_of(Tradable, AssetType.CORPORATE_BONDS)
GB_qty = bank.get_ledger().get_asset_valuation_of(Tradable, AssetType.GOV_BONDS)
action[AssetType.CORPORATE_BONDS], action[AssetType.GOV_BONDS] = 0.2 * abs(np.random.normal() - 0.5), 0.2 * np.random.normal() * abs(np.random.normal() - 0.5)
return action

Expand All @@ -153,6 +150,12 @@ def stupid_action(bank):
actions = {}
play += 1
for bank_name, bank in env.allAgents_dict.items():
CB_qty = bank.get_ledger().get_asset_valuation_of(Tradable, 1)
GB_qty = bank.get_ledger().get_asset_valuation_of(Tradable, 2)
equity = bank.get_ledger().get_equity_valuation()
lev_ratio_percent = bank.leverageConstraint.get_leverage() * 100
print(
f'Round {play}. Bank {bank_name}, CB: {int(CB_qty)}, GB: {int(GB_qty)}, EQUITY: {int(equity)}, LEV: {int(lev_ratio_percent)}%')
actions[bank_name] = stupid_action(bank) # this is where you use your RLAgents!
obs, _, _, infos = env.step(actions)
num_defaults.append(infos['NUM_DEFAULTS'])
Expand Down