-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvar_dayset.py
193 lines (152 loc) · 6.83 KB
/
cvar_dayset.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
from typing import List, Tuple
import matplotlib.pyplot as plt
from matplotlib.pyplot import Subplot
import numpy as np
import pandas as pd
class CVARDayset:
def __init__(self, pnl_vectors:pd.DataFrame, interpolation:str='lower'):
self.pnl_vectors = pnl_vectors
self.interpolation = interpolation
self.color = 'skyblue'
self.dt_format = '%d-%b-%y'
@property
def total_pnl(self):
return self.pnl_vectors.sum(axis=1)
def var(self, q:float):
return self.total_pnl.quantile(q, interpolation=self.interpolation)
def cvar_index(self, q:float):
return self.pnl_vectors[self.total_pnl < self.var(q)].index
def cvar(self, q:float, days:bool=True, assets:bool=True):
res = self.pnl_vectors.reindex(self.cvar_index(q))
if assets:
res = res.sum(axis=1)
if days:
res = res.mean(axis=0)
return res
def _index_sets(self, other, q:float):
sidx = self.cvar_index(q)
oidx = other.cvar_index(q)
return sidx.difference(oidx), sidx.intersection(oidx)
@staticmethod
def _srs(vals, idxs):
return pd.concat([pd.Series(v, i) for v,i in zip(vals, idxs)])
def _assets(self, other, assets):
if assets is None:
assets = self.pnl_vectors.columns.union(other.pnl_vectors.columns)
return assets, '', ''
else:
return assets, '\nAssets: {}'.format(', '.join(assets)), ' Contribution'
@staticmethod
def _widest_lims(subplots:List[Subplot], x_y:str):
lims = []
for sub in subplots:
plt.axes(sub)
lims.append(getattr(plt, x_y)())
final = (
min([tup[0] for tup in lims]),
max([tup[1] for tup in lims])
)
for sub in subplots:
plt.axes(sub)
getattr(plt, x_y)(final)
return None
@staticmethod
def _new_fig():
fig = plt.figure(figsize=(6.5,6.5))
gs = fig.add_gridspec(5,1)
sub0 = fig.add_subplot(gs[:1,:])
sub1 = fig.add_subplot(gs[1:,:])
return sub0, sub1
def _plot_date_text(self, title, x, index, index_sets):
plt.title(title)
plt.bar(x=x, height=[0])
plt.ylim((0,1))
plt.yticks([0, 1], ['Remain', 'Change'])
plt.xticks([], [])
y = self._srs([.99, .01], index_sets).reindex(index)
for xi, yi, si in zip(x, y, index):
va = 'bottom' if yi == y.min() else 'top'
plt.text(x=xi, y=yi, s=si.strftime(self.dt_format),
horizontalalignment='center',
verticalalignment=va,rotation=90)
def plot_same_days(self, other, q:float, assets:List[str]=None,
subplots:Tuple[Subplot, Subplot]=None):
asssets, assets_title, contribution_str = self._assets(other, assets)
heights = self.cvar(q, days=False, assets=False)
heights = heights.reindex(columns=assets).sum(axis=1).sort_values()
x = np.arange(heights.shape[0])
index_sets = self._index_sets(other, q)
# create axes if None given
sub0, sub1 = self._new_fig() if subplots is None else subplots
# top plot with text
plt.axes(sub0)
title = 'Apply New Positions to Old CVaR Days' + assets_title
self._plot_date_text(title, x, heights.index, index_sets)
# bottom plot with bars
oth_bars = other.pnl_vectors.reindex(columns=assets).sum(axis=1).reindex(heights.index)
sub1.bar(x=x, height=oth_bars, width=1, color=self.color,
edgecolor='black')
sub1.bar(x=x, height=heights, width=1, fill=False, edgecolor='black')
plt.axes(sub1)
plt.xticks([], [])
# summary box
cvar0 = f'{int(heights.mean()):,}'
cvar1 = f'{int(oth_bars.mean()):,}'
s = f'Old CVaR({q*100}%){contribution_str}: {cvar0}'
s = s + f'\nNew Positions on Old CVaR({q*100}%) Days: {cvar1}'
x,y = max(x), min(plt.ylim())*.95
plt.text(x+.5,y,s, verticalalignment='bottom', horizontalalignment='right')
plt.tight_layout()
return None
def plot_new_days(self, other, q:float, assets:List[str]=None,
subplots:Tuple[Subplot, Subplot]=None):
asssets, assets_title, contribution_str = self._assets(other, assets)
heights = other.cvar(q, days=False, assets=False)
heights = heights.reindex(columns=assets).sum(axis=1).sort_values()
x = np.arange(heights.shape[0])
index_sets = other._index_sets(self, q)
# create axes if None given
sub0, sub1 = self._new_fig() if subplots is None else subplots
plt.axes(sub0)
title = 'New Positions on New CVaR Days' + assets_title
self._plot_date_text(title, x, heights.index, index_sets)
# bottom plot with bars
plt.axes(sub1)
plt.bar(x=x, height=heights, width=1, color=self.color, edgecolor='black')
plt.xticks([], [])
cvar = f'{int(heights.mean()):,}'
s = f'New CVaR({q*100}%){contribution_str}: {cvar}'
x,y = max(x), min(plt.ylim())*.95
plt.text(x+.5,y,s, verticalalignment='bottom', horizontalalignment='right')
plt.tight_layout()
return None
def plot_change(self, other, q:float, assets:List[str]=None):
fig = plt.figure(figsize=(16,8))
gs = fig.add_gridspec(5,2)
sub0 = fig.add_subplot(gs[:1,0])
sub1 = fig.add_subplot(gs[1:,0])
self.plot_same_days(other, q, assets, (sub0, sub1))
sub2 = fig.add_subplot(gs[:1,1])
sub3 = fig.add_subplot(gs[1:,1])
self.plot_new_days(other, q, assets, (sub2, sub3))
self._widest_lims([sub1, sub3], 'ylim')
return None
def plot_cvar(self, q:float, assets:List[str]=None):
asssets, assets_title, contribution_str = self._assets(self, assets)
heights = self.cvar(q, days=False, assets=False)
heights = heights.reindex(columns=assets).sum(axis=1).sort_values()
x = np.arange(heights.shape[0])
plt.title('CVaR Days' + assets_title)
plt.bar(x=x, height=heights, width=1, fill=None, edgecolor='black')
plt.xticks([], [])
y = .05*min(plt.ylim())
for xi, si in zip(x, heights.index):
plt.text(x=xi, y=y, s=si.strftime(self.dt_format),
horizontalalignment='center',
verticalalignment='top', rotation=90)
cvar = f'{int(heights.mean()):,}'
s = f'CVaR({q*100}%){contribution_str}: {cvar}'
x,y = max(x), min(plt.ylim())*.95
plt.text(x+.5,y,s, verticalalignment='bottom', horizontalalignment='right')
plt.tight_layout()
return None