Skip to content

Commit

Permalink
Merge pull request #2 from EconomicSL/master
Browse files Browse the repository at this point in the history
Reinsurance simplified
  • Loading branch information
jsabuco authored Oct 24, 2017
2 parents f207204 + 555da94 commit 494a66e
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 47 deletions.
101 changes: 55 additions & 46 deletions insurancesimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ def __init__(self, replic_ID = None, override_no_riskmodels = False, simulation_
"no_riskmodels": 2, \
"norm_profit_markup": 0.15, \
"rein_norm_profit_markup": 0.15, \
"mean_contract_runtime": 50, \
"mean_contract_runtime": 30, \
"contract_runtime_halfspread": 10, \
"max_time": 600, \
"money_supply": 2000000000, \
"event_time_mean_separation": 100 / 0.3, \
"event_time_mean_separation": 200 / 0.3, \
"expire_immediately": True, \
"risk_factors_present": False, \
"risk_factor_lower_bound": 0.4, \
"risk_factor_upper_bound": 0.6, \
"initial_acceptance_threshold": 0.5, \
"acceptance_threshold_friction": 0.9, \
"initial_agent_cash": 100000, \
"initial_agent_cash": 10000, \
"initial_reinagent_cash": 50000, \
"no_risks": 2000}):
"no_risks": 20000}):

# override one-riskmodel case (this is to ensure all other parameters are truly identical for comparison runs)
if override_no_riskmodels:
Expand Down Expand Up @@ -218,50 +218,59 @@ def run(self):
individual_contracts_no = [len(insurancefirm.underwritten_contracts) for insurancefirm in self.insurancefirms]
for i in range(len(individual_contracts_no)):
self.history_individual_contracts[i].append(individual_contracts_no[i])


self.log()

def log(self):
if self.background_run:
wfile = open("data/two_operational.dat","a")
wfile.write(str(self.history_total_operational)+"\n")
wfile.close()
wfile = open("data/two_contracts.dat","a")
wfile.write(str(self.history_total_contracts)+"\n")
wfile.close()
wfile = open("data/two_cash.dat","a")
wfile.write(str(self.history_total_cash)+"\n")
wfile.close()
self.replication_log()
else:
wfile = open("data/operational.dat","w")
wfile.write(str(self.history_total_operational)+"\n")
wfile.close()
wfile = open("data/contracts.dat","w")
wfile.write(str(self.history_total_contracts)+"\n")
wfile.close()
wfile = open("data/cash.dat","w")
wfile.write(str(self.history_total_cash)+"\n")
wfile.close()
wfile = open("data/reinoperational.dat","w")
wfile.write(str(self.history_total_reinoperational)+"\n")
wfile.close()
wfile = open("data/reincontracts.dat","w")
wfile.write(str(self.history_total_reincontracts)+"\n")
wfile.close()
wfile = open("data/reincash.dat","w")
wfile.write(str(self.history_total_reincash)+"\n")
wfile.close()

#fig = plt.figure()
#ax0 = fig.add_subplot(311)
#ax0.plot(range(len(self.history_total_cash)), self.history_total_cash)
#ax0.set_ylabel("Cash")
#ax1 = fig.add_subplot(312)
#ax1.plot(range(len(self.history_total_contracts)), self.history_total_contracts)
#ax1.set_ylabel("Contracts")
#ax2 = fig.add_subplot(313)
#for i in range(len(self.history_individual_contracts)):
# ax2.plot(range(len(self.history_individual_contracts[i])), self.history_individual_contracts[i])
#ax2.set_ylabel("Contracts")
#ax2.set_xlabel("Time")
#plt.show()
self.single_log()

def replication_log(self):
wfile = open("data/two_operational.dat","a")
wfile.write(str(self.history_total_operational)+"\n")
wfile.close()
wfile = open("data/two_contracts.dat","a")
wfile.write(str(self.history_total_contracts)+"\n")
wfile.close()
wfile = open("data/two_cash.dat","a")
wfile.write(str(self.history_total_cash)+"\n")
wfile.close()

def single_log(self):
wfile = open("data/operational.dat","w")
wfile.write(str(self.history_total_operational)+"\n")
wfile.close()
wfile = open("data/contracts.dat","w")
wfile.write(str(self.history_total_contracts)+"\n")
wfile.close()
wfile = open("data/cash.dat","w")
wfile.write(str(self.history_total_cash)+"\n")
wfile.close()
wfile = open("data/reinoperational.dat","w")
wfile.write(str(self.history_total_reinoperational)+"\n")
wfile.close()
wfile = open("data/reincontracts.dat","w")
wfile.write(str(self.history_total_reincontracts)+"\n")
wfile.close()
wfile = open("data/reincash.dat","w")
wfile.write(str(self.history_total_reincash)+"\n")
wfile.close()

#fig = plt.figure()
#ax0 = fig.add_subplot(311)
#ax0.plot(range(len(self.history_total_cash)), self.history_total_cash)
#ax0.set_ylabel("Cash")
#ax1 = fig.add_subplot(312)
#ax1.plot(range(len(self.history_total_contracts)), self.history_total_contracts)
#ax1.set_ylabel("Contracts")
#ax2 = fig.add_subplot(313)
#for i in range(len(self.history_individual_contracts)):
# ax2.plot(range(len(self.history_individual_contracts[i])), self.history_individual_contracts[i])
#ax2.set_ylabel("Contracts")
#ax2.set_xlabel("Time")
#plt.show()

def receive_obligation(self, amount, recipient, due_time):
obligation = {"amount": amount, "recipient": recipient, "due_time": due_time}
Expand Down
17 changes: 16 additions & 1 deletion insurancesimulation_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@
import pdb, sys

class InsuranceSimulation_One(InsuranceSimulation):

def replication_log(self):
wfile = open("data/one_operational.dat","a")
wfile.write(str(self.history_total_operational)+"\n")
wfile.close()
wfile = open("data/one_contracts.dat","a")
wfile.write(str(self.history_total_contracts)+"\n")
wfile.close()
wfile = open("data/one_cash.dat","a")
wfile.write(str(self.history_total_cash)+"\n")
wfile.close()

def setup_risk_categories_caller(self):
if self.background_run:
rfile = open("data/rc_event_schedule.dat","r")
found = False
for i, line in enumerate(rfile):
print(i, self.replic_ID)
if i == self.replic_ID:
self.rc_event_schedule = eval(line)
found = True
rfile.close()

assert found, "rc event schedule for current replication ID number {0:d} not found in data file. Exiting.".format(self.replic_ID)
else:
self.setup_risk_categories()

Expand Down
100 changes: 100 additions & 0 deletions metaplotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import matplotlib.pyplot as plt
import numpy as np
import pdb
import os
import time

# do not overwrite old pdf
if os.path.exists("data/fig_one_and_two_rm_comp.pdf"):
os.rename("data/fig_one_and_two_rm_comp.pdf", "data/fig_one_and_two_rm_comp_old_" + time.strftime('%Y_%b_%d_%H_%M') + ".pdf")

upper_bound = 75
lower_bound = 25

rfile = open("data/one_contracts.dat","r")
contracts_one = [eval(k) for k in rfile]
rfile.close()

rfile = open("data/two_contracts.dat","r")
contracts_two = [eval(k) for k in rfile]
rfile.close()

rfile = open("data/one_operational.dat","r")
op_one = [eval(k) for k in rfile]
rfile.close()

rfile = open("data/two_operational.dat","r")
op_two = [eval(k) for k in rfile]
rfile.close()

c_one = []
c_two = []

c_one_lo = []
c_one_up = []
c_two_lo = []
c_two_up = []

o_one = []
o_two = []

o_one_lo = []
o_one_up = []
o_two_lo = []
o_two_up = []


for i in range(len(contracts_one[0])):
c1 = np.mean([item[i] for item in contracts_one])
c2 = np.mean([item[i] for item in contracts_two])
c1_lo = np.percentile([item[i] for item in contracts_one], lower_bound)
c1_up = np.percentile([item[i] for item in contracts_one], upper_bound)
c2_lo = np.percentile([item[i] for item in contracts_two], lower_bound)
c2_up = np.percentile([item[i] for item in contracts_two], upper_bound)
#o1 = np.mean([item[i] for item in op_one])
#o2 = np.mean([item[i] for item in op_two])
#c1 = np.median([item[i] for item in contracts_one])
#c2 = np.median([item[i] for item in contracts_two])
o1 = np.median([item[i] for item in op_one])
o2 = np.median([item[i] for item in op_two])
o1_lo = np.percentile([item[i] for item in op_one], lower_bound)
o1_up = np.percentile([item[i] for item in op_one], upper_bound)
o2_lo = np.percentile([item[i] for item in op_two], lower_bound)
o2_up = np.percentile([item[i] for item in op_two], upper_bound)
c_one.append(c1)
c_two.append(c2)
c_one_up.append(c1_up)
c_one_lo.append(c1_lo)
c_two_up.append(c2_up)
c_two_lo.append(c2_lo)
o_one.append(o1)
o_two.append(o2)
o_one_up.append(o1_up)
o_one_lo.append(o1_lo)
o_two_up.append(o2_up)
o_two_lo.append(o2_lo)

print(contracts_one)
print(contracts_two)
print(c_one)
print(c_two)
print(len(c_one), len(c_one_up))
#pdb.set_trace()

fig = plt.figure()
ax0 = fig.add_subplot(211)
ax0.plot(range(len(c_one)), c_one,"r", label="One riskmodel")
ax0.plot(range(len(c_two)), c_two,"b", label="Two riskmodels")
ax0.fill_between(range(len(c_one)), c_one_lo, c_one_up, facecolor='red', alpha=0.25)
ax0.fill_between(range(len(c_two)), c_two_lo, c_two_up, facecolor='blue', alpha=0.25)
ax0.set_ylabel("Contracts")
ax0.legend(loc='best')
ax1 = fig.add_subplot(212)
ax1.plot(range(len(o_one)), o_one,"r")
ax1.plot(range(len(o_two)), o_two,"b")
ax1.fill_between(range(len(o_one)), o_one_lo, o_one_up, facecolor='red', alpha=0.25)
ax1.fill_between(range(len(o_two)), o_two_lo, o_two_up, facecolor='blue', alpha=0.25)
ax1.set_ylabel("Active firms (out of initially 20)")
ax1.set_xlabel("Time")
plt.savefig("data/fig_one_and_two_rm_comp.pdf")
plt.show()

0 comments on commit 494a66e

Please sign in to comment.