Open williamjin1992 opened 2 years ago
Computing rolling regressions in Python's DataFrame can be a real pain, and the R language, SAS, Stata and Julia all tend to be faster and more flexible than Python. When only fixed-window OLS regression is required, the fastest method I've found so far comes from RollingOLS, a very useful but now barely updated package, hope it helps.
In the file of
char60/beta.py
, I noticed that there is a TODO for a faster way to get rolling Beta estimation, the original functionget_beta
uses the matrix operation of the OLS formula to estimate Beta, I think that the covariance representation can be faster, here is my suggestion:def_get_beta_VCV(df): temp = crsp.loc[df.index,:] vcv = temp.loc[:,['exret','mktrf]].cov(min_periods = None, ddof = 1).values beta = vcv[0,1]/vcv[1,1] return beta
I've tested it on my computer, and it is faster than the matrix operation approach.