-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathEventProfiler.py
More file actions
88 lines (71 loc) · 3.04 KB
/
Copy pathEventProfiler.py
File metadata and controls
88 lines (71 loc) · 3.04 KB
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
'''
(c) 2011, 2012 Georgia Tech Research Corporation
This source code is released under the New BSD license. Please see
http://wiki.quantsoftware.org/index.php?title=QSTK_License
for license details.
Created on Jan 16, 2013
@author: Sourabh Bajaj
@contact: sourabh@sourabhbajaj.com
@summary: EventProfiler
'''
import numpy as np
import matplotlib.pyplot as plt
import QSTK.qstkutil.DataAccess as da
import QSTK.qstkutil.tsutil as tsu
import QSTK.qstkutil.qsdateutil as du
def eventprofiler(df_events_arg, d_data, i_lookback=20, i_lookforward=20,
s_filename='study', b_market_neutral=True, b_errorbars=True,
s_market_sym='SPY'):
''' Event Profiler for an event matix'''
df_close = d_data['close'].copy()
df_rets = df_close.copy()
# Do not modify the original event dataframe.
df_events = df_events_arg.copy()
tsu.returnize0(df_rets.values)
if b_market_neutral == True:
df_rets = df_rets.subtract(df_rets[s_market_sym], axis='index')
del df_rets[s_market_sym]
del df_events[s_market_sym]
df_close = df_close.reindex(columns=df_events.columns)
# Removing the starting and the end events
df_events.values[0:i_lookback, :] = np.NaN
df_events.values[-i_lookforward:, :] = np.NaN
# Number of events
i_no_events = int(np.logical_not(np.isnan(df_events.values)).sum())
assert i_no_events > 0, "Zero events in the event matrix"
na_event_rets = "False"
# Looking for the events and pushing them to a matrix
for i, s_sym in enumerate(df_events.columns):
for j, dt_date in enumerate(df_events.index):
if df_events[s_sym][dt_date] == 1:
na_ret = df_rets[s_sym][j - i_lookback:j + 1 + i_lookforward]
if type(na_event_rets) == type(""):
na_event_rets = na_ret
else:
na_event_rets = np.vstack((na_event_rets, na_ret))
if len(na_event_rets.shape) == 1:
na_event_rets = np.expand_dims(na_event_rets, axis=0)
# Computing daily rets and retuns
na_event_rets = np.cumprod(na_event_rets + 1, axis=1)
na_event_rets = (na_event_rets.T / na_event_rets[:, i_lookback]).T
# Study Params
na_mean = np.mean(na_event_rets, axis=0)
na_std = np.std(na_event_rets, axis=0)
li_time = range(-i_lookback, i_lookforward + 1)
# Plotting the chart
plt.clf()
plt.axhline(y=1.0, xmin=-i_lookback, xmax=i_lookforward, color='k')
if b_errorbars == True:
plt.errorbar(li_time[i_lookback:], na_mean[i_lookback:],
yerr=na_std[i_lookback:], ecolor='#AAAAFF',
alpha=0.7)
plt.plot(li_time, na_mean, linewidth=3, label='mean', color='b')
plt.xlim(-i_lookback - 1, i_lookforward + 1)
if b_market_neutral == True:
plt.title('Market Relative mean return of ' +\
str(i_no_events) + ' events')
else:
plt.title('Mean return of ' + str(i_no_events) + ' events')
plt.xlabel('Days')
plt.ylabel('Cumulative Returns')
plt.savefig(s_filename, format='pdf')