Feng-CityUHK / EquityCharacteristics

Calculate U.S. equity (portfolio) characteristics
https://feng-cityuhk.github.io/EquityCharacteristics/
82 stars 66 forks source link

Possible faster method for calculating Beta #11

Open williamjin1992 opened 2 years ago

williamjin1992 commented 2 years ago

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 function get_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.

BeitianMa commented 1 year 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.