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

Separate out obligation-specific methods to a separate class. #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 38 additions & 22 deletions metainsuranceorg.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,40 @@ def get_mean_std(x):
variance = sum((val - m) ** 2 for val in x)
return m, np.sqrt(variance / len(x))

class MetaInsuranceOrg(GenericAgent):
class Obligation:
__slots__ = 'amount', 'recipient', 'due_time', 'purpose'

def __init__(self, amount, recipient, due_time, purpose):
self.amount = amount
self.recipient = recipient
self.due_time = due_time
self.purpose = purpose

class Messenger:
def __init__(self):
self.obligations = []

def receive_obligation(self, amount, recipient, due_time, purpose):
obligation = Obligation(amount, recipient, due_time, purpose)
self.obligations.append(obligation)

def pay(self, obligation):
amount = obligation.amount
if self.get_operational() and obligation.recipient.get_operational():
self.cash -= amount
if obligation.purpose is not 'dividend':
self.profits_losses -= amount
obligation.recipient.receive(amount)

def refresh_obligations(self, time):
self.obligations = [o for o in self.obligations if o.due_time > time]

def get_due_obligations(self, time):
return [o for o in self.obligations if o.due_time <= time]

class MetaInsuranceOrg(GenericAgent, Messenger):
def init(self, simulation_parameters, agent_parameters):
Messenger.__init__(self)
self.simulation = simulation_parameters['simulation']
self.simulation_parameters = simulation_parameters
self.contract_runtime_dist = scipy.stats.randint(simulation_parameters["mean_contract_runtime"] - \
Expand Down Expand Up @@ -75,7 +107,6 @@ def init(self, simulation_parameters, agent_parameters):
self.np_reinsurance_deductible_fraction = simulation_parameters["default_non-proportional_reinsurance_deductible"]
self.np_reinsurance_excess_fraction = simulation_parameters["default_non-proportional_reinsurance_excess"]
self.np_reinsurance_premium_share = simulation_parameters["default_non-proportional_reinsurance_premium_share"]
self.obligations = []
self.underwritten_contracts = []
self.profits_losses = 0
#self.reinsurance_contracts = []
Expand Down Expand Up @@ -238,8 +269,7 @@ def market_exit(self, time):
method of this class. It needs to be different from the method self.enter_bankruptcy() because in this case
all the obligations can be paid. After paying all the obligations this method dissolves the firm through the
method self.dissolve()."""
due = [item for item in self.obligations]
for obligation in due:
for obligation in self.obligations:
self.pay(obligation)
self.obligations = []
self.dissolve(time, 'record_market_exit')
Expand All @@ -261,7 +291,7 @@ def dissolve(self, time, record):
self.simulation.return_risks(self.risks_kept)
self.risks_kept = []
self.reinrisks_kept = []
obligation = {"amount": self.cash, "recipient": self.simulation, "due_time": time, "purpose": "Dissolution"}
obligation = Obligation(self.cash, self.simulation, time, "Dissolution")
self.pay(obligation) #This MUST be the last obligation before the dissolution of the firm.
self.excess_capital = 0 #Excess of capital is 0 after bankruptcy or market exit.
self.profits_losses = 0 #Profits and losses are 0 after bankruptcy or market exit.
Expand All @@ -270,14 +300,10 @@ def dissolve(self, time, record):
method_to_call()
self.operational = False

def receive_obligation(self, amount, recipient, due_time, purpose):
obligation = {"amount": amount, "recipient": recipient, "due_time": due_time, "purpose": purpose}
self.obligations.append(obligation)

def effect_payments(self, time):
due = [item for item in self.obligations if item["due_time"]<=time]
self.obligations = [item for item in self.obligations if item["due_time"]>time]
sum_due = sum([item["amount"] for item in due])
due = self.get_due_obligations(time)
self.refresh_obligations(time)
sum_due = sum([item.amount for item in due])
if sum_due > self.cash:
self.obligations += due
self.enter_illiquidity(time)
Expand All @@ -289,16 +315,6 @@ def effect_payments(self, time):
self.pay(obligation)


def pay(self, obligation):
amount = obligation["amount"]
recipient = obligation["recipient"]
purpose = obligation["purpose"]
if self.get_operational() and recipient.get_operational():
self.cash -= amount
if purpose is not 'dividend':
self.profits_losses -= amount
recipient.receive(amount)

def receive(self, amount):
"""Method to accept cash payments."""
self.cash += amount
Expand Down