bd = BusDay(sql) crsp = CRSP(sql, bd, rdb) logdir = os.path.join(settings['images'], 'weekrev') # Construct weekly reversal rebalbeg = 19730629 # increased stocks coverage in CRSP from around this date rebalend = 20210101 # a Friday, so can include last week in 2020 wd = Weekly(sql, 'Fri', rebalbeg, rebalend) # Generate Friday-end weekly cal # Retrieve weekly returns, standardize scores, and compute returns and i.c. june_universe = 0 # to track when reached a June end to update universe year = 0 # to track new year to retrieve prices in batch for screening res = DataFrame() tic = time.time() for rebaldate in wd.date_range(rebalbeg, rebalend)[:-1]: d = bd.june_universe(rebaldate) if d != june_universe: june_universe = d # update universe every June univ = crsp.get_universe(june_universe) # usual CRSP universe screen univ = univ[univ['decile'] < 10] # drop smalest decile stocks start = wd.begwk(rebaldate) # starting date of rebalance week beg = bd.offset(rebaldate, 1) # beginning date of holding week end = wd.endwk(beg) # ending date of holding week prcdate = bd.offset(start, -1) # require price available at start of week prcyear = (prcdate // 10000) * 10000 if prcyear != year: # retrieve new batch of prices each new year year = prcyear prc = crsp.get_range('daily', 'prc', 'date',
ls = smf.ols("ret ~ BETA + BETA2 + RES", data=rets).fit() print(ls.summary()) print(ls.get_robustcov_results('HC0').summary()) print(ls.get_robustcov_results('HAC', maxlags=3).summary()) print( ls.get_robustcov_results('hac-panel', groups=rets['port'], maxlags=3).summary()) print(ls.get_robustcov_results('cluster', groups=rets['port']).summary()) ## Fama MacBeth with individual stocks and standardized scores as loadings rebalbeg = 19640601 rebalend = LAST_DATE rebaldates = crsp.bd.date_range(rebalbeg, rebalend, 'endmo') loadings = dict() for pordate in rebaldates: # retrieve signal values every month date = bd.june_universe(pordate) univ = crsp.get_universe(date) cap = np.sqrt(crsp.get_cap(date)['cap']) smb = -np.log(cap).rename('size') hml = signals('hml', date, bd.endmo(date, -12))['hml'].rename('value') beta = (signals('beta', pordate, bd.begmo(pordate))['beta'] * 2 / 3) + (1 / 3) mom = signals('mom', pordate)['mom'].rename('momentum') df = pd.concat( (beta, hml, smb, mom), # inner join of signals with univ join='inner', axis=1).reindex(univ.index).dropna() loadings[pordate] = winsorized(df, quantiles=[0.05, 0.95]) ## Compute coefficients from FM cross-sectional regressions riskpremium = RiskPremium(user, bench, 'RF', LAST_DATE)