forked from hudson-and-thames/betting-against-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
381 lines (252 loc) · 12.8 KB
/
main.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
### -*- coding: utf-8 -*-
#### library ####
import pandas as pd
import numpy as np
import datetime as dt
from pathlib import Path
#### parameter ####
rf = 0 # risk-free rate, future development: using T-bills rate data
#### path ####
dataPath = Path("Data/")
resultPath = Path("Output/")
#### read data ####
def read_data(name):
"""
"""
dataPath = Path("Data/")
nameDF = name + '_AdjCloseData.csv'
tmpDF = pd.read_csv(dataPath / nameDF, index_col=0) # asset data
nameMarket = name + '.csv'
tmpMarket = pd.read_csv(dataPath / nameMarket, index_col=0) # market data
tickers = list(tmpDF.columns[1:])
tickers.insert(0, name)
dates = [dt for dt in tmpDF.index if dt in tmpMarket.index]
df = tmpDF.loc[dates[0]:dates[-1], :] # asset df
market = tmpMarket.loc[dates[0]:dates[-1], :] # market df
del tmpDF
del tmpMarket
# to datetime
df.index = pd.to_datetime(df.index)
market.index = pd.to_datetime(market.index)
return (df, market, tickers, dates)
############################################
############################################
#### deal with missing data ####
def missing_data(df, market):
"""
TSX one observation fewer than df
"""
if set(market.index) - set(df.index):
missDate = list(set(market.index) - set(df.index)) # find the missing data in df
market = market.drop(missDate, axis = 0) # delete the corresponding data in market data, otherwise, cant compute the correlation between them
else:
missDate = list(set(df.index) - set(market.index)) # find the missing data in df or in market
df = df.drop(missDate, axis = 0)
return (df, market)
############################################
############################################
def daily_returns(df):
"""
daily returns
"""
dailyReturn = lambda series:series.pct_change()
dailyReturnDF = df.apply(dailyReturn, axis = 0)
return dailyReturnDF
def monthly_returns(df):
"""
monthly returns
"""
total_return_from_returns = lambda returns:(returns + 1).prod() - 1 # Returns the return between the first and last value of the DataFrame
monthlyReturn = lambda series:series.pct_change().groupby([series.pct_change().index.year, series.pct_change().index.month])\
.apply(total_return_from_returns)
monthlyReturnDF = df.apply(monthlyReturn, axis = 0)
return monthlyReturnDF
#######
#### estimate beta ###
def estimate_beta(name, df, market):
"""
estimate beta
"""
# log return
log_returns = lambda series:np.diff(np.log(series)) # log return function
returnDF = df.apply(log_returns, axis = 0).fillna(0, axis=1) # too many zeros
returnDF.index = df.index[1:] # assign date to index
returnMarket = market.apply(log_returns, axis = 0).fillna(0, axis=1)
returnMarket.index = market.index[1:]
# three day log return
three_day_log_returns = lambda series:np.log(series).shift(-2) - np.log(series).shift(1)# three day log return function
threeDayReturnDF = df.apply(three_day_log_returns, axis = 0).iloc[1:,].fillna(0, axis=1)
threeDayReturnMarket = market.apply(three_day_log_returns, axis = 0).iloc[1:,].fillna(0, axis=1)
# volatility
volatility = lambda series:series.rolling(250).std()
volatilityDF = returnDF.apply(volatility, axis = 0)
volatilityMarket = returnMarket.apply(volatility, axis = 0)
# correlation
corr = lambda series:series.rolling(250*5).corr(threeDayReturnMarket[name])
corrDF = threeDayReturnDF.apply(corr, axis = 0)
# drop invalid values
idx = volatilityDF.index & volatilityMarket.index & corrDF.index
volatilityDF = volatilityDF.loc[idx, :]
volatilityMarket= volatilityMarket.loc[idx, :]
corrDF = corrDF.loc[idx, :]
# beta
beta = .6 * corrDF.mul(volatilityDF, axis = 0).apply(lambda x:x.div(volatilityMarket[name]), axis = 0) + .4
return (beta, idx)
############################
############################
#### BaB portfolio (daily rebalancing) ####
def portfolio_daily(idx, beta, dailyReturnDF):
"""
daily rebalancing
"""
betaRank = beta.rank(axis = 1) # same value: average their rank
median = betaRank.mean(axis = 1) # average rank on each day
k = 2 / abs(betaRank.subtract(median, axis = 0)).sum(axis = 1) # normalizing constant on each day
w = betaRank.subtract(median, axis = 0).mul(k, axis = 0) # weight (+: high beta, -: low beta)
# BaB factor
wH = w.applymap(lambda x:x if x > 0 else 0) # relative weight assigned to high beta
wL = w.applymap(lambda x:-x if x < 0 else 0) # relative weight assigned to low beta
returnDFBaB = dailyReturnDF.loc[idx, ].shift(-1, axis = 0)
portfolioDailyL = (returnDFBaB.mul(wL, axis = 1).sum(axis = 1) - rf) / (beta.mul(wL, axis = 1).sum(axis = 1))
portfolioDailyH = (returnDFBaB.mul(wH, axis = 1).sum(axis = 1) - rf) / (beta.mul(wH, axis = 1).sum(axis = 1))
portfolioDaily = portfolioDailyL - portfolioDailyH
# adjust index
newIdx = pd.Index(list(portfolioDaily.index)[1:])
portfolioDaily = portfolioDaily[:-1,]
portfolioDaily.index = newIdx
return portfolioDaily
#### BaB portfolio (monthly rebalancing) ####
def portfolio_monthly(idx, beta, monthlyReturnDF):
betaRank = beta.rank(axis = 1) # same value: average their rank
median = betaRank.mean(axis = 1) # average rank on each day
k = 2 / abs(betaRank.subtract(median, axis = 0)).sum(axis = 1) # normalizing constant on each day
w = betaRank.subtract(median, axis = 0).mul(k, axis = 0) # weight (+: high beta, -: low beta)
tail = lambda x:x.tail(1)
monthly = lambda x:x.groupby([x.index.year, x.index.month]).apply(tail)
wMonthly = w.apply(monthly, axis = 0)
betaMonthly = beta.apply(monthly, axis = 0)
wMonthlyL = wMonthly.applymap(lambda x:-x if x < 0 else 0) # relative weight assigned to low beta
wMonthlyH = wMonthly.applymap(lambda x:x if x > 0 else 0) # relative weight assigned to high beta
monthlyIdx = wMonthly.index & betaMonthly.index
monthlyReturnDFBaB = monthlyReturnDF.loc[monthlyIdx, ].shift(-1, axis = 0)
portfolioMonthlyL = (monthlyReturnDFBaB.mul(wMonthlyL, axis = 1).sum(axis = 1) - rf) / (betaMonthly.mul(wMonthlyL, axis = 1).sum(axis = 1))
portfolioMonthlyH = (monthlyReturnDFBaB.mul(wMonthlyH, axis = 1).sum(axis = 1) - rf) / (betaMonthly.mul(wMonthlyH, axis = 1).sum(axis = 1))
portfolioMonthly = portfolioMonthlyL - portfolioMonthlyH
# adjust index
newIdx = pd.Index(list(portfolioMonthly.index)[1:])
portfolioMonthly = portfolioMonthly[:-1,]
portfolioMonthly.index = newIdx
# reset index ( to datetime)
portfolioMonthly.index = portfolioMonthly.index.droplevel([0,1])
return portfolioMonthly
#### Equal-weighted BaB portfolio (monthly rebalancing) ####
def portfolio_monthly_equal_weighted(idx, beta, monthlyReturnDF):
beta = beta.dropna(how = 'all', axis = 0)
median = beta.mean(axis = 1) # average rank on each day
# assign weight to those whose beta larger than mean
compare_1 = lambda x: 1 if x else 0
assign_1 = lambda x: (x > median).apply(compare_1)
tmp_w = beta.apply(assign_1, axis = 0)
avg = lambda x: x.divide(tmp_w.sum(axis = 1))
wH = tmp_w.apply(avg, axis = 0)
# assign weight to those whose beta lower than mean
compare_2 = lambda x: 1 if x else 0
assign_2 = lambda x: (x <= median).apply(compare_2)
tmp_w = beta.apply(assign_2, axis = 0)
avg_2 = lambda x: x.divide(tmp_w.sum(axis = 1))
wL = tmp_w.apply(avg_2, axis = 0)
w = wL - wH
# monthly
tail = lambda x:x.tail(1)
monthly = lambda x:x.groupby([x.index.year, x.index.month]).apply(tail)
wMonthly = w.apply(monthly, axis = 0)
betaMonthly = beta.apply(monthly, axis = 0)
# pick monthly return
monthlyIdx = wMonthly.index & betaMonthly.index
monthlyReturnDFBaB = monthlyReturnDF.loc[monthlyIdx, ].shift(-1, axis = 0)
portfolioMonthly = (monthlyReturnDFBaB * wMonthly).sum(axis = 1)
# adjust index
newIdx = pd.Index(list(portfolioMonthly.index)[1:])
portfolioMonthly = portfolioMonthly[:-1,]
portfolioMonthly.index = newIdx
# reset index ( to datetime)
portfolioMonthly.index = portfolioMonthly.index.droplevel([0,1])
return portfolioMonthly
#### hedge the short position with equal weighted portfolio ####
def portfolio_monthly_hegding_EW(idx, beta, monthlyReturnDF):
tail = lambda x:x.tail(1)
monthly = lambda x:x.groupby([x.index.year, x.index.month]).apply(tail)
## long position
beta = beta.dropna(how = 'all', axis = 0)
median = beta.mean(axis = 1) # average rank on each day
# assign weight to those whose beta larger than median
compare = lambda x: 1 if x else 0
assign = lambda x: (x < median).apply(compare)
tmp_w = beta.apply(assign, axis = 0)
avg = lambda x: x.divide(tmp_w.sum(axis = 1))
wL = tmp_w.apply(avg, axis = 0)
wMonthlyL = wL.apply(monthly, axis = 0)
## short position
betaRank = beta.rank(axis = 1) # same value: average their rank
median = betaRank.mean(axis = 1) # average rank on each day
k = 2 / abs(betaRank.subtract(median, axis = 0)).sum(axis = 1) # normalizing constant on each day
w = betaRank.subtract(median, axis = 0).mul(k, axis = 0) # weight (+: high beta, -: low beta)
wMonthly = w.apply(monthly, axis = 0)
betaMonthly = beta.apply(monthly, axis = 0)
wMonthlyH = wMonthly.applymap(lambda x:x if x > 0 else 0) # relative weight assigned to high beta
monthlyIdx = wMonthly.index & betaMonthly.index
monthlyReturnDFBaB = monthlyReturnDF.loc[monthlyIdx, ].shift(-1, axis = 0)
portfolioMonthlyL = (monthlyReturnDFBaB * wMonthlyL).sum(axis = 1)
portfolioMonthlyH = (monthlyReturnDFBaB.mul(wMonthlyH, axis = 1).sum(axis = 1) - rf) / (betaMonthly.mul(wMonthlyH, axis = 1).sum(axis = 1))
portfolioMonthly = portfolioMonthlyL - portfolioMonthlyH
# adjust index
newIdx = pd.Index(list(portfolioMonthly.index)[1:])
portfolioMonthly = portfolioMonthly[:-1,]
portfolioMonthly.index = newIdx
# reset index ( to datetime)
portfolioMonthly.index = portfolioMonthly.index.droplevel([0,1])
return portfolioMonthly
#### Monthly cumulative return between specified start and end date ####
def portfolio_monthly_cum_ret(portfolioMonthly, start, end):
portfolioMonthly = portfolioMonthly.drop(index = portfolioMonthly.idxmin()) # after dropping the downside outlier
portfolioMonthly = portfolioMonthly.drop(index = portfolioMonthly.idxmax()) # after dropping the downside outlier
# filter date
portfolioMonthly = portfolioMonthly.loc[(portfolioMonthly.index > start) & (portfolioMonthly.index < end)]
# cumulative return
portfolioMonthlyCum = (1 + portfolioMonthly).cumprod()
# add 1 (initial investment) to the first row
firstDate = portfolioMonthlyCum.index[0] - pd.Timedelta(days = portfolioMonthly.index[0].day - 1)
first = pd.Series([initialInvest], index = [firstDate])
portfolioMonthlyCum = pd.concat([first, portfolioMonthlyCum])
return pd.DataFrame(portfolioMonthlyCum)
#### to csv #####
def to_csv_cum_ret(resultPath, name, data, method):
resultName = name + '_' + method + '.csv'
data.index.name = 'Date'
data.columns = [name]
data.to_csv(resultPath / resultName)
###############################################
###############################################
#################################################
name = 'TSX' #SP500/TSX/NYSE
start = dt.datetime(2011, 12, 31)
end = dt.datetime(2020, 1, 1)
initialInvest = 1
df, market, tickers, dates = read_data(name)
df, market = missing_data(df, market)
dailyReturnDF = daily_returns(df)
monthlyReturnDF = monthly_returns(df)
beta, idx = estimate_beta(name, df, market)
portfolioMonthly = portfolio_monthly(idx, beta, monthlyReturnDF)
portfolioMonthlyCum = portfolio_monthly_cum_ret(portfolioMonthly, start, end)
## compare with equal weighted
portfolioMonthly_equal_weighted = portfolio_monthly_equal_weighted(idx, beta, monthlyReturnDF)
portfolioMonthlyCum_equal_weighted = portfolio_monthly_cum_ret(portfolioMonthly_equal_weighted, start, end)
## compare with hedging the short position by equal-weighted long position
portfolioMonthly_hedging_EW = portfolio_monthly_hegding_EW(idx, beta, monthlyReturnDF)
portfolioMonthlyCum_hedging_EW = portfolio_monthly_cum_ret(portfolioMonthly_hedging_EW, start, end)
## to csv
to_csv_cum_ret(resultPath, name, portfolioMonthlyCum, 'CumRet')
to_csv_cum_ret(resultPath, name, portfolioMonthlyCum_equal_weighted, 'CumRet_EqualWeighted')
to_csv_cum_ret(resultPath, name, portfolioMonthlyCum_hedging_EW, 'CumRet_Hedging_EqualWeighted')
##################################