From cddcedeb42bc2f1a103c1914a4fe97549e2e510f Mon Sep 17 00:00:00 2001 From: Torsten Date: Tue, 10 Oct 2017 16:57:18 +0100 Subject: [PATCH 1/2] Fixed logging issue --- insurancesimulation.py | 101 ++++++++++++++++++++----------------- insurancesimulation_one.py | 17 ++++++- 2 files changed, 71 insertions(+), 47 deletions(-) diff --git a/insurancesimulation.py b/insurancesimulation.py index 92987c6..37c2935 100644 --- a/insurancesimulation.py +++ b/insurancesimulation.py @@ -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: @@ -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} diff --git a/insurancesimulation_one.py b/insurancesimulation_one.py index abc7a03..8c7c2d9 100644 --- a/insurancesimulation_one.py +++ b/insurancesimulation_one.py @@ -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() From 5f26365ab1a18a7031b603e47e173015f0ced344 Mon Sep 17 00:00:00 2001 From: Torsten Date: Thu, 19 Oct 2017 05:27:21 +0200 Subject: [PATCH 2/2] Added metaplotter.py to plot meta-results of experiments with many replications in controlled one- and two-riskmodel (homogeneity and diversity) settings --- metaplotter.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 metaplotter.py diff --git a/metaplotter.py b/metaplotter.py new file mode 100644 index 0000000..6c29527 --- /dev/null +++ b/metaplotter.py @@ -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()