-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovariance.py
378 lines (277 loc) · 9.72 KB
/
covariance.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import pandas as pd
import numpy as np
from paper_vrp_mini.helpers import quadratic_form, trim_symmetric_df
class CovarianceDataFrame:
"""Time series of of covariance matrices.
Representation of time series of symmetric covariance matrices, based on
`pandas.DataFrame`. Time is in the 0th level of the index.
Parameters
----------
df : pandas.DataFrame
with pandas.MultiIndex (date, asset)
"""
def __init__(self, df):
"""
"""
df = df.copy()
if df.index.nlevels < 2:
df.index = pd.MultiIndex.from_product(
[[pd.to_datetime("1900-01-01")], list(df.index)])
df.index.names = ["date", "asset"]
df.columns.name = "asset"
# sort
df = df.sort_index(axis=0, level="date", sort_remaining=False)
# unique dates
dt_idx = df.index.get_level_values("date").unique()
self.df = df
self.date_index = dt_idx
self.assets = df.columns
def __getattr__(self, item):
# Redefine to be able to get attributes from the underlying DataFrame
res = getattr(self.df, item)
if callable(res):
res = to_covariancedataframe(res)
return res
def __sub__(self, other):
"""
"""
return CovarianceDataFrame(self.df.__sub__(other.df))
def get_variances(self):
"""Get the time series of diagonal elements (variances).
Returns
-------
res : pandas.DataFrame
of variances, indexed with level 'date' of the original df
"""
def aux_diag_getter(x):
aux_res = pd.Series(np.diag(x), index=x.columns)
return aux_res
res = self.groupby_time().apply(aux_diag_getter)
return res
def __repr__(self):
repr_str = "DataFrame of covariance matrices:\n" + repr(self.df)
return repr_str
def reindex(self, date_index=None, assets=None, **kwargs):
"""Reindex dataframe.
Parameters
----------
date_index : list-like
(optional)
assets : list-like
(optional)
kwargs : any
arguments to `pandas.reindex`
Returns
-------
res : CovarianceDataFrame
"""
df_reix = self.df.copy()
if assets is not None:
df_reix = df_reix\
.reindex(assets, axis=0, level="asset", **kwargs)\
.reindex(columns=assets, **kwargs)
if date_index is not None:
date_index = pd.MultiIndex.from_product(
iterables=[list(date_index), list(self.assets)],
names=["date", "asset"])
df_reix = df_reix.reindex(index=date_index, **kwargs)
res = CovarianceDataFrame(df_reix)
return res
def between(self, start_dt, end_dt):
"""Subsample the dataframe between two dates.
Parameters
----------
start_dt : str
end_dt : str
Returns
-------
res : CovarianceDataFrame
"""
res = CovarianceDataFrame(
self.df\
.unstack(level=1).loc[start_dt:end_dt].stack(level=1)\
.sort_index()
)
return res
def groupby_time(self):
"""Group by the time index.
Implements an analogue of `pandas.DataFrame.iterrows()` for looping
over date rows only; wrapper around `pandas.groupby()`.
Watch out that in pandas.groupby(level=0), MultiIndex is not dropped
for individual grous!
Returns
-------
res : pandas.core.groupby.DataFrameGroupBy
"""
res = self.df.groupby(axis=0, level="date")
return res
def dropna(self, how="all"):
"""Drop (date, matrix) pairs where the matrix only contains NA.
Parameters
----------
how : str
Returns
-------
res : CovarianceDataFrame
"""
# function to get NA-only matrices
def na_func(x):
if how == "all":
all_na_flag = x.isnull().all().all()
else:
all_na_flag = x.isnull().any().any()
return all_na_flag
# find NA-only matrices
na_bool_idx = self.groupby_time().apply(na_func)
# broadcast to be able to .loc on the MultiIndex
na_bool_idx = na_bool_idx.reindex(index=self.df.index, level="date")
# .loc
res_df = self.df.loc[~na_bool_idx]
res = CovarianceDataFrame(res_df)
return res
def group_apply(self, func, level="asset"):
"""Apply function to the dataframe grouped by along the index axis.
Applies to 'slices' of the dataframe, e.g. when resampling. Underlies
most other convenience functions.
Parameters
----------
func : callable
level : str
'date' to apply stuff such as drop missing obs, or
'asset' to apply stuff such as rolling/expanding
Returns
-------
res : CovarianceDataFrame
"""
res = dict()
for n, grp in self.df.groupby(axis=0, level=level):
res[n] = func(grp.xs(n, axis=0, level=level, drop_level=True))
res_df = pd.concat(res, axis=0)
# restores date, asset order
if level == "asset":
res_df = res_df.swaplevel(axis=0)
# back to
res = CovarianceDataFrame(res_df)
return res
def rolling_apply(self, func2d, **kwargs):
"""Rolling trasformation along the time dimension.
Whenever a function needs to be applied to rolling windows of the
same asset, `rolling_apply` provides functionality to do so.
Examples include smoothing covariances of asset_1 with all other
assets over time.
Parameters
----------
func2d : callable
kwargs : dict
arguments to pandas.rolling
Returns
-------
res : CovarianceDataFrame
"""
# auxiliary function to evoke
def aux_func2d(x):
aux_res = x.rolling(**kwargs).apply(func2d)
return aux_res
# loop over assets, do the rolling magic
res = self.group_apply(aux_func2d, level="asset")
return res
def resample(self, func2d, **kwargs):
"""Convenient implementation of `pandas.DataFrame.resample()`.
Use rule='M' in `kwargs` to resample monthly.
Parameters
----------
func2d : callable or str
if str, can only be 'last'
kwargs : any
Returns
-------
res : CovarianceDataFrame
"""
# auxiliary function
def aux_func2d(x):
aux_res = x.resample(**kwargs, axis=0, level="date")
if func2d == "last":
aux_res = aux_res.last()
else:
aux_res = aux_res.apply(func2d)
return aux_res
res = self.group_apply(aux_func2d, level="asset")
return res
def quadratic_form(self, other, **kwargs):
"""Compute time series of quadratic forms.
Parameters
----------
other : pandas.Series or pandas.DataFrame
of the 'bread' of the sandwich; if Series, it will be used for all
dates, if DataFrame, dates will be matched (index of `other` will
be the index of the result)
kwargs : dict
keyword arguments to `quadratic_form()`, such as 'trim'
Returns
-------
res : pandas.Series
of quadratic forms
"""
if isinstance(other, pd.Series):
other = pd.concat({t: other for t in self.date_index}, axis=1).T
# reindex, get only the pandas.DataFrame for the sake of speed
vcv = self.reindex(date_index=other.index, assets=other.columns).df
# do the dot product
res = pd.Series(index=other.index)
for t, row in other.iterrows():
res.loc[t] = quadratic_form(vcv.loc[t], row, **kwargs)
return res
def get_det(self, trim=False):
"""Calculate determinant for each date.
Returns
-------
res : pandas.Series
"""
# auxiliary function to drop date level (otherwise errors) and use
# with `self.groupby_time()`
def det_fun(df):
df_tmp = df.copy()
df_tmp.index = df_tmp.index.droplevel(0)
if trim:
df_tmp = trim_symmetric_df(df_tmp)
if df_tmp.empty:
aux_res = np.nan
else:
aux_res = np.linalg.det(df_tmp.values)
return aux_res
res = self.groupby_time().apply(det_fun)
return res
def shift(self, *args, **kwargs):
"""Shift the dataframe by a number of time periods.
Parameters
----------
args : any
kwargs : any
arguments to `pandas.DataFrame.shift`
Returns
-------
res : CovMatDataFrame
"""
res = self.group_apply(func=lambda x: x.shift(*args, **kwargs),
level="asset")
return res
def to_covariancedataframe(func):
"""Transform generic output of `func` to CovarianceDataFrame.
Returns
-------
wrapper : callable
"""
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return CovarianceDataFrame(res)
return wrapper
if __name__ == "__main__":
df = pd.concat({t: pd.DataFrame(np.eye(4))*t.day
for t in pd.date_range("2001-01-01", periods=10)},
axis=0)
df = pd.DataFrame(np.eye(4))
df.iloc[3, 3] = np.nan
cv_df = CovarianceDataFrame(df)
w = pd.Series(np.ones(shape=(4,)) / 4)
cv_df.quadratic_form(other=w)
cv_df.get_det(trim=True)