-
Notifications
You must be signed in to change notification settings - Fork 20
/
metaplotter_pl_timescale_additional_measures.py
187 lines (167 loc) · 11.8 KB
/
metaplotter_pl_timescale_additional_measures.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import matplotlib.pyplot as plt
import numpy as np
import pdb
import os
import time
import glob
def read_data():
# do not overwrite old pdfs
#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")
#if os.path.exists("data/fig_three_and_four_rm_comp.pdf"):
# os.rename("data/fig_three_and_four_rm_comp.pdf", "data/fig_three_and_four_rm_comp_old_" + time.strftime('%Y_%b_%d_%H_%M') + ".pdf")
upper_bound = 75
lower_bound = 25
timeseries_dict = {}
timeseries_dict["mean"] = {}
timeseries_dict["median"] = {}
timeseries_dict["quantile25"] = {}
timeseries_dict["quantile75"] = {}
filenames_ones = glob.glob("data/one*.dat")
filenames_twos = glob.glob("data/two*.dat")
filenames_threes = glob.glob("data/three*.dat")
filenames_fours = glob.glob("data/four*.dat")
filenames_ones.sort()
filenames_twos.sort()
filenames_threes.sort()
filenames_fours.sort()
#assert len(filenames_ones) == len(filenames_twos) == len(filenames_threes) == len(filenames_fours)
all_filenames = filenames_ones + filenames_twos + filenames_threes + filenames_fours
for filename in all_filenames:
# read files
rfile = open(filename, "r")
data = [eval(k) for k in rfile]
rfile.close()
# compute data series
data_means = []
data_medians = []
data_q25 = []
data_q75 = []
for i in range(len(data[0])):
data_means.append(np.mean([item[i] for item in data]))
data_q25.append(np.percentile([item[i] for item in data], lower_bound))
data_q75.append(np.percentile([item[i] for item in data], upper_bound))
data_medians.append(np.median([item[i] for item in data]))
data_means = np.array(data_means)
data_medians = np.array(data_medians)
data_q25 = np.array(data_q25)
data_q75 = np.array(data_q75)
# record data series
timeseries_dict["mean"][filename] = data_means
timeseries_dict["median"][filename] = data_medians
timeseries_dict["quantile25"][filename] = data_q25
timeseries_dict["quantile75"][filename] = data_q75
return timeseries_dict
def plotting(output_label, timeseries_dict, riskmodelsetting1, riskmodelsetting2, series1, series2=None, additionalriskmodelsetting3=None, additionalriskmodelsetting4=None, plottype1="mean", plottype2="mean"):
# dictionaries
colors = {"one": "red", "two": "blue", "three": "green", "four": "yellow"}
labels = {"reinexcess_capital": "Excess Capital (Reinsurers)", "excess_capital": "Excess Capital (Insurers)", "cumulative_unrecovered_claims": "Uncovered Claims (cumulative)", "cumulative_bankruptcies": "Bankruptcies (cumulative)", "profitslosses": "Profits and Losses (Insurer)", "contracts": "Contracts (Insurers)", "cash": "Liquidity (Insurers)", "operational": "Active Insurers", "premium": "Premium", "reinprofitslosses": "Profits and Losses (Reinsurer)", "reincash": "Liquidity (Reinsurers)", "reincontracts": "Contracts (Reinsurers)", "reinoperational": "Active Reinsurers"}
# prepare labels, timeseries, etc.
color1 = colors[riskmodelsetting1]
color2 = colors[riskmodelsetting2]
label1 = str.upper(riskmodelsetting1[0]) + riskmodelsetting1[1:] + " riskmodels"
label2 = str.upper(riskmodelsetting2[0]) + riskmodelsetting2[1:] + " riskmodels"
plot_1_1 = "data/" + riskmodelsetting1 + "_" + series1 + ".dat"
plot_1_2 = "data/" + riskmodelsetting2 + "_" + series1 + ".dat"
if series2 is not None:
plot_2_1 = "data/" + riskmodelsetting1 + "_" + series2 + ".dat"
plot_2_2 = "data/" + riskmodelsetting2 + "_" + series2 + ".dat"
if additionalriskmodelsetting3 is not None:
color3 = colors[additionalriskmodelsetting3]
label3 = str.upper(additionalriskmodelsetting3[0]) + additionalriskmodelsetting3[1:] + " riskmodels"
plot_1_3 = "data/" + additionalriskmodelsetting3 + "_" + series1 + ".dat"
if series2 is not None:
plot_2_3 = "data/" + additionalriskmodelsetting3 + "_" + series2 + ".dat"
if additionalriskmodelsetting4 is not None:
color4 = colors[additionalriskmodelsetting4]
label4 = str.upper(additionalriskmodelsetting4[0]) + additionalriskmodelsetting4[1:] + " riskmodels"
plot_1_4 = "data/" + additionalriskmodelsetting4 + "_" + series1 + ".dat"
if series2 is not None:
plot_2_4 = "data/" + additionalriskmodelsetting4 + "_" + series2 + ".dat"
# Backup existing figures (so as not to overwrite them)
outputfilename = "data/" + output_label + ".pdf"
backupfilename = "data/" + output_label + "_old_" + time.strftime('%Y_%b_%d_%H_%M') + ".pdf"
if os.path.exists(outputfilename):
os.rename(outputfilename, backupfilename)
# Plot and save
fig = plt.figure()
if series2 is not None:
ax0 = fig.add_subplot(211)
else:
ax0 = fig.add_subplot(111)
maxlen_plots = 0
if additionalriskmodelsetting3 is not None:
ax0.plot(range(len(timeseries_dict[plottype1][plot_1_3]))[200:], timeseries_dict[plottype1][plot_1_3][200:], color=color3, label=label3)
maxlen_plots = max(maxlen_plots, len(timeseries_dict[plottype1][plot_1_3]))
if additionalriskmodelsetting4 is not None:
ax0.plot(range(len(timeseries_dict[plottype1][plot_1_4]))[200:], timeseries_dict[plottype1][plot_1_4][200:], color=color4, label=label4)
maxlen_plots = max(maxlen_plots, len(timeseries_dict[plottype1][plot_1_4]))
ax0.plot(range(len(timeseries_dict[plottype1][plot_1_1]))[200:], timeseries_dict[plottype1][plot_1_1][200:], color=color1, label=label1)
ax0.plot(range(len(timeseries_dict[plottype1][plot_1_2]))[200:], timeseries_dict[plottype1][plot_1_2][200:], color=color2, label=label2)
ax0.fill_between(range(len(timeseries_dict["quantile25"][plot_1_1]))[200:], timeseries_dict["quantile25"][plot_1_1][200:], timeseries_dict["quantile75"][plot_1_1][200:], facecolor=color1, alpha=0.25)
ax0.fill_between(range(len(timeseries_dict["quantile25"][plot_1_1]))[200:], timeseries_dict["quantile25"][plot_1_2][200:], timeseries_dict["quantile75"][plot_1_2][200:], facecolor=color2, alpha=0.25)
ax0.set_ylabel(labels[series1])#"Contracts")
maxlen_plots = max(maxlen_plots, len(timeseries_dict[plottype1][plot_1_1]), len(timeseries_dict[plottype1][plot_1_2]))
xticks = np.arange(200, maxlen_plots, step=120)
ax0.set_xticks(xticks)
ax0.set_xticklabels(["${0:d}$".format(int((xtc-200)/12)) for xtc in xticks]);
ax0.legend(loc='best')
if series2 is not None:
ax1 = fig.add_subplot(212)
maxlen_plots = 0
if additionalriskmodelsetting3 is not None:
ax1.plot(range(len(timeseries_dict[plottype2][plot_2_3]))[200:], timeseries_dict[plottype2][plot_2_3][200:], color=color3, label=label3)
maxlen_plots = max(maxlen_plots, len(timeseries_dict[plottype1][plot_2_3]))
if additionalriskmodelsetting4 is not None:
ax1.plot(range(len(timeseries_dict[plottype2][plot_2_4]))[200:], timeseries_dict[plottype2][plot_2_4][200:], color=color4, label=label4)
maxlen_plots = max(maxlen_plots, len(timeseries_dict[plottype1][plot_2_4]))
ax1.plot(range(len(timeseries_dict[plottype2][plot_2_1]))[200:], timeseries_dict[plottype2][plot_2_1][200:], color=color1, label=label1)
ax1.plot(range(len(timeseries_dict[plottype2][plot_2_2]))[200:], timeseries_dict[plottype2][plot_2_2][200:], color=color2, label=label2)
ax1.fill_between(range(len(timeseries_dict["quantile25"][plot_2_1]))[200:], timeseries_dict["quantile25"][plot_2_1][200:], timeseries_dict["quantile75"][plot_2_1][200:], facecolor=color1, alpha=0.25)
ax1.fill_between(range(len(timeseries_dict["quantile25"][plot_2_1]))[200:], timeseries_dict["quantile25"][plot_2_2][200:], timeseries_dict["quantile75"][plot_2_2][200:], facecolor=color2, alpha=0.25)
maxlen_plots = max(maxlen_plots, len(timeseries_dict[plottype1][plot_2_1]), len(timeseries_dict[plottype1][plot_2_2]))
xticks = np.arange(200, maxlen_plots, step=120)
ax1.set_xticks(xticks)
ax1.set_xticklabels(["${0:d}$".format(int((xtc-200)/12)) for xtc in xticks]);
ax1.set_ylabel(labels[series2])
ax1.set_xlabel("Years")
else:
ax0.set_xlabel("Years")
plt.savefig(outputfilename)
plt.show()
timeseries = read_data()
# for just two different riskmodel settings
#plotting(output_label="fig_pl_excap_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", \
# riskmodelsetting2="two", series1="profitslosses", series2="excess_capital", plottype1="mean", plottype2="mean")
#plotting(output_label="fig_reinsurers_pl_excap_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", \
# riskmodelsetting2="two", series1="reinprofitslosses", series2="reinexcess_capital", plottype1="mean", plottype2="mean")
#plotting(output_label="fig_bankruptcies_unrecovered_claims_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", \
# riskmodelsetting2="two", series1="cumulative_bankruptcies", series2="cumulative_unrecovered_claims", plottype1="mean", plottype2="median")
#plotting(output_label="fig_premium_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", riskmodelsetting2="two", \
# series1="premium", series2=None, plottype1="mean", plottype2=None)
#
#raise SystemExit
# for four different riskmodel settings
plotting(output_label="fig_pl_excap_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", \
riskmodelsetting2="two", series1="profitslosses", series2="excess_capital", additionalriskmodelsetting3="three", \
additionalriskmodelsetting4="four", plottype1="mean", plottype2="mean")
plotting(output_label="fig_reinsurers_pl_excap_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", \
riskmodelsetting2="two", series1="reinprofitslosses", series2="reinexcess_capital", additionalriskmodelsetting3="three", \
additionalriskmodelsetting4="four", plottype1="mean", plottype2="mean")
plotting(output_label="fig_bankruptcies_unrecovered_claims_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", \
riskmodelsetting2="two", series1="cumulative_bankruptcies", series2="cumulative_unrecovered_claims", additionalriskmodelsetting3="three", \
additionalriskmodelsetting4="four", plottype1="mean", plottype2="median")
plotting(output_label="fig_premium_1_2", timeseries_dict=timeseries, riskmodelsetting1="one", riskmodelsetting2="two", \
series1="premium", series2=None, additionalriskmodelsetting3="three", additionalriskmodelsetting4="four", plottype1="mean", plottype2=None)
plotting(output_label="fig_pl_excap_3_4", timeseries_dict=timeseries, riskmodelsetting1="three", \
riskmodelsetting2="four", series1="profitslosses", series2="excess_capital", additionalriskmodelsetting3="one", \
additionalriskmodelsetting4="two", plottype1="mean", plottype2="mean")
plotting(output_label="fig_reinsurers_pl_excap_3_4", timeseries_dict=timeseries, riskmodelsetting1="three", \
riskmodelsetting2="four", series1="reinprofitslosses", series2="reinexcess_capital", additionalriskmodelsetting3="one", \
additionalriskmodelsetting4="two", plottype1="mean", plottype2="mean")
plotting(output_label="fig_bankruptcies_unrecovered_claims_3_4", timeseries_dict=timeseries, riskmodelsetting1="three", \
riskmodelsetting2="four", series1="cumulative_bankruptcies", series2="cumulative_unrecovered_claims", additionalriskmodelsetting3="one", \
additionalriskmodelsetting4="two", plottype1="mean", plottype2="median")
plotting(output_label="fig_premium_3_4", timeseries_dict=timeseries, riskmodelsetting1="three", riskmodelsetting2="four", \
series1="premium", series2=None, additionalriskmodelsetting3="one", additionalriskmodelsetting4="two", plottype1="mean", plottype2=None)
#pdb.set_trace()