forked from linhx25/DeepRiskModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathest_factor_ret.py
More file actions
201 lines (150 loc) · 5.81 KB
/
est_factor_ret.py
File metadata and controls
201 lines (150 loc) · 5.81 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
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
import os
import argparse
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
def RLS(y, X, R=None, r=None, w=None):
"""Restricted (Weighted) Least Square
Args:
y (numpy.array): target value, [#sample]
X (numpy.array): factors, [#sample, #factor]
R (numpy.array, optional): constraints, [#factor, #constraint] or [#factor]
r (numpy.array, optional): target value for constraints, [#constraint]
w (numpy.array, optional): sample weights, [#sample]
Returns:
b (numpy.array): regression coefficients
resid (numpy.array): regression residual
tvalues (numpy.array): T-stats for b
r2 (float): R-Squared
Reference:
- [William, 1991] (https://www.jstor.org/stable/2109587)
"""
# weights
if w is not None:
X = X * np.atleast_2d(np.sqrt(w)).T
y = y * np.sqrt(w)
# solve
if R is not None:
R = np.atleast_2d(R)
if r is None:
r = np.zeros(R.shape[0])
z = np.zeros((len(R), len(R)))
W = np.block([[X.T @ X, R.T], [R, z]])
p = np.r_[X.T @ y, r]
W_inv = np.linalg.pinv(W)
m = X.shape[1]
b = W_inv[:m] @ p
X_inv = W_inv[:m, :m] @ X.T
else:
W_inv = np.linalg.pinv(X.T @ X)
X_inv = W_inv @ X.T
b = X_inv @ y
# calc t-value
resid = y - X @ b
ss = (resid**2).sum() / (len(resid) - len(b))
b_var = ss * X_inv @ X_inv.T
tvalues = b / np.sqrt(np.diag(b_var))
# calc r2
r2 = 1 - (resid**2).sum() / (y**2).sum()
# restore resid
if w is not None:
resid /= np.sqrt(w)
return b, resid, tvalues, r2
def calc_factor_ret(factor_exp, stock_ret, stock_cap, num_group=0,
clip_weight=True):
"""estimate factor return
Note:
when `num_group > 0`, we expect the factors are ordered by
[COUNTRY, IND$1, IND$2, ..., IND$num_group, ...]
Args:
factor_exp (pandas.DataFrame): factor exposure, [#stock, #factor]
stock_ret (pandas.Series): stock return, [#stock]
stock_cap (pandas.Series): stock capitalization, [#stock]
num_group (int, optional): number of stock group
clip_weight (bool, optional): whether clip cap weight by 95%
Returns:
factor_ret (pandas.Series): factor return, [#factor]
stock_resid (pandas.Series): stock residual return, [#stock]
factor_tval (pandas.Series): factor return T-values, [#factor]
r2 (float): R-Squared
"""
# store index, output will use this index
orig_index = factor_exp.index
# dropna & align index
factor_exp = factor_exp.dropna()
stock_ret = stock_ret.reindex(factor_exp.index).fillna(0)
stock_cap = stock_cap.reindex(factor_exp.index).fillna(0)
# factor exposure
X = factor_exp.values
# sample weights
w = stock_cap.values**0.5
if clip_weight:
q = np.quantile(w, 0.95)
w[w > q] = q
w = w / w.sum() * len(w)
# constraints
R = None
if num_group > 0:
R = np.zeros(X.shape[1])
slc = slice(1, num_group + 1)
cap_w = stock_cap.values @ X[:, slc]
cap_w = cap_w / cap_w.sum() * len(cap_w)
R[slc] = cap_w
# target
y = stock_ret.values
# regression
b, resid, tvalues, r2 = RLS(y, X, R, w=w)
factor_ret = pd.Series(b, index=factor_exp.columns)
factor_tval = pd.Series(tvalues, index=factor_exp.columns)
stock_resid = pd.Series(resid, index=factor_exp.index)
stock_resid = stock_resid.reindex(orig_index)
return factor_ret, stock_resid, factor_tval, r2
def run(factor_exp, stock_ret, stock_cap, num_group=29, clip_weight=True):
factor_ret = dict()
stock_resid = dict()
factor_tval = dict()
factor_r2 = dict()
dates = factor_exp.index.get_level_values(level=0).unique()
iterator = tqdm(dates)
for date in iterator:
iterator.set_description(str(date)[:10])
ret, resid, tval, r2 = calc_factor_ret(
factor_exp.loc[date], stock_ret.loc[date],
stock_cap.loc[date], num_group=num_group,
clip_weight=clip_weight)
factor_ret[date] = ret
stock_resid[date] = resid
factor_tval[date] = tval
factor_r2[date] = r2
factor_ret = pd.DataFrame(factor_ret).T
stock_resid = pd.DataFrame(stock_resid).T
factor_tval = pd.DataFrame(factor_tval).T
factor_r2 = pd.Series(factor_r2)
return factor_ret, stock_resid, factor_tval, factor_r2
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--outdir', default='data')
parser.add_argument('--num_group', type=int, default=29)
parser.add_argument('--replace', action='store_true')
parser.add_argument('--clip_weight', action='store_true', default=True) # default true
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
factor_exp = pd.read_pickle('data/base.pkl').loc[pd.Timestamp('2015-01-01'):]
stock_ret = pd.read_pickle('data/ret.pkl').loc[pd.Timestamp('2015-01-01'):]
stock_cap = pd.read_pickle('data/cap.pkl').loc[pd.Timestamp('2015-01-01'):]
if os.path.exists(args.outdir + '/pred.pkl'):
df = pd.read_pickle(args.outdir + '/pred.pkl')
df.columns = ['RISK%d'%d for d in range(df.shape[1])]
if args.replace:
factor_exp = factor_exp.iloc[:, :-10]
factor_exp[df.columns] = df.loc[pd.Timestamp('2015-01-01'):]
factor_ret, stock_resid, factor_tval, factor_r2 = run(
factor_exp, stock_ret, stock_cap,
num_group=args.num_group, clip_weight=args.clip_weight
)
factor_ret.to_pickle(args.outdir + '/factor_ret.pkl')
stock_resid.to_pickle(args.outdir + '/stock_resid.pkl')
factor_tval.to_pickle(args.outdir + '/factor_ret_tval.pkl')
factor_r2.to_pickle(args.outdir + '/factor_ret_r2.pkl')