-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpaper_pictures_30Portfolios_comparison_ICML18.py
204 lines (165 loc) · 6.23 KB
/
paper_pictures_30Portfolios_comparison_ICML18.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 2 20:17:54 2018
@author: jeremiasknoblauch
Description: Produce plots based on 30PF data (run: 'comparison')
"""
import numpy as np
from Evaluation_tool import EvaluationTool
from matplotlib import pyplot as plt
import csv
import datetime
import matplotlib
from matplotlib import rcParams
#ensure that we have type 1 fonts (for ICML publishing guidelines)
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
baseline_working_directory = ("//Users//jeremiasknoblauch//Documents//OxWaSP//BOCPDMS//" +
"//Code//" +
"SpatialBOCD//Paper//30PortfolioData")
date_file = "portfolio_dates.csv"
results_file= "results_30PortfolioData_comparison.txt"
"""Read in the dates & results"""
myl = []
count = 0
with open(baseline_working_directory + "//" + date_file) as csvfile:
reader = csv.reader(csvfile)
for row in reader:
#Don't read in column header
if count > 0:
myl += row
count += 1
if count % 2500 == 0:
print(count)
"""Dates for the x-axis"""
dates = []
for e in myl:
dates.append(int(e))
"""Build the EvaluationTool from the results you have saved"""
result_path = baseline_working_directory
EvT = EvaluationTool()
EvT.build_EvaluationTool_via_results(result_path + "//" + results_file)
"""Using the dates, select your range and indices: select
03/07/1975 -- 31/12/2008, i.e. find indices that correspond"""
start_test = dates.index(19740703)
start_algo = dates.index(19750703)
stop = dates.index(20081231)
"""time period for which we want RLD"""
start_comparison = dates.index(19980102)
stop_comparison = stop
all_dates = []
for d in dates[start_comparison:stop-2]:
s = str(d)
all_dates.append(datetime.date(year=int(s[0:4]),
month=int(s[4:6]), day=int(s[6:8])))
"""indices of that time period"""
start_range = start_comparison - start_test
stop_range = stop_comparison - start_test
time_range = np.linspace(start_range, stop_range,
stop_range - start_range, dtype = int)
"""Create list of event times and labels"""
label_list, event_time_list= [],[]
number_labels = True
"""Color for the arrows"""
color_us = "orange"
color_they = "black"
arrow_color_list = []
"""helper function getting closest date in all_dates"""
def nearest(pivot):
return min(all_dates, key=lambda x: abs(x - pivot))
"""Get the CP dates"""
label_list.append("Asia Crisis")
#date: last quarter 98
event_time_list.append(nearest(datetime.date(1998, 9, 12)))
arrow_color_list.append(color_they)
label_list.append("DotCom bubble bursts")
#date: April, when inflation reports came in
event_time_list.append(nearest(datetime.date(2000, 4, 12)))
arrow_color_list.append(color_they)
label_list.append("OPEC cuts output by 4%, Japanese central bank starts QE")
#date: announcement of output cut
event_time_list.append(nearest(datetime.date(2001,3,19)))
arrow_color_list.append(color_us)
label_list.append("9/11")
#date: airplane day
event_time_list.append(nearest(datetime.date(2001, 9, 9)))
arrow_color_list.append(color_they)
label_list.append("Afghanistan war")
#date: Operation Enduring Freedom begins
event_time_list.append(nearest(datetime.date(2001, 10, 7)))
arrow_color_list.append(color_us)
label_list.append("2002 Crash")
#date: Crash of 2002, 23/7 lowest Dow Jones value
event_time_list.append(nearest(datetime.date(2002, 7, 23)))
arrow_color_list.append(color_us)
label_list.append("Bali bombing attack")
#date: day bomb went up
event_time_list.append(nearest(datetime.date(2002, 10,13)))
arrow_color_list.append(color_us)
label_list.append("Iraq war")
#date: start of invasion
event_time_list.append(nearest(datetime.date(2003,3,20)))
arrow_color_list.append(color_us)
label_list.append("Bush cuts")
#date: day regulation was passed
event_time_list.append(nearest(datetime.date(2003, 5, 23)))
arrow_color_list.append(color_they)
label_list.append("US Election")
#date: election day
event_time_list.append(nearest(datetime.date(2005,11,8)))
arrow_color_list.append(color_they)
label_list.append("Iran announces that it has successfully enriched Uranium")
#date: day of announcement
event_time_list.append(nearest(datetime.date(2006, 7, 5)))
arrow_color_list.append(color_us)
label_list.append("Northern Rock bank run")
#date: day of run
event_time_list.append(nearest(datetime.date(2007, 9, 14)))
arrow_color_list.append(color_they)
label_list.append("Lehman collapse")
#date: day when bankruptcy was declared
event_time_list.append(nearest(datetime.date(2008, 9, 15)))
arrow_color_list.append(color_they)
"""Get the CP label number"""
if number_labels:
label_list = []
for i in range(0, len(event_time_list)):
label_list.append(str(i+1))
"""Obtain the plot for RLD"""
fig, ax = plt.subplots(1, figsize=(20,5)) #20,5
fig.subplot.top : 0.5
_, myfig = EvT.plot_run_length_distr(buffer=50, show_MAP_CPs = False, mark_median = False,
mark_max = True, upper_limit = 285,
print_colorbar = False, #True
colorbar_location= None, #'bottom',
log_format = True, aspect_ratio = 'auto',
C1=700,C2=1,
time_range = np.linspace(start_comparison - start_test,
stop_comparison - start_test - 2,
stop_comparison-start_comparison-2, dtype=int),
start=(1998 + 1/365), stop = 2009,
all_dates = all_dates,
event_time_list=event_time_list,
label_list=label_list,
space_to_colorbar = 0.75, #0.6
arrow_colors= arrow_color_list,
xlab = "", #"Year"
ax = ax, figure = fig,
number_fontsize = 27, #14
xlab_fontsize =27, #14
ylab_fontsize = 27, #14
xticks_fontsize = 23, #12
yticks_fontsize = 23, #12
arrow_length = 30, #32
arrow_thickness = 4.0,
arrows_setleft_indices = [3],
arrows_setleft_by = [datetime.timedelta(days = 35)],
zero_distance = datetime.timedelta(days=0)
#ylabel_coords = [10, 150] #void
)
"""Save the plot as pdf"""
fig.savefig(baseline_working_directory + "//30portfolio_picture_comparison.pdf",
format = "pdf", dpi = 800)
fig.savefig(baseline_working_directory + "//30portfolio_picture_comparison.jpg",
format = "jpg", dpi = 800)