# Efficient algorithm for calculating Beta coefficient

I'm using Python/Pandas. Using naive nested for-loops to do Beta calculation for all ~5k stocks by ~5k days (moving window ~250 days) is unbearably slow. Is there any fast and elegant way to accomplish this goal?

Edit: Simply using Numpy instead of Pandas for all the intermediate steps, would speed up the whole process by >10X.

• You mention 'naive' nested for-loops, what do you suspect would be better? What have you tried? – Phil H May 23 '18 at 8:15
• What method are you using to calculate Beta? – amdopt May 23 '18 at 14:14
• Consider - in addition to other proposed solution - trying pandas.iteritems() and pandas.iterrows(). These by-column and by-row iterators are pretty awesome. – Igor Pozdeev May 27 '18 at 20:24

I don’t know how naïve your nested loops are, but I assume you are using the OLS calculation $\beta = (X’X)^{-1}X’Y$, where $X$ contains the index returns and $Y$ contains the security returns.
If you have data for all time periods for all securities, then $(X’X)^{-1}$ will not change for each security. The best solution would be to use numpy to calculate the matrix multiplication directly for all securities. Alternatively, you can calculate $(X’X)^{-1}$ before entering the loop, and then calculate $\beta$ for every individual security.