def run_regressions(totals: pd.DataFrame, window: int = 3, infectious_period: float = 4.5) -> pd.DataFrame: # run rolling regressions and get parameters model = RollingOLS.from_formula(formula="logdelta ~ time", window=window, data=totals) rolling = model.fit(method="lstsq") growthrates = rolling.params.join(rolling.bse, rsuffix="_stderr") growthrates["rsq"] = rolling.rsquared growthrates.rename( lambda s: s.replace("time", "gradient").replace("const", "intercept"), axis=1, inplace=True) # calculate growth rates growthrates[ "egrowthrateM"] = growthrates.gradient + 2 * growthrates.gradient_stderr growthrates[ "egrowthratem"] = growthrates.gradient - 2 * growthrates.gradient_stderr growthrates["R"] = growthrates.gradient * infectious_period + 1 growthrates[ "RM"] = growthrates.gradient + 2 * growthrates.gradient_stderr * infectious_period + 1 growthrates[ "Rm"] = growthrates.gradient - 2 * growthrates.gradient_stderr * infectious_period + 1 growthrates["date"] = growthrates.index growthrates["days"] = totals.time return growthrates
def test_formula(): y, x, w = gen_data(250, 3, True, pandas=True) fmla = "y ~ 1 + x0 + x1 + x2" data = pd.concat([y, x], axis=1) mod = RollingWLS.from_formula(fmla, window=100, data=data, weights=w) res = mod.fit() alt = RollingWLS(y, x, window=100) alt_res = alt.fit() assert_allclose(res.params, alt_res.params) ols_mod = RollingOLS.from_formula(fmla, window=100, data=data) ols_mod.fit()
def rollingOLS(totals: pd.DataFrame, window: int = 3, infectious_period: float = 4.5) -> pd.DataFrame: """ legacy rolling regression-based implementation of Bettencourt/Ribeiro method """ # run rolling regressions and get parameters model = RollingOLS.from_formula(formula = "logdelta ~ time", window = window, data = totals) rolling = model.fit(method = "lstsq") growthrates = rolling.params.join(rolling.bse, rsuffix="_stderr") growthrates["rsq"] = rolling.rsquared growthrates.rename(lambda s: s.replace("time", "gradient").replace("const", "intercept"), axis = 1, inplace = True) # calculate growth rates growthrates["egrowthrateM"] = growthrates.gradient + 2 * growthrates.gradient_stderr growthrates["egrowthratem"] = growthrates.gradient - 2 * growthrates.gradient_stderr growthrates["R"] = growthrates.gradient * infectious_period + 1 growthrates["RM"] = growthrates.gradient + 2 * growthrates.gradient_stderr * infectious_period + 1 growthrates["Rm"] = growthrates.gradient - 2 * growthrates.gradient_stderr * infectious_period + 1 growthrates["date"] = growthrates.index.get_level_values('status_change_date') growthrates["days"] = totals.time return growthrates
result.dropna( axis=0, how='any', inplace=True ) # not every month we have factor return "result.isnull().sum()" final_result = pd.concat([final_result, longshort], axis=0) # Step 4: Do risk adjustment in FF3F framework # convert factor values to pure factor returns (without size or industry) by using long short just like ff3f factor = pd.DataFrame({ 'yearmonth': result['yearmonth'], f: result['ret_f0f1_high'] - result['ret_f0f1_low'] }) df_return = pd.merge(df_return, factor, on='yearmonth') model = RollingOLS.from_formula(f'{f}~mktrf+smb+hml', data=df_return, window=12).fit() # 1 year rolling asset_pricing = pd.DataFrame([], index=[f + ' exposure', f + ' tstat']) asset_pricing['alpha'] = [ np.mean(model.params['Intercept']), np.mean(model.tvalues['Intercept']) ] asset_pricing['beta_mktrf'] = [ np.mean(model.params['mktrf']), np.mean(model.tvalues['mktrf']) ] asset_pricing['beta_smb'] = [ np.mean(model.params['smb']), np.mean(model.tvalues['smb']) ] asset_pricing['beta_hml'] = [
exog = sm.add_constant(factors[exog_vars]) rols = RollingOLS(endog, exog, window=60) rres = rols.fit() fig = rres.plot_recursive_coefficient(variables=exog_vars, figsize=(14, 18)) # ## Formulas # # `RollingOLS` and `RollingWLS` both support model specification using the # formula interface. The example below is equivalent to the 3-factor model # estimated previously. Note that one variable is renamed to have a valid # Python variable name. joined = pd.concat([factors, industries], axis=1) joined["Mkt_RF"] = joined["Mkt-RF"] mod = RollingOLS.from_formula("HiTec ~ Mkt_RF + SMB + HML", data=joined, window=60) rres = mod.fit() rres.params.tail() # ## `RollingWLS`: Rolling Weighted Least Squares # # The `rolling` module also provides `RollingWLS` which takes an optional # `weights` input to perform rolling weighted least squares. It produces # results that match `WLS` when applied to rolling windows of data. # ## Fit Options # # Fit accepts other optional keywords to set the covariance estimator. # Only two estimators are supported, `'nonrobust'` (the classic OLS # estimator) and `'HC0'` which is White's heteroskedasticity robust